From 8a60fd10b8761e92379c201150ee0e99fcf73644 Mon Sep 17 00:00:00 2001
From: Legopitstop <1589lego@gmail.com>
Date: Mon, 20 Jan 2025 21:31:42 -0600
Subject: [PATCH 1/4] Added tkinter snippet
---
.../python/[tkinter]/basics/hello-world.md | 22 +++++++++++++++++++
snippets/python/[tkinter]/icon.svg | 1 +
2 files changed, 23 insertions(+)
create mode 100644 snippets/python/[tkinter]/basics/hello-world.md
create mode 100644 snippets/python/[tkinter]/icon.svg
diff --git a/snippets/python/[tkinter]/basics/hello-world.md b/snippets/python/[tkinter]/basics/hello-world.md
new file mode 100644
index 00000000..9dbd2dbb
--- /dev/null
+++ b/snippets/python/[tkinter]/basics/hello-world.md
@@ -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("100x100")
+
+ self.lbl = Label(self, text='Hello, World!')
+ self.lbl.pack(expand=1)
+
+# Usage:
+root = App()
+root.mainloop()
+```
diff --git a/snippets/python/[tkinter]/icon.svg b/snippets/python/[tkinter]/icon.svg
new file mode 100644
index 00000000..9316a704
--- /dev/null
+++ b/snippets/python/[tkinter]/icon.svg
@@ -0,0 +1 @@
+
\ No newline at end of file
From c6e9e612daa9e982c39c9aa8d085c2b31de3f8e6 Mon Sep 17 00:00:00 2001
From: Legopitstop <1589lego@gmail.com>
Date: Tue, 21 Jan 2025 20:32:09 -0600
Subject: [PATCH 2/4] Increased window size
---
snippets/python/[tkinter]/basics/hello-world.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/snippets/python/[tkinter]/basics/hello-world.md b/snippets/python/[tkinter]/basics/hello-world.md
index 9dbd2dbb..12b6e724 100644
--- a/snippets/python/[tkinter]/basics/hello-world.md
+++ b/snippets/python/[tkinter]/basics/hello-world.md
@@ -11,7 +11,7 @@ from tkinter import Tk, Label
class App(Tk):
def __init__(self):
Tk.__init__(self)
- self.geometry("100x100")
+ self.geometry("200x200")
self.lbl = Label(self, text='Hello, World!')
self.lbl.pack(expand=1)
From 7baca3727e74afab6e03e54bc555b231efce2b98 Mon Sep 17 00:00:00 2001
From: Legopitstop <1589lego@gmail.com>
Date: Tue, 21 Jan 2025 20:32:36 -0600
Subject: [PATCH 3/4] Added tkinter snippets
---
.../basics/display-a-pillow-image.md | 49 +++++++++++++++++++
.../entry-validation/allow-alphanumeric.md | 24 +++++++++
.../entry-validation/allow-decimal.md | 32 ++++++++++++
.../allow-digits-with-a-max-length.md | 27 ++++++++++
.../entry-validation/allow-lowercase.md | 24 +++++++++
.../allow-negative-integers.md | 28 +++++++++++
.../allow-numbers-in-range.md | 32 ++++++++++++
.../entry-validation/allow-only-alphabets.md | 24 +++++++++
.../entry-validation/allow-only-digits.md | 24 +++++++++
.../allow-positive-integers.md | 24 +++++++++
.../entry-validation/allow-signed-decimals.md | 32 ++++++++++++
.../entry-validation/allow-signed-integers.md | 30 ++++++++++++
.../allow-specific-characters.md | 25 ++++++++++
.../entry-validation/allow-uppercase.md | 24 +++++++++
.../custom-regular-expression.md | 28 +++++++++++
.../entry-validation/restrict-length.md | 25 ++++++++++
.../entry-validation/validate-file-path.md | 27 ++++++++++
.../python/[tkinter]/menus/context-menu.md | 31 ++++++++++++
18 files changed, 510 insertions(+)
create mode 100644 snippets/python/[tkinter]/basics/display-a-pillow-image.md
create mode 100644 snippets/python/[tkinter]/entry-validation/allow-alphanumeric.md
create mode 100644 snippets/python/[tkinter]/entry-validation/allow-decimal.md
create mode 100644 snippets/python/[tkinter]/entry-validation/allow-digits-with-a-max-length.md
create mode 100644 snippets/python/[tkinter]/entry-validation/allow-lowercase.md
create mode 100644 snippets/python/[tkinter]/entry-validation/allow-negative-integers.md
create mode 100644 snippets/python/[tkinter]/entry-validation/allow-numbers-in-range.md
create mode 100644 snippets/python/[tkinter]/entry-validation/allow-only-alphabets.md
create mode 100644 snippets/python/[tkinter]/entry-validation/allow-only-digits.md
create mode 100644 snippets/python/[tkinter]/entry-validation/allow-positive-integers.md
create mode 100644 snippets/python/[tkinter]/entry-validation/allow-signed-decimals.md
create mode 100644 snippets/python/[tkinter]/entry-validation/allow-signed-integers.md
create mode 100644 snippets/python/[tkinter]/entry-validation/allow-specific-characters.md
create mode 100644 snippets/python/[tkinter]/entry-validation/allow-uppercase.md
create mode 100644 snippets/python/[tkinter]/entry-validation/custom-regular-expression.md
create mode 100644 snippets/python/[tkinter]/entry-validation/restrict-length.md
create mode 100644 snippets/python/[tkinter]/entry-validation/validate-file-path.md
create mode 100644 snippets/python/[tkinter]/menus/context-menu.md
diff --git a/snippets/python/[tkinter]/basics/display-a-pillow-image.md b/snippets/python/[tkinter]/basics/display-a-pillow-image.md
new file mode 100644
index 00000000..050d1a98
--- /dev/null
+++ b/snippets/python/[tkinter]/basics/display-a-pillow-image.md
@@ -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()
+
+```
diff --git a/snippets/python/[tkinter]/entry-validation/allow-alphanumeric.md b/snippets/python/[tkinter]/entry-validation/allow-alphanumeric.md
new file mode 100644
index 00000000..93677aaa
--- /dev/null
+++ b/snippets/python/[tkinter]/entry-validation/allow-alphanumeric.md
@@ -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()
+```
diff --git a/snippets/python/[tkinter]/entry-validation/allow-decimal.md b/snippets/python/[tkinter]/entry-validation/allow-decimal.md
new file mode 100644
index 00000000..3b3e465d
--- /dev/null
+++ b/snippets/python/[tkinter]/entry-validation/allow-decimal.md
@@ -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()
+```
diff --git a/snippets/python/[tkinter]/entry-validation/allow-digits-with-a-max-length.md b/snippets/python/[tkinter]/entry-validation/allow-digits-with-a-max-length.md
new file mode 100644
index 00000000..34559991
--- /dev/null
+++ b/snippets/python/[tkinter]/entry-validation/allow-digits-with-a-max-length.md
@@ -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()
+```
diff --git a/snippets/python/[tkinter]/entry-validation/allow-lowercase.md b/snippets/python/[tkinter]/entry-validation/allow-lowercase.md
new file mode 100644
index 00000000..3abb1fc3
--- /dev/null
+++ b/snippets/python/[tkinter]/entry-validation/allow-lowercase.md
@@ -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()
+```
diff --git a/snippets/python/[tkinter]/entry-validation/allow-negative-integers.md b/snippets/python/[tkinter]/entry-validation/allow-negative-integers.md
new file mode 100644
index 00000000..71286f70
--- /dev/null
+++ b/snippets/python/[tkinter]/entry-validation/allow-negative-integers.md
@@ -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()
+```
diff --git a/snippets/python/[tkinter]/entry-validation/allow-numbers-in-range.md b/snippets/python/[tkinter]/entry-validation/allow-numbers-in-range.md
new file mode 100644
index 00000000..a0837b40
--- /dev/null
+++ b/snippets/python/[tkinter]/entry-validation/allow-numbers-in-range.md
@@ -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()
+```
diff --git a/snippets/python/[tkinter]/entry-validation/allow-only-alphabets.md b/snippets/python/[tkinter]/entry-validation/allow-only-alphabets.md
new file mode 100644
index 00000000..c11b11bc
--- /dev/null
+++ b/snippets/python/[tkinter]/entry-validation/allow-only-alphabets.md
@@ -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()
+```
diff --git a/snippets/python/[tkinter]/entry-validation/allow-only-digits.md b/snippets/python/[tkinter]/entry-validation/allow-only-digits.md
new file mode 100644
index 00000000..a0663cce
--- /dev/null
+++ b/snippets/python/[tkinter]/entry-validation/allow-only-digits.md
@@ -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()
+```
diff --git a/snippets/python/[tkinter]/entry-validation/allow-positive-integers.md b/snippets/python/[tkinter]/entry-validation/allow-positive-integers.md
new file mode 100644
index 00000000..a552958d
--- /dev/null
+++ b/snippets/python/[tkinter]/entry-validation/allow-positive-integers.md
@@ -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()
+```
diff --git a/snippets/python/[tkinter]/entry-validation/allow-signed-decimals.md b/snippets/python/[tkinter]/entry-validation/allow-signed-decimals.md
new file mode 100644
index 00000000..e19e8d1b
--- /dev/null
+++ b/snippets/python/[tkinter]/entry-validation/allow-signed-decimals.md
@@ -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()
+```
diff --git a/snippets/python/[tkinter]/entry-validation/allow-signed-integers.md b/snippets/python/[tkinter]/entry-validation/allow-signed-integers.md
new file mode 100644
index 00000000..eae6307b
--- /dev/null
+++ b/snippets/python/[tkinter]/entry-validation/allow-signed-integers.md
@@ -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()
+```
diff --git a/snippets/python/[tkinter]/entry-validation/allow-specific-characters.md b/snippets/python/[tkinter]/entry-validation/allow-specific-characters.md
new file mode 100644
index 00000000..5f852915
--- /dev/null
+++ b/snippets/python/[tkinter]/entry-validation/allow-specific-characters.md
@@ -0,0 +1,25 @@
+---
+title: Allow Specific Characters
+description: A validation function to allow specific characters.
+author: Legopitstop
+tags: validation,regex
+---
+
+```py
+from tkinter import Tk, Entry
+
+
+def allow_specific_characters(value, allowed_chars):
+ return all(char in allowed_chars for char in value)
+
+
+# Usage:
+root = Tk()
+root.geometry("200x200")
+
+reg = root.register(allow_specific_characters)
+allowed_chars = "0123456789ABCDEFabcdef" # Hexadecimal characters
+Entry(root, validate="key", validatecommand=(reg, "%P", allowed_chars)).pack()
+
+root.mainloop()
+```
diff --git a/snippets/python/[tkinter]/entry-validation/allow-uppercase.md b/snippets/python/[tkinter]/entry-validation/allow-uppercase.md
new file mode 100644
index 00000000..0fac61a1
--- /dev/null
+++ b/snippets/python/[tkinter]/entry-validation/allow-uppercase.md
@@ -0,0 +1,24 @@
+---
+title: Allow Uppercase
+description: A validation function to allow uppercase letters.
+author: Legopitstop
+tags: validation,uppercase
+---
+
+```py
+from tkinter import Tk, Entry
+
+
+def allow_uppercase(value):
+ return value.isupper() or value == ""
+
+
+# Usage:
+root = Tk()
+root.geometry("200x200")
+
+reg = root.register(allow_uppercase)
+Entry(root, validate="key", validatecommand=(reg, "%P")).pack()
+
+root.mainloop()
+```
diff --git a/snippets/python/[tkinter]/entry-validation/custom-regular-expression.md b/snippets/python/[tkinter]/entry-validation/custom-regular-expression.md
new file mode 100644
index 00000000..16b35a87
--- /dev/null
+++ b/snippets/python/[tkinter]/entry-validation/custom-regular-expression.md
@@ -0,0 +1,28 @@
+---
+title: Custom Regular Expression
+description: A validation function to match a regular expression pattern.
+author: Legopitstop
+tags: validation,regex,pattern
+---
+
+```py
+from tkinter import Tk, Entry
+import re
+
+
+def custom_regular_expression(action, value, pattern):
+ if action == "1":
+ return re.fullmatch(pattern, value) is not None
+ return True
+
+
+# Usage:
+root = Tk()
+root.geometry("200x200")
+
+reg = root.register(custom_regular_expression)
+pattern = r"^\d{0,4}$" # Allow up to 4 digits
+Entry(root, validate="key", validatecommand=(reg, "%d", "%P", pattern)).pack()
+
+root.mainloop()
+```
diff --git a/snippets/python/[tkinter]/entry-validation/restrict-length.md b/snippets/python/[tkinter]/entry-validation/restrict-length.md
new file mode 100644
index 00000000..e1d67cba
--- /dev/null
+++ b/snippets/python/[tkinter]/entry-validation/restrict-length.md
@@ -0,0 +1,25 @@
+---
+title: Restrict Length
+description: A validation function to limit the length.
+author: Legopitstop
+tags: validation,length
+---
+
+```py
+from tkinter import Tk, Entry
+
+
+def restrict_length(value, max_length):
+ return len(value) <= int(max_length)
+
+
+# Usage:
+root = Tk()
+root.geometry("200x200")
+
+reg = root.register(restrict_length)
+# 10 is the maximum length allowed
+Entry(root, validate="key", validatecommand=(reg, "%P", 10)).pack()
+
+root.mainloop()
+```
diff --git a/snippets/python/[tkinter]/entry-validation/validate-file-path.md b/snippets/python/[tkinter]/entry-validation/validate-file-path.md
new file mode 100644
index 00000000..351d7a2a
--- /dev/null
+++ b/snippets/python/[tkinter]/entry-validation/validate-file-path.md
@@ -0,0 +1,27 @@
+---
+title: Validate File Path
+description: A validation function to ensure the file path exists.
+author: Legopitstop
+tags: validation,filepath,fp
+---
+
+```py
+from tkinter import Tk, Entry
+import os
+
+
+def validate_file_path(action, value):
+ if action == "1":
+ return value == "" or os.path.exists(os.path.expandvars(value))
+ return True
+
+
+# Usage:
+root = Tk()
+root.geometry("200x200")
+
+reg = root.register(validate_file_path)
+Entry(root, validate="key", validatecommand=(reg, "%d", "%P")).pack()
+
+root.mainloop()
+```
diff --git a/snippets/python/[tkinter]/menus/context-menu.md b/snippets/python/[tkinter]/menus/context-menu.md
new file mode 100644
index 00000000..866ad08b
--- /dev/null
+++ b/snippets/python/[tkinter]/menus/context-menu.md
@@ -0,0 +1,31 @@
+---
+title: Context Menu
+description: Opens a menu when you right click a widget.
+author: Legopitstop
+tags: menu
+---
+
+```py
+from tkinter import Tk, Label, Menu
+
+
+class App(Tk):
+ def __init__(self):
+ Tk.__init__(self)
+ self.geometry("200x200")
+
+ lbl = Label(self, text="Right-click me!")
+ lbl.bind("", self.do_popup)
+ lbl.pack(expand=1, ipadx=10, ipady=10)
+
+ def do_popup(self, event):
+ menu = Menu(self, tearoff=0)
+ menu.add_command(label="Option 1", command=lambda: print("Option 1"))
+ menu.add_command(label="Option 2", command=lambda: print("Option 2"))
+ menu.post(event.x_root, event.y_root)
+
+
+# Usage:
+root = App()
+root.mainloop()
+```
From ead93c6f1fcc46c3004aa8df4d470c4e735a1284 Mon Sep 17 00:00:00 2001
From: Technophile <96492656+technoph1le@users.noreply.github.com>
Date: Mon, 24 Feb 2025 02:02:07 +0200
Subject: [PATCH 4/4] Fix file name matching error
---
snippets/python/[tkinter]/entry-validation/allow-lowercase.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/snippets/python/[tkinter]/entry-validation/allow-lowercase.md b/snippets/python/[tkinter]/entry-validation/allow-lowercase.md
index 3abb1fc3..c016932f 100644
--- a/snippets/python/[tkinter]/entry-validation/allow-lowercase.md
+++ b/snippets/python/[tkinter]/entry-validation/allow-lowercase.md
@@ -1,5 +1,5 @@
---
-title: Allow Lower Case
+title: Allow Lowercase
description: A validation function to allow only lowercase alphabetic characters.
author: Legopitstop
tags: validation,lowercase