Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # widget_options.py
- '''
- still needs a whole lot more work...
- '''
- import tkinter
- tk_features = []
- for feature in dir(tkinter):
- tk_features += [feature]
- from tkinter import *
- from tkinter import ttk
- from random import *
- rnd_letters = 'qwrtypdfghjkzxvbnm'
- root = Tk()
- root.title("Widget Demo")
- root.resizable(False, False)
- # create a list of just the options of each selected widget
- # in which the user can intuitively change the options
- r = -1
- def rrr():
- global r
- r += 1
- return r
- class Cv(): 0
- cv = Cv()
- # Tk_list_all_widget_options.py
- widget_list = []
- for feature in tk_features:
- if feature.lower() not in ('image', 'tk'):
- try:
- options = eval(feature+'().config().keys()')
- widget_list += [feature]
- except:
- try:
- options = eval(feature+'(root).config().keys()')
- widget_list += [feature]
- except:
- 0
- print('\n\n')
- for widget in widget_list:
- print(widget)
- print('\n\n')
- # considering this GUI is in a place() format
- primary_options_list = '''
- x y width height text font command image relief state anchor justify
- '''.strip().split()
- font_list = '''
- Arial, Verdana, Courier, Times, New Roman, Comic Sans, MS Sans Serif, MS Serif, Symbol, System, Fixedsys
- '''.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()
- def init():
- def refresh(event): # hi-jacked for demo
- # if selected_widget_menu has changed then randomly_create_data()
- if event in widget_list:
- randomly_create_data()
- # update secondary_options_label <<<<<
- options_label = LabelFrame(root, text="Selected Widget Options")
- options_label.grid(padx=3, pady=5)
- # create a drop-down menu to select the widget
- cv.selected_widget = StringVar(options_label)
- cv.selected_widget.set(widget_list[0])
- widget_list2 = [widget_list[0]]+widget_list
- selected_widget_menu = ttk.OptionMenu(options_label, cv.selected_widget, *widget_list2, command=refresh)
- selected_widget_menu.grid(row=rrr(), column=1, sticky=E)
- # display only primary options in the given order
- for option in primary_options_list:
- option_label = Label(options_label, text=option, fg='red')
- option_label.grid(row=rrr(), column=0, sticky=E)
- # if option has sub-option, then place a drop-menu of those
- if option == 'font':
- cv.font_option = StringVar(options_label)
- cv.font_option.set(font_list[0])
- font_list2 = [font_list[0]]+font_list
- font_option_menu = ttk.OptionMenu(options_label, cv.font_option, *font_list2, command=refresh)
- font_option_menu.grid(row=r, column=1, sticky=E)
- elif option == 'relief':
- cv.relief_option = StringVar(options_label)
- cv.relief_option.set(relief_list[0])
- relief_list2 = [relief_list[0]]+relief_list
- relief_option_menu = ttk.OptionMenu(options_label, cv.relief_option, *relief_list2, command=refresh)
- relief_option_menu.grid(row=r, column=1, sticky=E)
- elif option == 'state':
- cv.state_option = StringVar(options_label)
- cv.state_option.set(state_list[0])
- state_list2 = [state_list[0]]+state_list
- state_option_menu = ttk.OptionMenu(options_label, cv.state_option, *state_list2, command=refresh)
- state_option_menu.grid(row=r, column=1, sticky=E)
- elif option == 'justify':
- cv.justify_option = StringVar(options_label)
- cv.justify_option.set(justify_list[0])
- justify_list2 = [justify_list[0]]+justify_list
- justify_option_menu = ttk.OptionMenu(options_label,cv. justify_option, *justify_list2, command=refresh)
- justify_option_menu.grid(row=r, column=1, sticky=E)
- elif option == 'anchor':
- cv.anchor_option = StringVar(options_label)
- cv.anchor_option.set(anchor_list[0])
- anchor_list2 = [anchor_list[0]]+anchor_list
- anchor_option_menu = ttk.OptionMenu(options_label, cv.anchor_option, *anchor_list2, command=refresh)
- anchor_option_menu.grid(row=r, column=1, sticky=E)
- else:
- option_entry = Entry(options_label)
- option_entry.config(state='disabled')
- option_entry.grid(row=r, column=1, padx=2)
- # display only secondary options in a TopLevel window
- def secondary_options_label(widget):
- r = 0
- secondary_options_label = Toplevel(root)
- secondary_options_label.title("Secondary Options")
- secondary_options_label.geometry("200x600")
- secondary_options_label.resizable(False, False)
- # loop through widget options to only display the non-primary options
- for option in widget.keys():
- if option not in primary_options_list:
- option_label = Label(secondary_options_label, text=option)
- option_label.grid(row=r, column=0)
- option_entry = Entry(secondary_options_label)
- option_entry.grid(row=r, column=1)
- option_entry.config(state='disabled')
- r += 1
- ### create a demo for this in which all the options in each selected_widget is randomly filled out
- def randomly_create_data():
- x, y = [randint(1,20)*5 for i in range(2)]
- width, height = [randint(1,20)*5 for i in range(2)]
- text = ''.join([choice(rnd_letters) for i in range(10)])
- 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)
- options = {
- 'width': width,
- 'height': height,
- 'text': text,
- 'font': font,
- 'command': command,
- 'image': image,
- 'relief': relief,
- 'state': state,
- 'justify': justify,
- 'anchor': anchor,
- }
- # only if option in canvas options
- selected = cv.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))
- print(xy, '=', eval(xy))
- for option in options:
- # update all the corresponding options
- option_widget = options_label.grid_slaves(row=primary_options_list.index(option)+1, column=1)[0]
- option_widget_label = options_label.grid_slaves(row=primary_options_list.index(option)+1, column=0)[0]
- option_widget_label.config(state='normal')
- if option in widget_keys:
- option_widget.config(state='normal')
- print(option, '=', options[option])
- if options[option] == 'None':
- continue
- # set text in option_widget
- if 'insert' in dir(option_widget):
- option_widget.delete(0, END)
- option_widget.insert(0, options[option])
- else:
- eval('cv.'+option+'_option.set(options[option])')
- else:
- # set text in option_widget
- if 'insert' in dir(option_widget):
- option_widget.delete(0, END)
- else:
- eval('cv.'+option+'_option.set("")')
- option_widget.config(state='disabled')
- option_widget_label.config(state='disabled')
- print('')
- root.update()
- root.bind('<<ComboboxSelected>>', refresh)
- init()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement