Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_WUTYEWCIE.py
- '''
- About: Tkinter WUTYEWCIE - A Community Project For You To Make Python Scripts Your Way
- '''
- 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.geometry(f"+{100}+{5}")
- root.resizable(False, False)
- root.overrideredirect(True)
- root.attributes('-topmost', True)
- def pick_color(color_var):
- color = colorchooser.askcolor()[1]
- if color:
- color_var.delete(0, END)
- color_var.insert(0, 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):
- btn = Button(label, textvariable=v1, relief=FLAT, command=lambda: xMenu2(label, v1, v2))
- btn.config(bg=label.cget("bg"), fg=label.cget("fg"))
- btn.grid(row=rrr(), column=0, sticky=E)
- return btn
- def xMenu2(label, v1, v2):
- geom = root.geometry()
- x = label.winfo_rootx()
- menu_window = Toplevel(root)
- 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()
- listbox = Listbox(listbox_frame, height=42)
- listbox.grid(row=0, column=0, sticky=tk.NSEW)
- scrollbar = Scrollbar(listbox_frame, orient=tk.VERTICAL)
- scrollbar.grid(row=0, column=1, sticky=tk.NS)
- listbox.config(yscrollcommand=scrollbar.set)
- scrollbar.config(command=listbox.yview)
- for item in v2:
- listbox.insert(END, item)
- def on_motion(event):
- root.attributes('-topmost', True)
- menu_window.destroy()
- def on_select(event):
- focused = listbox.get(listbox.curselection())
- v1.set(focused)
- menu_window.destroy()
- listbox_frame.bind("<Leave>", on_motion)
- listbox.bind('<<ListboxSelect>>', on_select)
- listbox.config(width=30)
- menu_window.geometry(f"+{x}+{10}")
- 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():
- focused = focused_widget.get()
- options = []
- if focused:
- for option in primary_options_list:
- option_widget = menu_widget[option]
- if option_label[option].cget('state') != 'disabled':
- if isinstance(option_widget, ttk.OptionMenu):
- var = menu_sVar[option].get()
- else:
- var = option_widget.get()
- if var:
- options.append(f"{option}={var}")
- syntax = f"{focused}(root, " + ", ".join(options) + ")"
- print(syntax)
- def randomly_create_data(e=None):
- toggle_widgets(root, "", 'disabled')
- category_widget_menu.config(state='normal')
- focused_widget_menu.config(state='normal')
- apply_button.config(state='normal')
- exit_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': ''
- }
- focused = focused_widget.get()
- try:
- widget_keys = eval(focused+'().config().keys()')
- except:
- widget_keys = eval(focused+'(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="tk WutYewCie")
- options_label.grid(padx=3, pady=5)
- categories = [
- "Tkinter",
- "Logic",
- "Loops",
- "Math",
- "Text",
- "Lists",
- "Variables",
- "Functions",
- "Events",
- "Controls"
- ]
- def update_focused_listbox(category):
- oMenu(options_label, focused_widget, category, randomly_create_data)
- randomly_create_data()
- focused_widget = StringVar(options_label)
- focused_widget_menu = oMenu(options_label, focused_widget, widget_list, randomly_create_data)
- focused_widget_menu.grid(row=rrr(), column=1, sticky=E)
- focused_widget.set('Canvas')
- category_widget = StringVar(options_label)
- category_widget_menu = oMenu(options_label, category_widget, categories, randomly_create_data)
- category_widget_menu.grid(row=r, column=0, sticky=E)
- category_widget.set('Tkinter')
- 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] = xMenu(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'] = xMenu(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, widget=menu_widget[option]: pick_color(widget))
- 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=W)
- exit_button = Button(options_label, text="X", font="Verdana 9 bold", bg='red', fg='white', command=lambda: root.destroy())
- exit_button.grid(row=r, column=1, sticky=E)
- 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