-
-
Notifications
You must be signed in to change notification settings - Fork 130
[Snippets] Added tkinter snippet #240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
title: Display a Pillow Image | ||
description: Use Pillow to show an image in a Tkinter window. | ||
author: Legopitstop | ||
tags: app,hello-world,object-oriented | ||
--- | ||
|
||
```py | ||
from tkinter import Tk, Label | ||
from PIL import Image, ImageDraw, ImageTk | ||
|
||
|
||
class App(Tk): | ||
def __init__(self): | ||
Tk.__init__(self) | ||
self.geometry("200x200") | ||
|
||
# PhotoImage must be global or be assigned to a class or it will be garbage collected. | ||
self.photo = ImageTk.PhotoImage(self.make_image()) | ||
lbl = Label(self, image=self.photo) | ||
lbl.pack(expand=1) | ||
|
||
def make_image(self): | ||
width, height = 200, 200 | ||
image = Image.new("RGB", (width, height), "white") | ||
|
||
# Create a drawing context | ||
draw = ImageDraw.Draw(image) | ||
|
||
# Draw a circle | ||
radius = 80 | ||
center = (width // 2, height // 2) | ||
draw.ellipse( | ||
[ | ||
(center[0] - radius, center[1] - radius), | ||
(center[0] + radius, center[1] + radius), | ||
], | ||
fill="red", | ||
outline="black", | ||
width=3, | ||
) | ||
return image | ||
|
||
|
||
# Usage: | ||
root = App() | ||
root.mainloop() | ||
|
||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
title: Hello, World! | ||
description: Creates a basic Tkinter window with a "Hello, World!" label. | ||
author: Legopitstop | ||
tags: app,hello-world,object-oriented | ||
--- | ||
|
||
```py | ||
from tkinter import Tk, Label | ||
|
||
class App(Tk): | ||
def __init__(self): | ||
Tk.__init__(self) | ||
self.geometry("200x200") | ||
|
||
self.lbl = Label(self, text='Hello, World!') | ||
self.lbl.pack(expand=1) | ||
|
||
# Usage: | ||
root = App() | ||
root.mainloop() | ||
``` |
24 changes: 24 additions & 0 deletions
snippets/python/[tkinter]/entry-validation/allow-alphanumeric.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
title: Allow Alphanumeric | ||
description: A validation function to allow alphanumeric characters. | ||
author: Legopitstop | ||
tags: validation,alphanumeric | ||
--- | ||
|
||
```py | ||
from tkinter import Tk, Entry | ||
|
||
|
||
def allow_alphanumeric(value): | ||
return value.isalnum() or value == "" | ||
|
||
|
||
# Usage: | ||
root = Tk() | ||
root.geometry("200x200") | ||
|
||
reg = root.register(allow_alphanumeric) | ||
Entry(root, validate="key", validatecommand=(reg, "%P")).pack() | ||
|
||
root.mainloop() | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
title: Allow Decimal | ||
description: A validation function to allow only decimal numbers. | ||
author: Legopitstop | ||
tags: validation,decimals | ||
--- | ||
|
||
```py | ||
from tkinter import Tk, Entry | ||
|
||
|
||
def allow_decimal(action, value): | ||
if action == "1": | ||
if value == "": | ||
return True | ||
try: | ||
float(value) | ||
return True | ||
except ValueError: | ||
return False | ||
return True | ||
|
||
|
||
# Usage: | ||
root = Tk() | ||
root.geometry("200x200") | ||
|
||
reg = root.register(allow_decimal) | ||
Entry(root, validate="key", validatecommand=(reg, "%d", "%P")).pack() | ||
|
||
root.mainloop() | ||
``` |
27 changes: 27 additions & 0 deletions
snippets/python/[tkinter]/entry-validation/allow-digits-with-a-max-length.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
title: Allow Digits with A Max Length | ||
description: A validation function to allow only digits with a specified maximum length. | ||
author: Legopitstop | ||
tags: validation,max,length | ||
--- | ||
|
||
```py | ||
from tkinter import Tk, Entry | ||
|
||
|
||
def allow_digits_with_max_length(action, value, max_length): | ||
if action == "1": | ||
return value == "" or (value.isdigit() and len(value) <= int(max_length)) | ||
return True | ||
|
||
|
||
# Usage: | ||
root = Tk() | ||
root.geometry("200x200") | ||
|
||
reg = root.register(allow_digits_with_max_length) | ||
# 4 is the max length | ||
Entry(root, validate="key", validatecommand=(reg, "%d", "%P", 4)).pack() | ||
|
||
root.mainloop() | ||
``` |
24 changes: 24 additions & 0 deletions
snippets/python/[tkinter]/entry-validation/allow-lowercase.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
title: Allow Lowercase | ||
description: A validation function to allow only lowercase alphabetic characters. | ||
author: Legopitstop | ||
tags: validation,lowercase | ||
--- | ||
|
||
```py | ||
from tkinter import Tk, Entry | ||
|
||
|
||
def allow_lowercase(value): | ||
return value.islower() or value == "" | ||
|
||
|
||
# Usage: | ||
root = Tk() | ||
root.geometry("200x200") | ||
|
||
reg = root.register(allow_lowercase) | ||
Entry(root, validate="key", validatecommand=(reg, "%P")).pack() | ||
|
||
root.mainloop() | ||
``` |
28 changes: 28 additions & 0 deletions
snippets/python/[tkinter]/entry-validation/allow-negative-integers.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
title: Allow Negative Integers | ||
description: A validation function to allow only negative integers. | ||
author: Legopitstop | ||
tags: validation,negative,integers | ||
--- | ||
|
||
```py | ||
from tkinter import Tk, Entry | ||
|
||
|
||
def allow_negative_integers(value): | ||
return ( | ||
value in ("", "-") or value.startswith("-") and value[1:].isdigit() | ||
if value | ||
else True | ||
) | ||
|
||
|
||
# Usage: | ||
root = Tk() | ||
root.geometry("200x200") | ||
|
||
reg = root.register(allow_negative_integers) | ||
Entry(root, validate="key", validatecommand=(reg, "%P")).pack() | ||
|
||
root.mainloop() | ||
``` |
32 changes: 32 additions & 0 deletions
snippets/python/[tkinter]/entry-validation/allow-numbers-in-range.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
title: Allow Numbers in Range | ||
description: A validation function to allow only numbers within a specified range. | ||
author: Legopitstop | ||
tags: validation,number,range | ||
--- | ||
|
||
```py | ||
from tkinter import Tk, Entry | ||
|
||
|
||
def allow_numbers_in_range(action, value, min_value, max_value): | ||
if action == "1": | ||
try: | ||
num = float(value) | ||
return float(min_value) <= num <= float(max_value) | ||
except ValueError: | ||
return False | ||
return True | ||
|
||
|
||
# Usage: | ||
root = Tk() | ||
root.geometry("200x200") | ||
|
||
reg = root.register(allow_numbers_in_range) | ||
# 0 is the minimum value | ||
# 10 is the maximum value | ||
Entry(root, validate="key", validatecommand=(reg, "%d", "%P", 0, 10)).pack() | ||
|
||
root.mainloop() | ||
``` |
24 changes: 24 additions & 0 deletions
snippets/python/[tkinter]/entry-validation/allow-only-alphabets.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
title: Allow Only Alphabets | ||
description: A validation function to allow only alphabetic characters. | ||
author: Legopitstop | ||
tags: validation,alphabets | ||
--- | ||
|
||
```py | ||
from tkinter import Tk, Entry | ||
|
||
|
||
def allow_only_alphabets(value): | ||
return value.isalpha() or value == "" | ||
|
||
|
||
# Usage: | ||
root = Tk() | ||
root.geometry("200x200") | ||
|
||
reg = root.register(allow_only_alphabets) | ||
Entry(root, validate="key", validatecommand=(reg, "%P")).pack() | ||
|
||
root.mainloop() | ||
``` |
24 changes: 24 additions & 0 deletions
snippets/python/[tkinter]/entry-validation/allow-only-digits.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
title: Allow Only Digits | ||
description: A validation function to allow only digits. | ||
author: Legopitstop | ||
tags: validation,digits | ||
--- | ||
|
||
```py | ||
from tkinter import Tk, Entry | ||
|
||
|
||
def allow_only_digits(value): | ||
return value.isdigit() or value == "" | ||
|
||
|
||
# Usage: | ||
root = Tk() | ||
root.geometry("200x200") | ||
|
||
reg = root.register(allow_only_digits) | ||
Entry(root, validate="key", validatecommand=(reg, "%P")).pack() | ||
|
||
root.mainloop() | ||
``` |
24 changes: 24 additions & 0 deletions
snippets/python/[tkinter]/entry-validation/allow-positive-integers.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
title: Allow Positive Integers | ||
description: A validation function to allow only positive integers. | ||
author: Legopitstop | ||
tags: validation,positive,integers | ||
--- | ||
|
||
```py | ||
from tkinter import Tk, Entry | ||
|
||
|
||
def allow_positive_integers(value): | ||
return value.isdigit() and (value == "" or int(value) > 0) | ||
|
||
|
||
# Usage: | ||
root = Tk() | ||
root.geometry("200x200") | ||
|
||
reg = root.register(allow_positive_integers) | ||
Entry(root, validate="key", validatecommand=(reg, "%P")).pack() | ||
|
||
root.mainloop() | ||
``` |
32 changes: 32 additions & 0 deletions
snippets/python/[tkinter]/entry-validation/allow-signed-decimals.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
title: Allow signed Decimals | ||
description: A validation function to allow only signed decimal numbers. | ||
author: Legopitstop | ||
tags: validation,signed,decimals | ||
--- | ||
|
||
```py | ||
from tkinter import Tk, Entry | ||
|
||
|
||
def allow_signed_decimals(action, value): | ||
if action == "1": | ||
try: | ||
if value in ("", "-"): | ||
return True | ||
float(value) | ||
return True | ||
except ValueError: | ||
return False | ||
return True | ||
|
||
|
||
# Usage: | ||
root = Tk() | ||
root.geometry("200x200") | ||
|
||
reg = root.register(allow_signed_decimals) | ||
Entry(root, validate="key", validatecommand=(reg, "%d", "%P")).pack() | ||
|
||
root.mainloop() | ||
``` |
30 changes: 30 additions & 0 deletions
snippets/python/[tkinter]/entry-validation/allow-signed-integers.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
title: Allow Signed Integers | ||
description: A validation function to allow only signed integers. | ||
author: Legopitstop | ||
tags: validation,signed,integers | ||
--- | ||
|
||
```py | ||
from tkinter import Tk, Entry | ||
|
||
|
||
def allow_signed_integers(action, value): | ||
if action == "1": | ||
return ( | ||
value in ("", "-") | ||
or value.isdigit() | ||
or (value.startswith("-") and value[1:].isdigit()) | ||
) | ||
return True | ||
|
||
|
||
# Usage: | ||
root = Tk() | ||
root.geometry("200x200") | ||
|
||
reg = root.register(allow_signed_integers) | ||
Entry(root, validate="key", validatecommand=(reg, "%d", "%P")).pack() | ||
|
||
root.mainloop() | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.