Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_WUTYEWCIE.py
- '''
- About: A Community Python Project To Make Python Programming The Easiest As Possible To Do
- .
- At the moment... this project apparently needs to be in a better "partnership" filtering social platform for which each of us can block out anybody in many various ways.
- '''
- import tkinter as tk
- from tkinter import *
- from tkinter import ttk, colorchooser, font
- from random import choice, randint
- import itertools
- import re
- root = tk.Tk()
- root.title("Widget Demo")
- root.geometry(f"+{100}+{5}")
- root.resizable(False, False)
- root.attributes('-topmost', True)
- def pick_color(color_var):
- color = colorchooser.askcolor()[1]
- if color:
- color_var.set(color)
- def oMenu(label, v1, v2, cmd=None):
- try:
- vals = [v2[0]] + v2
- except:
- vals = []
- return ttk.OptionMenu(label, v1, *vals, command=cmd)
- def xMenu(label, v1, v2, cmd=None):
- geom = root.geometry()
- w, h, x, y = map(int, re.findall(r"(\d+)", geom))
- label.grid(row=r, column=1)
- menu_frame = Frame(label)
- menu_frame.grid(row=rrr(), column=1, sticky=E)
- menu_window = Toplevel(label)
- menu_window.overrideredirect(True)
- menu_window.resizable(False, False)
- root.attributes('-topmost', False)
- menu_window.attributes('-topmost', True)
- listbox_frame = Frame(menu_window)
- listbox_frame.pack()
- scrollbar = Scrollbar(listbox_frame, orient=tk.VERTICAL)
- listbox = Listbox(listbox_frame, height=42)
- scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
- listbox.config(yscrollcommand=scrollbar.set)
- listbox.pack(side=tk.LEFT, fill=tk.BOTH)
- scrollbar.config(command=listbox.yview)
- for item in v2:
- listbox.insert(END, item)
- def on_leave(event):
- x = root.winfo_pointerx()
- geom = menu_window.geometry()
- w, h, x2, y2 = map(int, re.findall(r"(\d+)", geom))
- if not (x2 < x < x2 + w + 50):
- menu_window.destroy()
- root.attributes('-topmost', True)
- def on_select(event):
- selected = listbox.get(listbox.curselection())
- v1.set(selected)
- menu_window.destroy()
- listbox.bind("<Leave>", on_leave)
- listbox.bind('<<ListboxSelect>>', on_select)
- listbox.config(width=30)
- menu_window.geometry(f"+{x+80}+{10}")
- def create_beveled_button(parent, text_var, command):
- label = tk.Label(parent, textvariable=text_var,
- relief=tk.RAISED, borderwidth=2, padx=5, pady=5,
- bg="#e1e1e1", fg="#000000")
- label.bind("<Button-1>", command)
- return label
- def toggle_widgets(widget, target, bit='disabled'):
- if target in str(widget).lower():
- try:
- widget.config(state='!disabled')
- except:
- pass
- if isinstance(widget, Entry):
- widget.delete(0, END)
- elif isinstance(widget, Text):
- widget.delete(1.0, END)
- try:
- widget.config(state=bit)
- except:
- pass
- for child in widget.winfo_children():
- toggle_widgets(child, target, bit)
- def apply_syntax():
- selected = selected_widget.get()
- options = {}
- for option in primary_options_list:
- if option in menu_widget:
- option_widget = menu_widget[option]
- if option_label[option].cget('state') != 'disabled':
- if isinstance(option_widget, ttk.OptionMenu):
- value_var = next((var for var in menu_sVar.values() if option in var.get()), None)
- options[option] = value_var.get() if value_var else ""
- else:
- try:
- options[option] = option_widget.get()
- except:
- continue
- elif option in option_label:
- option_widget = option_label[option]
- if option_widget.cget('state') != 'disabled':
- try:
- options[option] = option_widget.get()
- except:
- continue
- syntax = f"{selected}(root, " + ", ".join(f"{key}='{val}'" for key, val in options.items() if val) + ")"
- print(syntax)
- def randomly_create_data(e=None):
- toggle_widgets(root, "", 'disabled')
- selected_widget_menu.config(state='normal')
- apply_button.config(state='normal')
- x, y = [randint(1, 20) * 5 for i in range(2)]
- width, height = [randint(1, 20) * 5 for i in range(2)]
- options = {
- 'x': x,
- 'y': y,
- 'width': width,
- 'height': height,
- 'text': ''.join(choice('qwrtypdfghjkzxvbnm') for _ in range(16)),
- 'font': choice(font_list),
- 'command': choice('copy cut paste exit None'.split()),
- 'image': choice('gif jpg png None'.split()),
- 'relief': choice(relief_list),
- 'state': choice(state_list),
- 'justify': choice(justify_list),
- 'anchor': choice(anchor_list),
- 'fg': '#000000',
- 'bg': '#ffffff',
- 'padx': '',
- 'pady': '',
- 'wrap': '',
- 'wraplength': ''
- }
- selected = selected_widget.get()
- try:
- widget_keys = eval(selected+'().config().keys()')
- except:
- widget_keys = eval(selected+'(root).config().keys()')
- for xy in 'xy':
- option_widget = options_label.grid_slaves(row=primary_options_list.index(xy)+1, column=1)[0]
- option_widget.config(state='normal')
- option_widget.delete(0, END)
- option_widget.insert(0, eval(xy))
- for option in options:
- option_widget = menu_widget[option]
- try:
- menu_sVar[option].set('...')
- except:
- 0
- if option in widget_keys:
- if option in ('font'):
- menu_widget['size']['state'] = 'normal'
- menu_sVar['size'].set(choice(size_options))
- menu_widget['style']['state'] = 'normal'
- menu_sVar['style'].set(choice(font_styles))
- try:
- menu_sVar[option].set(options[option])
- except:
- 0
- try:
- option_label[option].config(state='normal')
- except:
- 0
- try:
- option_widget.config(state='normal')
- option_widget.insert(0, options[option])
- except:
- 0
- try:
- option_widget.set(options[option])
- except:
- 0
- else:
- if option in ('font'):
- menu_widget['size']['state'] = 'normal'
- menu_sVar['size'].set('...')
- menu_widget['style']['state'] = 'normal'
- menu_sVar['style'].set('...')
- r = -1
- def rrr():
- global r
- r += 1
- return r
- option_label = {}
- menu_sVar = {}
- menu_widget = {}
- font_list = list(font.families())
- widget_list = [feature for feature in dir(tk) if feature.lower() not in ('image', 'tk', 'ttk')]
- widget_list = [feature for feature in widget_list if hasattr(eval(f"tk.{feature}"), 'config')]
- widget_list.pop()
- primary_options_list = '''
- x y width height text font image relief state anchor justify padx pady wrap wraplength fg bg command
- '''.strip().split()
- state_list = '''
- normal active disabled
- '''.strip().split()
- justify_list = '''
- center right left
- '''.strip().split()
- anchor_list = '''
- N NE E SE S SW W NW
- '''.strip().split()
- relief_list = '''
- FLAT RAISED SUNKEN GROOVE RIDGE
- '''.strip().split()
- primary_options_list = '''
- x y width height text font image relief state anchor justify padx pady wrap wraplength fg bg command
- '''.strip().split()
- size_options = list(range(6, 400))
- font_styles = list(itertools.product(['', 'bold'], ['', 'roman', 'italic']))
- font_styles = [f"{weight} {slant}".strip() for weight, slant in font_styles]
- options_label = LabelFrame(root, text="Selected Widget Options")
- options_label.grid(padx=3, pady=5)
- selected_widget = StringVar(options_label)
- selected_widget_menu = oMenu(options_label, selected_widget, widget_list, randomly_create_data)
- selected_widget_menu.grid(row=rrr(), column=1, sticky=E)
- selected_widget.set('Canvas')
- for option in primary_options_list:
- option_label[option] = Label(options_label, text=option, fg='red')
- option_label[option].grid(row=rrr(), column=0, sticky=E)
- if option == 'font':
- option_label[option].grid(sticky=W)
- menu_sVar[option] = StringVar(options_label)
- menu_widget[option] = oMenu(options_label, menu_sVar[option], font_list)
- menu_widget[option].grid(row=r, column=0, columnspan=2, sticky=E, padx=(24, 0))
- menu_sVar['size'] = StringVar(options_label)
- menu_widget['size'] = oMenu(options_label, menu_sVar['size'], size_options)
- menu_widget['size'].grid(row=rrr(), column=0, columnspan=2, sticky=W)
- menu_sVar['style'] = StringVar(options_label)
- menu_widget['style'] = oMenu(options_label, menu_sVar['style'], font_styles)
- menu_widget['style'].grid(row=r, column=1, columnspan=2, sticky=E)
- elif option == 'relief':
- menu_sVar[option] = StringVar(options_label)
- menu_widget[option] = oMenu(options_label, menu_sVar[option], relief_list)
- menu_widget[option].grid(row=r, column=1, sticky=E)
- elif option == 'state':
- menu_sVar[option] = StringVar(options_label)
- menu_widget[option] = oMenu(options_label, menu_sVar[option], state_list)
- menu_widget[option].grid(row=r, column=1, sticky=E)
- elif option == 'justify':
- menu_sVar[option] = StringVar(options_label)
- menu_widget[option] = oMenu(options_label, menu_sVar[option], justify_list)
- menu_widget[option].grid(row=r, column=1, sticky=E)
- elif option == 'anchor':
- menu_sVar[option] = StringVar(options_label)
- menu_widget[option] = oMenu(options_label, menu_sVar[option], anchor_list)
- menu_widget[option].grid(row=r, column=1, sticky=E)
- elif option in ['fg', 'bg']:
- menu_widget[option] = Entry(options_label, textvariable=option)
- menu_widget[option].grid(row=r, column=1, padx=2, sticky=W)
- canvas_btn = Canvas(options_label, width=72, height=20)
- canvas_btn.grid(row=r, column=1, padx=2, sticky=E)
- canvas_btn.create_rectangle(0, 0, 72, 20, fill='gray', tag='rectangle')
- canvas_btn.create_text(36, 10, text="Pick Color", fill="white", tag='text', anchor='center')
- canvas_btn.bind('<Button-1>', lambda event, c=menu_widget[option]: pick_color(c))
- else:
- menu_widget[option] = Entry(options_label)
- menu_widget[option].grid(row=r, column=1, padx=2, sticky='ew')
- xm, ym = 0, 0
- def on_pressed(event):
- global xm, ym
- xm, ym = event.x, event.y
- def on_motion(event):
- x = root.winfo_x() - xm + event.x
- y = root.winfo_y() - ym + event.y
- root.geometry(f"+{x}+{y}")
- apply_button = Button(options_label, text="Apply", command=apply_syntax)
- apply_button.grid(row=rrr(), column=0, sticky='ew')
- root.bind("<ButtonPress-1>", on_pressed)
- root.bind("<B1-Motion>", on_motion)
- randomly_create_data()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement