Skip to content
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

[Snippets] Added tkinter snippet #240

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions snippets/python/[tkinter]/basics/display-a-pillow-image.md
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()

```
22 changes: 22 additions & 0 deletions snippets/python/[tkinter]/basics/hello-world.md
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()
```
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()
```
32 changes: 32 additions & 0 deletions snippets/python/[tkinter]/entry-validation/allow-decimal.md
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()
```
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: Allow Lower Case
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()
```
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()
```
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()
```
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
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()
```
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()
```
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()
```
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()
```
Loading