Advertisement
here2share

# tk_WUTYEWCIE.py ^

Sep 21st, 2024 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.78 KB | None | 0 0
  1. # tk_WUTYEWCIE.py
  2.  
  3. '''
  4. About: Tkinter WUTYEWCIE - A Community Project For You To Make Python Scripts Your Way
  5. '''
  6.  
  7. import tkinter as tk
  8. from tkinter import *
  9. from tkinter import ttk, colorchooser, font
  10. from random import choice, randint
  11. import itertools
  12. import re
  13.  
  14. root = tk.Tk()
  15. root.geometry(f"+{100}+{5}")
  16. root.resizable(False, False)
  17. root.overrideredirect(True)
  18. root.attributes('-topmost', True)
  19.  
  20. def pick_color(color_var):
  21.     color = colorchooser.askcolor()[1]
  22.     if color:
  23.         color_var.delete(0, END)
  24.         color_var.insert(0, color)
  25.  
  26. def oMenu(label, v1, v2, cmd=None):
  27.     try:
  28.         vals = [v2[0]] + v2
  29.     except:
  30.         vals = []
  31.     return ttk.OptionMenu(label, v1, *vals, command=cmd)
  32.    
  33. def xMenu(label, v1, v2):
  34.     btn = Button(label, textvariable=v1, relief=FLAT, command=lambda: xMenu2(label, v1, v2))
  35.     btn.config(bg=label.cget("bg"), fg=label.cget("fg"))
  36.     btn.grid(row=rrr(), column=0, sticky=E)
  37.     return btn
  38.  
  39. def xMenu2(label, v1, v2):
  40.     geom = root.geometry()     
  41.     x = label.winfo_rootx()
  42.     menu_window = Toplevel(root)
  43.     menu_window.overrideredirect(True)
  44.     menu_window.resizable(False, False)
  45.     root.attributes('-topmost', False)
  46.     menu_window.attributes('-topmost', True)
  47.    
  48.     listbox_frame = Frame(menu_window)
  49.     listbox_frame.pack()
  50.  
  51.     listbox = Listbox(listbox_frame, height=42)
  52.     listbox.grid(row=0, column=0, sticky=tk.NSEW)
  53.  
  54.     scrollbar = Scrollbar(listbox_frame, orient=tk.VERTICAL)
  55.     scrollbar.grid(row=0, column=1, sticky=tk.NS)
  56.  
  57.     listbox.config(yscrollcommand=scrollbar.set)
  58.     scrollbar.config(command=listbox.yview)
  59.  
  60.     for item in v2:
  61.         listbox.insert(END, item)
  62.        
  63.     def on_motion(event):
  64.         root.attributes('-topmost', True)
  65.         menu_window.destroy()
  66.  
  67.     def on_select(event):
  68.         focused = listbox.get(listbox.curselection())
  69.         v1.set(focused)
  70.         menu_window.destroy()
  71.        
  72.     listbox_frame.bind("<Leave>", on_motion)
  73.     listbox.bind('<<ListboxSelect>>', on_select)
  74.     listbox.config(width=30)
  75.    
  76.     menu_window.geometry(f"+{x}+{10}")
  77.    
  78. def toggle_widgets(widget, target, bit='disabled'):
  79.     if target in str(widget).lower():
  80.         try:
  81.             widget.config(state='!disabled')
  82.         except:
  83.             pass
  84.         if isinstance(widget, Entry):
  85.             widget.delete(0, END)
  86.         elif isinstance(widget, Text):
  87.             widget.delete(1.0, END)
  88.         try:
  89.             widget.config(state=bit)
  90.         except:
  91.             pass
  92.  
  93.     for child in widget.winfo_children():
  94.         toggle_widgets(child, target, bit)
  95.  
  96. def apply_syntax():
  97.     focused = focused_widget.get()
  98.     options = []
  99.    
  100.     if focused:
  101.         for option in primary_options_list:
  102.             option_widget = menu_widget[option]
  103.             if option_label[option].cget('state') != 'disabled':
  104.                 if isinstance(option_widget, ttk.OptionMenu):
  105.                     var = menu_sVar[option].get()
  106.                 else:
  107.                     var = option_widget.get()
  108.                 if var:
  109.                     options.append(f"{option}={var}")
  110.  
  111.         syntax = f"{focused}(root, " + ", ".join(options) + ")"
  112.         print(syntax)
  113.    
  114. def randomly_create_data(e=None):          
  115.     toggle_widgets(root, "", 'disabled')
  116.     category_widget_menu.config(state='normal')
  117.     focused_widget_menu.config(state='normal')
  118.     apply_button.config(state='normal')
  119.     exit_button.config(state='normal')
  120.     x, y = [randint(1, 20) * 5 for i in range(2)]
  121.     width, height = [randint(1, 20) * 5 for i in range(2)]
  122.     options = {
  123.         'x': x,
  124.         'y': y,
  125.         'width': width,
  126.         'height': height,
  127.         'text': ''.join(choice('qwrtypdfghjkzxvbnm') for _ in range(16)),
  128.         'font': choice(font_list),
  129.         'command': choice('copy cut paste exit None'.split()),
  130.         'image': choice('gif jpg png None'.split()),
  131.         'relief': choice(relief_list),
  132.         'state': choice(state_list),
  133.         'justify': choice(justify_list),
  134.         'anchor': choice(anchor_list),
  135.         'fg': '#000000',
  136.         'bg': '#ffffff',
  137.         'padx': '',
  138.         'pady': '',
  139.         'wrap': '',
  140.         'wraplength': ''
  141.     }
  142.  
  143.     focused = focused_widget.get()
  144.     try:
  145.         widget_keys = eval(focused+'().config().keys()')
  146.     except:
  147.         widget_keys = eval(focused+'(root).config().keys()')
  148.  
  149.     for xy in 'xy':
  150.         option_widget = options_label.grid_slaves(row=primary_options_list.index(xy)+1, column=1)[0]
  151.         option_widget.config(state='normal')
  152.         option_widget.delete(0, END)
  153.         option_widget.insert(0, eval(xy))
  154.  
  155.     for option in options:
  156.         option_widget = menu_widget[option]
  157.         try:
  158.             menu_sVar[option].set('...')
  159.         except:
  160.             0
  161.         if option in widget_keys:
  162.             if option in ('font'):
  163.                 menu_widget['size']['state'] = 'normal'
  164.                 menu_sVar['size'].set(choice(size_options))
  165.                 menu_widget['style']['state'] = 'normal'
  166.                 menu_sVar['style'].set(choice(font_styles))
  167.             try:
  168.                 menu_sVar[option].set(options[option])
  169.             except:
  170.                 0
  171.             try:
  172.                 option_label[option].config(state='normal')
  173.             except:
  174.                 0
  175.             try:
  176.                 option_widget.config(state='normal')
  177.                 option_widget.insert(0, options[option])
  178.             except:
  179.                 0
  180.             try:
  181.                 option_widget.set(options[option])
  182.             except:
  183.                 0
  184.         else:
  185.             if option in ('font'):
  186.                 menu_widget['size']['state'] = 'normal'
  187.                 menu_sVar['size'].set('...')
  188.                 menu_widget['style']['state'] = 'normal'
  189.                 menu_sVar['style'].set('...')
  190.  
  191. r = -1
  192. def rrr():
  193.     global r
  194.     r += 1
  195.     return r
  196.  
  197. option_label = {}
  198. menu_sVar = {}
  199. menu_widget = {}
  200.  
  201. font_list = list(font.families())
  202.  
  203. widget_list = [feature for feature in dir(tk) if feature.lower() not in ('image', 'tk', 'ttk')]
  204. widget_list = [feature for feature in widget_list if hasattr(eval(f"tk.{feature}"), 'config')]
  205. widget_list.pop()
  206.  
  207. primary_options_list = '''
  208. x y width height text font image relief state anchor justify padx pady wrap wraplength fg bg command
  209. '''.strip().split()
  210.  
  211. state_list = '''
  212. normal active disabled
  213. '''.strip().split()
  214.  
  215. justify_list = '''
  216. center right left
  217. '''.strip().split()
  218.  
  219. anchor_list = '''
  220. N NE E SE S SW W NW
  221. '''.strip().split()
  222.  
  223. relief_list = '''
  224. FLAT RAISED SUNKEN GROOVE RIDGE
  225. '''.strip().split()
  226.  
  227. primary_options_list = '''
  228. x y width height text font image relief state anchor justify padx pady wrap wraplength fg bg command
  229. '''.strip().split()
  230.  
  231. size_options = list(range(6, 400))
  232.  
  233. font_styles = list(itertools.product(['', 'bold'], ['', 'roman', 'italic']))
  234. font_styles = [f"{weight} {slant}".strip() for weight, slant in font_styles]
  235.  
  236. options_label = LabelFrame(root, text="tk WutYewCie")
  237. options_label.grid(padx=3, pady=5)
  238.  
  239. categories = [
  240.     "Tkinter",
  241.     "Logic",
  242.     "Loops",
  243.     "Math",
  244.     "Text",
  245.     "Lists",
  246.     "Variables",
  247.     "Functions",
  248.     "Events",
  249.     "Controls"
  250. ]
  251.  
  252. def update_focused_listbox(category):
  253.     oMenu(options_label, focused_widget, category, randomly_create_data)
  254.     randomly_create_data()
  255.  
  256. focused_widget = StringVar(options_label)
  257. focused_widget_menu = oMenu(options_label, focused_widget, widget_list, randomly_create_data)
  258. focused_widget_menu.grid(row=rrr(), column=1, sticky=E)
  259. focused_widget.set('Canvas')
  260.    
  261. category_widget = StringVar(options_label)
  262. category_widget_menu = oMenu(options_label, category_widget, categories, randomly_create_data)
  263. category_widget_menu.grid(row=r, column=0, sticky=E)
  264. category_widget.set('Tkinter')
  265.  
  266. for option in primary_options_list:
  267.     option_label[option] = Label(options_label, text=option, fg='red')
  268.     option_label[option].grid(row=rrr(), column=0, sticky=E)
  269.     if option == 'font':
  270.         option_label[option].grid(sticky=W)
  271.         menu_sVar[option] = StringVar(options_label)
  272.         menu_widget[option] = xMenu(options_label, menu_sVar[option], font_list)
  273.         menu_widget[option].grid(row=r, column=0, columnspan=2, sticky=E, padx=(24, 0))
  274.         menu_sVar['size'] = StringVar(options_label)
  275.         menu_widget['size'] = xMenu(options_label, menu_sVar['size'], size_options)
  276.         menu_widget['size'].grid(row=rrr(), column=0, columnspan=2, sticky=W)
  277.         menu_sVar['style'] = StringVar(options_label)
  278.         menu_widget['style'] = oMenu(options_label, menu_sVar['style'], font_styles)
  279.         menu_widget['style'].grid(row=r, column=1, columnspan=2, sticky=E)
  280.     elif option == 'relief':
  281.         menu_sVar[option] = StringVar(options_label)
  282.         menu_widget[option] = oMenu(options_label, menu_sVar[option], relief_list)
  283.         menu_widget[option].grid(row=r, column=1, sticky=E)
  284.     elif option == 'state':
  285.         menu_sVar[option] = StringVar(options_label)
  286.         menu_widget[option] = oMenu(options_label, menu_sVar[option], state_list)
  287.         menu_widget[option].grid(row=r, column=1, sticky=E)
  288.     elif option == 'justify':
  289.         menu_sVar[option] = StringVar(options_label)
  290.         menu_widget[option] = oMenu(options_label, menu_sVar[option], justify_list)
  291.         menu_widget[option].grid(row=r, column=1, sticky=E)
  292.     elif option == 'anchor':
  293.         menu_sVar[option] = StringVar(options_label)
  294.         menu_widget[option] = oMenu(options_label, menu_sVar[option], anchor_list)
  295.         menu_widget[option].grid(row=r, column=1, sticky=E)
  296.     elif option in ['fg', 'bg']:
  297.         menu_widget[option] = Entry(options_label, textvariable=option)
  298.         menu_widget[option].grid(row=r, column=1, padx=2, sticky=W)
  299.         canvas_btn = Canvas(options_label, width=72, height=20)
  300.         canvas_btn.grid(row=r, column=1, padx=2, sticky=E)
  301.         canvas_btn.create_rectangle(0, 0, 72, 20, fill='gray', tag='rectangle')
  302.         canvas_btn.create_text(36, 10, text="Pick Color", fill="white", tag='text', anchor='center')
  303.         canvas_btn.bind('<Button-1>', lambda event, widget=menu_widget[option]: pick_color(widget))
  304.     else:
  305.         menu_widget[option] = Entry(options_label)
  306.         menu_widget[option].grid(row=r, column=1, padx=2, sticky='ew')
  307.  
  308. xm, ym = 0, 0
  309. def on_pressed(event):
  310.     global xm, ym
  311.     xm, ym = event.x, event.y
  312.  
  313. def on_motion(event):
  314.     x = root.winfo_x() - xm + event.x
  315.     y = root.winfo_y() - ym + event.y
  316.     root.geometry(f"+{x}+{y}")
  317.        
  318. apply_button = Button(options_label, text="Apply", command=apply_syntax)
  319. apply_button.grid(row=rrr(), column=0, sticky=W)
  320.  
  321. exit_button = Button(options_label, text="X", font="Verdana 9 bold", bg='red', fg='white', command=lambda: root.destroy())
  322. exit_button.grid(row=r, column=1, sticky=E)
  323.  
  324. root.bind("<ButtonPress-1>", on_pressed)
  325. root.bind("<B1-Motion>", on_motion)
  326.  
  327. randomly_create_data()
  328. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement