edit_options.py


directory : D:\2018_py_proj\TkGridGUI\tkgridgui
Line Count : 148
Line Clip Size : No Clipping

tab_of_notebook_changed  Word MatchCase (0) 
1 #!/usr/bin/env python 2 # -*- coding: ascii -*- 3 from __future__ import print_function 4 from __future__ import unicode_literals 5 6 from builtins import str 7 from builtins import range 8 from itertools import combinations 9 """ 10 Collect the basic edit options for all widgets, including definitions 11 12 Given a widget from PreviewWin, get the actual attributes from widget.keys() 13 14 Provide helper functions to set and get properties. 15 """ 16 17 WidgetPropertyDefinitionsD = {} # index=option name: value=definition 18 SortedPropertyNamesL = [] # sorted list of option names 19 20 grid_option_set = set(['sticky','columnspan','rowspan']) 21 22 def add_definition( options_str ): 23 """Take input options_str and process it into an options dictionary""" 24 strL = options_str.split('\n') 25 26 for s in strL: 27 sL = s.split('#') 28 if len(sL)==2: 29 WidgetPropertyDefinitionsD[sL[0].strip()] = sL[1].strip() 30 31 def get_definition_optionsL( attr_name ): 32 """Some attr have defined options. The definitions end with all-cap options.""" 33 if attr_name=='cursor': 34 return partial_cursorL 35 36 elif attr_name=='sticky': 37 optionsL = [] 38 for i in range(1,5): 39 cL = list(combinations('nsew', i)) 40 for c in cL: 41 optionsL.append( ''.join(c) ) 42 return optionsL 43 44 s = WidgetPropertyDefinitionsD.get( attr_name, '' ) 45 if s: 46 optionsL = [] 47 sL = reversed( s.split() ) 48 for word in sL: 49 if word.isupper(): 50 optionsL.append( word.lower() ) 51 else: 52 break 53 return optionsL 54 55 else: 56 return [] 57 58 def set_attribute_if_possible(widget, attr_name, value): 59 prop_set = set( widget.keys() ) 60 #print('prop_set = ', sorted(prop_set)) 61 if attr_name in prop_set: 62 widget[ attr_name ] = value 63 #print("Setting: ",attr_name,' to ',value) 64 65 elif attr_name in grid_option_set: 66 # ignore rowspan and columnspan if not an integer 67 if attr_name in ('rowspan','columnspan'): 68 try: 69 value = int(value) 70 except: 71 return 72 widget.grid( {attr_name:value} ) 73 74 def get_properties_dict( widget ): 75 """Returns a dictionary of name, current value (as strings)""" 76 resultD = {} 77 78 prop_set = set( widget.keys() ) 79 for name in SortedPropertyNamesL: 80 if name in prop_set: 81 val = str( widget.cget( name ) ) 82 resultD[name] = val 83 return resultD 84 85 def get_properties_for_editing( widget ): 86 """Returns a sorted list of tuples (option name, current value, definition)""" 87 resultL = [] 88 89 prop_set = set( widget.keys() ) 90 for name in SortedPropertyNamesL: 91 if name in prop_set: 92 val = str( widget.cget( name ) ) 93 resultL.append( (name, val, WidgetPropertyDefinitionsD[name]) ) 94 95 return resultL 96 97 buttonStr = '''background #The background color 98 borderwidth # The size of the border in pixels. usually 1 or 2 pixels. 99 foreground #Color to use for text and bitmap content 100 image #Image to display (requires tk.PhotoImage) 101 justify #Align multiple lines of text. LEFT RIGHT CENTER 102 overrelief #Relief when mouse is over widget SUNKEN RAISED GROOVE RIDGE FLAT 103 relief #Border decoration. SUNKEN RAISED GROOVE RIDGE FLAT 104 state #Widget state NORMAL ACTIVE DISABLED''' 105 106 add_definition( buttonStr) 107 108 commonPropertiesStr = """width #Width in pixels or characters 109 height #Height in pixels or text lines 110 text #Text displayed on the widget. 111 font #The font used for text on the widget. 112 cursor #The shape of the cursor when the cursor is over the widget.""" 113 114 add_definition( commonPropertiesStr ) 115 116 specialtyPropertiesStr = """from_ #Constrain the values to a numeric range. For example, from_=-10 and to=10 117 to #Value that defines one end of the scale's range 118 value #The initial value of the widget's variable 119 length #Number of pixels for the x dimension if the scale is horizontal, or the y dimension if vertical 120 label #An optional text label 121 orient #Orientation options HORIZONTAL VERTICAL 122 padx #Additional padding left and right of the text. 123 pady #Additional padding above and below the text. 124 sticky #Justification of widget within grid cell N S E W 125 rowspan #Number of rows a widget spans (must be integer) 126 columnspan #Number of columns a widget spans (must be integer) 127 row_weights #Weight values used in rowconfigure on resize 128 col_weights #Weight values used in columnconfigure on resize 129 scrolly #Enable scrolling in y direction YES NO 130 scrollx #Enable scrolling in x direction YES NO 131 style #TTK style""" 132 133 add_definition( specialtyPropertiesStr ) 134 135 SortedPropertyNamesL = sorted( WidgetPropertyDefinitionsD.keys() ) 136 137 partial_cursorL = ["arrow","circle","clock","cross","dotbox","exchange","fleur","heart","hand1","hand2", 138 "man","mouse","pirate","plus","shuttle","sizing","spider","spraycan","star","target", 139 "tcross","trek","watch"] 140 141 if __name__ == "__main__": 142 143 print("test_propsD = {", end='') 144 for name in SortedPropertyNamesL: 145 print( ' "%s"'%name, ' '*(14-len(name)), ':', '("" ,"%s"),'%WidgetPropertyDefinitionsD[name] ) 146 147 print("}") 148