Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 03c6e91

Browse files
Merge pull request #240 from legopitstop/main
[Snippets] Added tkinter snippet
2 parents 8202e68 + ead93c6 commit 03c6e91

20 files changed

+533
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
title: Display a Pillow Image
3+
description: Use Pillow to show an image in a Tkinter window.
4+
author: Legopitstop
5+
tags: app,hello-world,object-oriented
6+
---
7+
8+
```py
9+
from tkinter import Tk, Label
10+
from PIL import Image, ImageDraw, ImageTk
11+
12+
13+
class App(Tk):
14+
def __init__(self):
15+
Tk.__init__(self)
16+
self.geometry("200x200")
17+
18+
# PhotoImage must be global or be assigned to a class or it will be garbage collected.
19+
self.photo = ImageTk.PhotoImage(self.make_image())
20+
lbl = Label(self, image=self.photo)
21+
lbl.pack(expand=1)
22+
23+
def make_image(self):
24+
width, height = 200, 200
25+
image = Image.new("RGB", (width, height), "white")
26+
27+
# Create a drawing context
28+
draw = ImageDraw.Draw(image)
29+
30+
# Draw a circle
31+
radius = 80
32+
center = (width // 2, height // 2)
33+
draw.ellipse(
34+
[
35+
(center[0] - radius, center[1] - radius),
36+
(center[0] + radius, center[1] + radius),
37+
],
38+
fill="red",
39+
outline="black",
40+
width=3,
41+
)
42+
return image
43+
44+
45+
# Usage:
46+
root = App()
47+
root.mainloop()
48+
49+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: Hello, World!
3+
description: Creates a basic Tkinter window with a "Hello, World!" label.
4+
author: Legopitstop
5+
tags: app,hello-world,object-oriented
6+
---
7+
8+
```py
9+
from tkinter import Tk, Label
10+
11+
class App(Tk):
12+
def __init__(self):
13+
Tk.__init__(self)
14+
self.geometry("200x200")
15+
16+
self.lbl = Label(self, text='Hello, World!')
17+
self.lbl.pack(expand=1)
18+
19+
# Usage:
20+
root = App()
21+
root.mainloop()
22+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: Allow Alphanumeric
3+
description: A validation function to allow alphanumeric characters.
4+
author: Legopitstop
5+
tags: validation,alphanumeric
6+
---
7+
8+
```py
9+
from tkinter import Tk, Entry
10+
11+
12+
def allow_alphanumeric(value):
13+
return value.isalnum() or value == ""
14+
15+
16+
# Usage:
17+
root = Tk()
18+
root.geometry("200x200")
19+
20+
reg = root.register(allow_alphanumeric)
21+
Entry(root, validate="key", validatecommand=(reg, "%P")).pack()
22+
23+
root.mainloop()
24+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: Allow Decimal
3+
description: A validation function to allow only decimal numbers.
4+
author: Legopitstop
5+
tags: validation,decimals
6+
---
7+
8+
```py
9+
from tkinter import Tk, Entry
10+
11+
12+
def allow_decimal(action, value):
13+
if action == "1":
14+
if value == "":
15+
return True
16+
try:
17+
float(value)
18+
return True
19+
except ValueError:
20+
return False
21+
return True
22+
23+
24+
# Usage:
25+
root = Tk()
26+
root.geometry("200x200")
27+
28+
reg = root.register(allow_decimal)
29+
Entry(root, validate="key", validatecommand=(reg, "%d", "%P")).pack()
30+
31+
root.mainloop()
32+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: Allow Digits with A Max Length
3+
description: A validation function to allow only digits with a specified maximum length.
4+
author: Legopitstop
5+
tags: validation,max,length
6+
---
7+
8+
```py
9+
from tkinter import Tk, Entry
10+
11+
12+
def allow_digits_with_max_length(action, value, max_length):
13+
if action == "1":
14+
return value == "" or (value.isdigit() and len(value) <= int(max_length))
15+
return True
16+
17+
18+
# Usage:
19+
root = Tk()
20+
root.geometry("200x200")
21+
22+
reg = root.register(allow_digits_with_max_length)
23+
# 4 is the max length
24+
Entry(root, validate="key", validatecommand=(reg, "%d", "%P", 4)).pack()
25+
26+
root.mainloop()
27+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: Allow Lowercase
3+
description: A validation function to allow only lowercase alphabetic characters.
4+
author: Legopitstop
5+
tags: validation,lowercase
6+
---
7+
8+
```py
9+
from tkinter import Tk, Entry
10+
11+
12+
def allow_lowercase(value):
13+
return value.islower() or value == ""
14+
15+
16+
# Usage:
17+
root = Tk()
18+
root.geometry("200x200")
19+
20+
reg = root.register(allow_lowercase)
21+
Entry(root, validate="key", validatecommand=(reg, "%P")).pack()
22+
23+
root.mainloop()
24+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: Allow Negative Integers
3+
description: A validation function to allow only negative integers.
4+
author: Legopitstop
5+
tags: validation,negative,integers
6+
---
7+
8+
```py
9+
from tkinter import Tk, Entry
10+
11+
12+
def allow_negative_integers(value):
13+
return (
14+
value in ("", "-") or value.startswith("-") and value[1:].isdigit()
15+
if value
16+
else True
17+
)
18+
19+
20+
# Usage:
21+
root = Tk()
22+
root.geometry("200x200")
23+
24+
reg = root.register(allow_negative_integers)
25+
Entry(root, validate="key", validatecommand=(reg, "%P")).pack()
26+
27+
root.mainloop()
28+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: Allow Numbers in Range
3+
description: A validation function to allow only numbers within a specified range.
4+
author: Legopitstop
5+
tags: validation,number,range
6+
---
7+
8+
```py
9+
from tkinter import Tk, Entry
10+
11+
12+
def allow_numbers_in_range(action, value, min_value, max_value):
13+
if action == "1":
14+
try:
15+
num = float(value)
16+
return float(min_value) <= num <= float(max_value)
17+
except ValueError:
18+
return False
19+
return True
20+
21+
22+
# Usage:
23+
root = Tk()
24+
root.geometry("200x200")
25+
26+
reg = root.register(allow_numbers_in_range)
27+
# 0 is the minimum value
28+
# 10 is the maximum value
29+
Entry(root, validate="key", validatecommand=(reg, "%d", "%P", 0, 10)).pack()
30+
31+
root.mainloop()
32+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: Allow Only Alphabets
3+
description: A validation function to allow only alphabetic characters.
4+
author: Legopitstop
5+
tags: validation,alphabets
6+
---
7+
8+
```py
9+
from tkinter import Tk, Entry
10+
11+
12+
def allow_only_alphabets(value):
13+
return value.isalpha() or value == ""
14+
15+
16+
# Usage:
17+
root = Tk()
18+
root.geometry("200x200")
19+
20+
reg = root.register(allow_only_alphabets)
21+
Entry(root, validate="key", validatecommand=(reg, "%P")).pack()
22+
23+
root.mainloop()
24+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: Allow Only Digits
3+
description: A validation function to allow only digits.
4+
author: Legopitstop
5+
tags: validation,digits
6+
---
7+
8+
```py
9+
from tkinter import Tk, Entry
10+
11+
12+
def allow_only_digits(value):
13+
return value.isdigit() or value == ""
14+
15+
16+
# Usage:
17+
root = Tk()
18+
root.geometry("200x200")
19+
20+
reg = root.register(allow_only_digits)
21+
Entry(root, validate="key", validatecommand=(reg, "%P")).pack()
22+
23+
root.mainloop()
24+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /