Advertisement
here2share

# widget_options.py

Aug 6th, 2022 (edited)
1,226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.11 KB | None | 0 0
  1. # widget_options.py
  2.  
  3. '''
  4. still needs a whole lot more work...
  5. '''
  6.  
  7. import tkinter
  8.  
  9. tk_features = []
  10. for feature in dir(tkinter):
  11.     tk_features += [feature]
  12.  
  13. from tkinter import *
  14. from tkinter import ttk
  15. from random import *
  16.  
  17. rnd_letters = 'qwrtypdfghjkzxvbnm'
  18.  
  19. root = Tk()
  20. root.title("Widget Demo")
  21. root.resizable(False, False)
  22.  
  23. # create a list of just the options of each selected widget
  24. # in which the user can intuitively change the options
  25.  
  26. r = -1
  27. def rrr():
  28.     global r
  29.     r += 1
  30.     return r
  31.  
  32. class Cv(): 0
  33. cv = Cv()
  34.  
  35. # Tk_list_all_widget_options.py
  36. widget_list = []
  37. for feature in tk_features:
  38.     if feature.lower() not in ('image', 'tk'):
  39.         try:
  40.             options = eval(feature+'().config().keys()')
  41.             widget_list += [feature]
  42.         except:
  43.             try:
  44.                 options = eval(feature+'(root).config().keys()')
  45.                 widget_list += [feature]
  46.             except:
  47.                 0
  48.  
  49. print('\n\n')
  50. for widget in widget_list:
  51.     print(widget)
  52. print('\n\n')
  53.  
  54. # considering this GUI is in a place() format
  55. primary_options_list = '''
  56. x y width height text font command image relief state anchor justify
  57. '''.strip().split()
  58.  
  59. font_list = '''
  60. Arial, Verdana, Courier, Times, New Roman, Comic Sans, MS Sans Serif, MS Serif, Symbol, System, Fixedsys
  61. '''.strip().split(', ')
  62.  
  63. state_list = '''
  64. normal active disabled
  65. '''.strip().split()
  66.  
  67. justify_list = '''
  68. center right left
  69. '''.strip().split()
  70.  
  71. anchor_list = '''
  72. N NE E SE S SW W NW
  73. '''.strip().split()
  74.  
  75. relief_list = '''
  76. FLAT RAISED SUNKEN GROOVE RIDGE
  77. '''.strip().split()
  78.  
  79. def init():
  80.     def refresh(event): # hi-jacked for demo
  81.         # if selected_widget_menu has changed then randomly_create_data()
  82.         if event in widget_list:
  83.             randomly_create_data()
  84.         # update secondary_options_label <<<<<
  85.        
  86.     options_label = LabelFrame(root, text="Selected Widget Options")
  87.     options_label.grid(padx=3, pady=5)
  88.  
  89.     # create a drop-down menu to select the widget
  90.     cv.selected_widget = StringVar(options_label)
  91.     cv.selected_widget.set(widget_list[0])
  92.     widget_list2 = [widget_list[0]]+widget_list
  93.     selected_widget_menu = ttk.OptionMenu(options_label, cv.selected_widget, *widget_list2, command=refresh)
  94.     selected_widget_menu.grid(row=rrr(), column=1, sticky=E)
  95.  
  96.     # display only primary options in the given order
  97.  
  98.     for option in primary_options_list:
  99.         option_label = Label(options_label, text=option, fg='red')
  100.         option_label.grid(row=rrr(), column=0, sticky=E)
  101.         # if option has sub-option, then place a drop-menu of those
  102.         if option == 'font':
  103.             cv.font_option = StringVar(options_label)
  104.             cv.font_option.set(font_list[0])
  105.             font_list2 = [font_list[0]]+font_list
  106.             font_option_menu = ttk.OptionMenu(options_label, cv.font_option, *font_list2, command=refresh)
  107.             font_option_menu.grid(row=r, column=1, sticky=E)
  108.         elif option == 'relief':
  109.             cv.relief_option = StringVar(options_label)
  110.             cv.relief_option.set(relief_list[0])
  111.             relief_list2 = [relief_list[0]]+relief_list
  112.             relief_option_menu = ttk.OptionMenu(options_label, cv.relief_option, *relief_list2, command=refresh)
  113.             relief_option_menu.grid(row=r, column=1, sticky=E)
  114.         elif option == 'state':
  115.             cv.state_option = StringVar(options_label)
  116.             cv.state_option.set(state_list[0])
  117.             state_list2 = [state_list[0]]+state_list
  118.             state_option_menu = ttk.OptionMenu(options_label, cv.state_option, *state_list2, command=refresh)
  119.             state_option_menu.grid(row=r, column=1, sticky=E)
  120.         elif option == 'justify':
  121.             cv.justify_option = StringVar(options_label)
  122.             cv.justify_option.set(justify_list[0])
  123.             justify_list2 = [justify_list[0]]+justify_list
  124.             justify_option_menu = ttk.OptionMenu(options_label,cv. justify_option, *justify_list2, command=refresh)
  125.             justify_option_menu.grid(row=r, column=1, sticky=E)
  126.         elif option == 'anchor':
  127.             cv.anchor_option = StringVar(options_label)
  128.             cv.anchor_option.set(anchor_list[0])
  129.             anchor_list2 = [anchor_list[0]]+anchor_list
  130.             anchor_option_menu = ttk.OptionMenu(options_label, cv.anchor_option, *anchor_list2, command=refresh)
  131.             anchor_option_menu.grid(row=r, column=1, sticky=E)
  132.         else:
  133.             option_entry = Entry(options_label)
  134.             option_entry.config(state='disabled')
  135.             option_entry.grid(row=r, column=1, padx=2)
  136.  
  137.     # display only secondary options in a TopLevel window
  138.     def secondary_options_label(widget):
  139.         r = 0
  140.         secondary_options_label = Toplevel(root)
  141.         secondary_options_label.title("Secondary Options")
  142.         secondary_options_label.geometry("200x600")
  143.         secondary_options_label.resizable(False, False)
  144.         # loop through widget options to only display the non-primary options
  145.         for option in widget.keys():
  146.             if option not in primary_options_list:
  147.                 option_label = Label(secondary_options_label, text=option)
  148.                 option_label.grid(row=r, column=0)
  149.                 option_entry = Entry(secondary_options_label)
  150.                 option_entry.grid(row=r, column=1)
  151.                 option_entry.config(state='disabled')
  152.                 r += 1
  153.  
  154.     ### create a demo for this in which all the options in each selected_widget is randomly filled out
  155.  
  156.     def randomly_create_data():
  157.         x, y = [randint(1,20)*5 for i in range(2)]
  158.         width, height = [randint(1,20)*5 for i in range(2)]
  159.         text = ''.join([choice(rnd_letters) for i in range(10)])
  160.         font = choice(font_list)
  161.         command = choice('copy cut paste exit None'.split())
  162.         image = choice('gif jpg png None'.split())
  163.         relief = choice(relief_list)
  164.         state = choice(state_list)
  165.         justify = choice(justify_list)
  166.         anchor = choice(anchor_list)
  167.         options = {
  168.             'width': width,
  169.             'height': height,
  170.             'text': text,
  171.             'font': font,
  172.             'command': command,
  173.             'image': image,
  174.             'relief': relief,
  175.             'state': state,
  176.             'justify': justify,
  177.             'anchor': anchor,
  178.             }
  179.  
  180.         # only if option in canvas options
  181.         selected = cv.selected_widget.get()
  182.         try:
  183.             widget_keys = eval(selected+'().config().keys()')
  184.         except:
  185.             widget_keys = eval(selected+'(root).config().keys()')
  186.         for xy in 'xy':
  187.             option_widget = options_label.grid_slaves(row=primary_options_list.index(xy)+1, column=1)[0]
  188.             option_widget.config(state='normal')
  189.             option_widget.delete(0, END)
  190.             option_widget.insert(0, eval(xy))
  191.             print(xy, '=', eval(xy))
  192.         for option in options:
  193.             # update all the corresponding options
  194.             option_widget = options_label.grid_slaves(row=primary_options_list.index(option)+1, column=1)[0]
  195.             option_widget_label = options_label.grid_slaves(row=primary_options_list.index(option)+1, column=0)[0]
  196.             option_widget_label.config(state='normal')
  197.             if option in widget_keys:
  198.                 option_widget.config(state='normal')
  199.                 print(option, '=', options[option])
  200.                 if options[option] == 'None':
  201.                     continue
  202.                 # set text in option_widget
  203.                 if 'insert' in dir(option_widget):
  204.                     option_widget.delete(0, END)
  205.                     option_widget.insert(0, options[option])
  206.                 else:
  207.                     eval('cv.'+option+'_option.set(options[option])')
  208.             else:
  209.                 # set text in option_widget
  210.                 if 'insert' in dir(option_widget):
  211.                     option_widget.delete(0, END)
  212.                 else:
  213.                     eval('cv.'+option+'_option.set("")')
  214.                 option_widget.config(state='disabled')
  215.                 option_widget_label.config(state='disabled')
  216.  
  217.         print('')
  218.        
  219.         root.update()
  220.                        
  221.     root.bind('<<ComboboxSelected>>', refresh)
  222.    
  223. init()
  224. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement