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]/basics/hello-world.md b/snippets/python/[tkinter]/basics/hello-world.md new file mode 100644 index 00000000..12b6e724 --- /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("200x200") + + self.lbl = Label(self, text='Hello, World!') + self.lbl.pack(expand=1) + +# 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..c016932f --- /dev/null +++ b/snippets/python/[tkinter]/entry-validation/allow-lowercase.md @@ -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() +``` 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]/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 @@ +<svg version="1.2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50" width="50" height="50"><style>.a{fill:#f14729}.b{fill:#1e5cb3}.c{fill:#fff}.d{fill:#fff200}</style><path class="a" d="m19.9 48.8l20.4 0.7-1.2-25.2 1.9-23-9.4 0.5-4.4-1.8-19.2 1 1 22.7-1 26.2z"/><path class="b" d="m26.5 30.7l-1.8 0.4 1-2.3 0.9-6.3-1.1-0.3 1.4-2 1.7-6.6-3.1 0.7c0 0 1.7-3 3.4-5l-2.3-1.7c0 0-7.3 3.2-5.3 13.7l-1.5-0.3 1.4 11-1.2 0.3c0 0 1.3 2.6 3 3.8 0.2 0.1-2.2-8.6 0.3-17.3l0.3 0.6c0 0-2 9.2 0 16.7 0 0 0.1 0.2 1.1 0.1 0 0 1.8-3.5 1.8-5.5z"/><path class="c" d="m13.6 38.5h0.3v-0.7q0.3 0 0.5 0v0.7h0.5v0.2h-0.5v1.8c0 0.1 0 0.3 0.2 0.3 0.2 0 0.2-0.1 0.3-0.2h0.1c-0.1 0.3-0.4 0.4-0.6 0.4-0.4 0-0.5-0.2-0.5-0.5v-1.8h-0.3z"/><path class="c" d="m18.9 40.2c-0.2 0.4-0.4 0.8-1.1 0.8-0.9 0-1.2-0.8-1.2-1.2 0-1.1 0.9-1.3 1.2-1.3 0.4 0 1 0.1 1 0.6 0 0.2-0.1 0.3-0.3 0.3-0.2 0-0.3-0.1-0.3-0.3 0-0.2 0.2-0.2 0.2-0.3 0-0.1-0.4-0.2-0.5-0.2-0.6 0-0.7 0.4-0.7 1.2 0 0.4 0.1 0.6 0.1 0.7q0.1 0.3 0.6 0.4c0.3 0 0.7-0.3 0.8-0.7z"/><path class="c" d="m20.5 40.9v-0.1h0.5v-3.9h-0.5v-0.1h0.3q0.3 0 0.7-0.1v4.1h0.4v0.1z"/><path class="c" d="m26.3 38.5h0.4v-0.7q0.2 0 0.4 0v0.7h0.5v0.2h-0.5v1.8c0 0.1 0 0.3 0.2 0.3 0.2 0 0.3-0.1 0.4-0.2h0.1c-0.1 0.3-0.4 0.4-0.7 0.4-0.4 0-0.5-0.2-0.5-0.5v-1.8h-0.3z"/><path class="c" d="m30.8 38.7v-0.2h1v0.2h-0.4l-0.8 0.7 1.1 1.4h0.3v0.1h-1.2v-0.1h0.3l-1-1.2 1.1-0.9zm-1.5 2.2v-0.1h0.3v-3.9h-0.3v-0.1h0.1q0.4 0 0.7-0.1v4.1h0.4v0.1z"/><path class="c" d="m24.1 18c0 0-1.1 4.8-0.8 12.5 0.1 2.3 0.4 4.4 0.7 6.2 0.2 1.7 0.3 2.2 0.9 4.2l-0.1 1.2c0 0-4.2-13.3-0.7-24.1z"/><path class="c" d="m26.7 13.9c0 0 0.9-0.4 2.3-0.6 0 0 1.4 2.7-2.2 7.3 0 0 2.2-4.8 1.7-6.3 0 0-0.6-0.4-1.8-0.4z"/><path class="c" d="m26.2 22c0 0 0.9 0.2 1.3-0.1 0 0-0.8 3.1-0.6 4.8 0.2 1.8-1.3 2.9-1.3 2.9 0 0 1.3-1.7 0.9-3-0.3-0.9 0-3.3 0.4-4 0 0-0.4-0.1-0.7-0.6z"/><path class="c" d="m25.4 30.3c0 0 1.4-0.2 1.5-0.9 0 0 0.2 4.2-1.7 5.9 0 0 1.4-2.3 1.2-4.6z"/><path class="c" d="m21.5 32c0 0-0.5 0.5-1.8-0.6 0 0 0.4 2.8 3.1 4.1 0 0-1.9-1.3-2.4-2.8 0 0 0.7-0.2 1.1-0.7z"/><path class="c" d="m21 30.9c0 0-1.8-4.6-1.4-10.9 0 0 0.4 1.2 1.8 1.6 0 0-0.7 0.1-1 0 0 0-1 2.7 0.6 9.3z"/><path class="c" d="m28.7 9.4c0 0-0.7-5-5 0.1-3.4 4-3 7.4-2.8 9.4 0.1 0.5-0.6-5.2 3.1-8.9 3.8-3.7 4.6-0.8 4.7-0.6z"/><path class="d" d="m22.4 5.1c0 0-3.4-1-4.4 10.2 0 0-0.9-3.5 1.3-8.3 2.1-4.9 3.1-1.9 3.1-1.9z"/><path class="d" d="m18.2 4.3c0 0-1.7 2.6-1.3 5.3 0 0-1.3-5.2 1.3-5.3z"/><path class="d" d="m30.8 11.5c0 0 2.1-0.7-0.8 6.8 0 0 1.5-2.2 2.4-5.7 0.6-2.2-1.6-1.1-1.6-1.1z"/><path class="d" d="m34.1 10.9c0 0 1.5 1.3 0.4 3 0 0 2.6-2.6-0.4-3z"/><path class="d" d="m14 43.6c0 0.1 0 0.2-0.1 0.2-0.2 0-0.2-0.1-0.2-0.2v-0.6h0.1v0.5c0 0.2 0 0.3 0.1 0.3 0.1 0 0.1-0.1 0.1-0.3v-0.5z"/><path class="d" d="m15.1 43v0.5q0 0.1 0 0.2 0-0.1 0-0.1l-0.2-0.6h-0.1v0.8h0.1v-0.5q0-0.1-0.1-0.2 0.1 0.1 0.1 0.1l0.2 0.6z"/><path class="d" d="m16 43.8h-0.1v-0.8h0.1z"/><path class="d" d="m16.9 43.8h-0.1l-0.1-0.8 0.2 0.6q0 0 0 0.1 0-0.1 0-0.1l0.1-0.6h0.1z"/><path class="d" d="m18 43h-0.2v0.3h0.2v0.1h-0.2v0.4h0.2-0.2v-0.8h0.2z"/><path class="d" d="m18.8 43.8v-0.8c0.2 0 0.3 0 0.3 0.2 0 0.1 0 0.2-0.2 0.2l0.2 0.4h-0.1l-0.2-0.4v0.4zm0.1-0.5h0.1c0.1 0 0.1 0 0.1-0.1 0-0.2-0.1-0.2-0.2-0.2z"/><path class="d" d="m20.1 43.1q0-0.1-0.1-0.1-0.1 0-0.1 0.2c0 0.1 0 0.1 0.1 0.2 0.1 0 0.2 0.1 0.2 0.2 0 0.1-0.1 0.2-0.3 0.2q0 0-0.1 0v-0.1q0.1 0.1 0.1 0.1c0.1 0 0.2-0.1 0.2-0.2 0-0.1-0.1-0.1-0.2-0.2-0.1 0-0.1-0.1-0.1-0.2 0-0.1 0-0.3 0.2-0.3q0 0 0.1 0.1z"/><path class="d" d="m21.2 43.8h-0.1v-0.2h-0.2v0.2h-0.1l0.2-0.8zm-0.1-0.3l-0.1-0.3q0-0.1 0-0.2 0 0.1 0 0.2l-0.1 0.3z"/><path class="d" d="m22 43.8h0.2-0.3v-0.8h0.1z"/><path class="d" d="m24 43.1q-0.1-0.1-0.2-0.1-0.1 0-0.1 0.2c0 0.1 0.1 0.1 0.2 0.2 0 0 0.1 0.1 0.1 0.2 0 0.1-0.1 0.2-0.2 0.2q-0.1 0-0.1 0v-0.1q0 0.1 0.1 0.1c0.1 0 0.1-0.1 0.1-0.2 0-0.1 0-0.1-0.1-0.2-0.1 0-0.2-0.1-0.2-0.2 0-0.1 0.1-0.3 0.2-0.3q0.1 0 0.2 0.1z"/><path class="d" d="m25 43.7q0 0.1-0.1 0.1c-0.1 0-0.1-0.3-0.1-0.4 0-0.2 0-0.4 0.1-0.4q0.1 0 0.1 0.1v-0.1q0-0.1-0.1-0.1c-0.2 0-0.2 0.4-0.2 0.5 0 0.2 0 0.4 0.2 0.4q0.1 0 0.1 0z"/><path class="d" d="m25.8 43.8v-0.8c0.2 0 0.3 0 0.3 0.2 0 0.1 0 0.2-0.2 0.2l0.2 0.4h-0.1l-0.2-0.4v0.4zm0.1-0.5c0.2 0 0.2 0 0.2-0.1 0-0.2-0.1-0.2-0.2-0.2z"/><path class="d" d="m26.9 43.8h-0.1v-0.8h0.1z"/><path class="d" d="m27.6 43.8v-0.8h0.1c0.1 0 0.3 0 0.3 0.2 0 0.2-0.1 0.2-0.3 0.2v0.4zm0.1-0.5c0.1 0 0.1 0 0.1-0.1 0-0.1 0-0.2-0.1-0.2z"/><path class="d" d="m28.9 43h-0.3 0.1v0.8h0.1v-0.8h0.1z"/><path class="d" d="m29.7 43.8h-0.1v-0.8h0.1z"/><path class="d" d="m30.8 43.8h-0.1l-0.2-0.6q0 0 0-0.1 0 0.1 0 0.2v0.5h-0.1v-0.8h0.1l0.2 0.6q0 0 0 0.1 0-0.1 0-0.2v-0.5h0.1z"/><path class="d" d="m31.7 43.4h0.2v0.1c0 0.2 0 0.3-0.2 0.3-0.2 0-0.2-0.2-0.2-0.4 0-0.2 0-0.5 0.2-0.5q0.2 0.1 0.2 0.2h-0.1c0 0 0-0.1-0.1-0.1-0.1 0-0.1 0.2-0.1 0.4 0 0.3 0 0.4 0.1 0.4 0.1 0 0.1-0.1 0.1-0.3v-0.1h-0.1z"/></svg> \ No newline at end of file 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("<Button-3>", 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() +```