edit_Dialog.py


directory : D:\2018_py_proj\TkGridGUI\tkgridgui
Line Count : 224
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 future import standard_library 7 standard_library.install_aliases() 8 from builtins import str 9 from builtins import object 10 import sys 11 12 if sys.version_info < (3,): 13 from future import standard_library 14 standard_library.install_aliases() 15 from tkSimpleDialog import Dialog 16 else: 17 from tkinter.simpledialog import Dialog 18 19 from tkinter import * 20 import tkinter.messagebox 21 import tkinter.colorchooser 22 from tkinter import Button, Canvas, Checkbutton, Entry, Frame, Label, LabelFrame 23 from tkinter import Listbox, Message, Radiobutton, Spinbox, Text 24 from tkinter import OptionMenu # ttk OptionMenu seems to be broken 25 from tkinter.ttk import Combobox, Progressbar, Separator, Treeview, Style, Notebook 26 27 28 from tkgridgui.tkfontchooser import askfont 29 from tkgridgui.edit_options import WidgetPropertyDefinitionsD, get_definition_optionsL 30 31 class _Dialog(Dialog): 32 # use dialogOptionsD dictionary to set any values in the dialog 33 def __init__(self, parent, title = None, dialogOptionsD=None): 34 35 self.dialogOptionsD = dialogOptionsD 36 self.my_title = title 37 Dialog.__init__(self, parent, title) 38 39 OMIT_ATTR = ('row_weights','col_weights', 'tab_labels', 'child_widget_list') # private attr. 40 41 class Edit_Properties_Dialog(_Dialog): 42 43 def body(self, master): 44 dialogframe = Frame(master, width=439, height=428) 45 dialogframe.pack() 46 47 48 self.Delete_Checkbutton = Checkbutton(dialogframe,text="Check Here to Delete Widget", width="15") 49 50 self.Delete_Checkbutton.grid(row=0, column=1, columnspan=3, sticky=W+E+N+S) 51 52 self.Delete_Checkbutton_StringVar = StringVar() 53 self.Delete_Checkbutton_StringVar.set("no") 54 self.Delete_Checkbutton.configure(variable=self.Delete_Checkbutton_StringVar, onvalue="yes", offvalue="no") 55 56 self.name_labelL = [] 57 self.val_entryL = [] 58 self.val_strvarL = [] 59 self.def_labelL = [] 60 self.cboxD = {} # index=row: value=Combobox (if any) 61 self.cbox_svarD = {}# index=Combobox Widget Object: value=Combobox StringVar (if any) 62 self.val_crossrefD = {}# index=Combobox Widget Object: value=index of val_strvarL 63 64 keyL = sorted( self.dialogOptionsD.keys() ) 65 66 keyL = [k for k in keyL if k not in OMIT_ATTR] 67 68 for i,key in enumerate( keyL ): 69 row = 3 + i 70 val, desc = self.dialogOptionsD[ key ] 71 72 self.name_labelL.append( Label(dialogframe,text=key) ) 73 74 self.val_entryL.append( Entry(dialogframe) ) 75 self.val_strvarL.append( StringVar() ) 76 self.val_entryL[-1].configure(textvariable=self.val_strvarL[-1]) 77 self.val_strvarL[-1].set( val ) 78 79 self.def_labelL.append( Label(dialogframe,text=desc) ) 80 81 82 self.name_labelL[-1].grid(row=row, column=1, sticky=E) 83 self.val_entryL[-1].grid(row=row, column=2) 84 85 if key.lower().endswith("ground"): # i.e. a color 86 btn = Button(dialogframe, text="Color", command=lambda N=i: self.get_color( N ) ) 87 btn.grid(row=row, column=0, sticky=E) 88 89 elif key.lower()=="font": 90 btn = Button(dialogframe, text="Font ", command=lambda N=i: self.get_font( N ) ) 91 btn.grid(row=row, column=0, sticky=E) 92 93 self.def_labelL[-1].grid(row=row, column=3, sticky=W) 94 95 if key in WidgetPropertyDefinitionsD: 96 long_def_label = Label(dialogframe,text=WidgetPropertyDefinitionsD[key]) 97 long_def_label.grid(row=row, column=4, sticky=W) 98 99 optionsL = get_definition_optionsL( key ) 100 if optionsL: 101 svar = StringVar() 102 self.cboxD[i] = Combobox(dialogframe, values=optionsL, width=7, textvariable=svar) 103 # validatecommand=lambda N=i: self.get_option(N)) 104 self.cboxD[i].grid(row=row, column=0, sticky=E) 105 106 self.cbox_svarD[ self.cboxD[i] ] = svar 107 self.cbox_svarD[ self.cboxD[i] ].set( val ) 108 self.val_crossrefD[ self.cboxD[i] ] = i 109 110 self.cboxD[i].bind("<<ComboboxSelected>>", self.get_option ) 111 112 #self.resizable(0,0) # Linux may not respect this 113 114 def get_option(self, event): 115 i = self.val_crossrefD[ event.widget ] 116 #print('get_option for %i ='%i , self.cbox_svarD[ event.widget ].get() ) 117 self.val_strvarL[i].set( self.cbox_svarD[ event.widget ].get() ) 118 119 def get_font(self, N): 120 font = askfont(self) 121 if font: 122 # spaces in the family name need to be escaped 123 font['family'] = font['family'].replace(' ', '\ ') 124 font_str = "%(family)s %(size)i %(weight)s %(slant)s" % font 125 if font['underline']: 126 font_str += ' underline' 127 if font['overstrike']: 128 font_str += ' overstrike' 129 130 self.val_strvarL[N].set( font_str ) 131 132 133 def get_color(self, N): 134 ctup,cstr = tkinter.colorchooser.askcolor(title='Selected Color') 135 if cstr: 136 self.val_strvarL[N].set( cstr ) 137 138 139 def validate(self): 140 """Return ONLY changes made to default values""" 141 self.result = {} # return a dictionary of results 142 143 # check for delete command 144 if self.Delete_Checkbutton_StringVar.get() == "yes": 145 146 msg = "Do you really want to delete %s?"%self.my_title 147 148 child_widget_list = self.dialogOptionsD.get('child_widget_list',[]) 149 if len(child_widget_list) > 0: 150 sL = [ '\n\n%s contains %i other widgets\n\n'%(self.my_title, len(child_widget_list)) ] 151 152 for name in child_widget_list: 153 sL.append( '%s\n'%name ) 154 msg = msg + ''.join(sL) 155 156 really = tkinter.messagebox.askyesno( "Delete %s ?"%self.my_title, msg ) 157 #print("really = ", really) 158 if really: 159 self.result["DeleteWidget"] = "yes" 160 161 #self.name_labelL, self.val_entryL, self.val_strvarL, self.def_labelL, 162 keyL = sorted( self.dialogOptionsD.keys() ) 163 164 keyL = [k for k in keyL if k not in OMIT_ATTR] 165 166 for i,key in enumerate( keyL ): 167 168 val, desc = self.dialogOptionsD[ key ] 169 if str(val) != str(self.val_strvarL[i].get()): 170 self.result[key] = str(self.val_strvarL[i].get()) 171 172 return 1 173 174 def apply(self): 175 #print( 'apply called in edit_Dialog' ) 176 pass 177 178 test_propsD = {"background" : ("" ,"The background color"), 179 "borderwidth" : ("" ,"The size of the border in pixels. usually 1 or 2 pixels."), 180 "cursor" : ("" ,"The shape of the cursor when the cursor is over the widget."), 181 "font" : ("" ,"The font used for text on the widget."), 182 "foreground" : ("" ,"Color to use for text and bitmap content"), 183 "from_" : ("" ,"Constrain the values to a numeric range. For example, from_=-10 and to=10"), 184 "height" : ("" ,"Height in pixels or text lines"), 185 "image" : ("" ,"Image to display (requires tk.PhotoImage)"), 186 "justify" : ("" ,"Align multiple lines of text. LEFT, RIGHT, CENTER"), 187 "label" : ("" ,"An optional text label"), 188 "length" : ("" ,"Number of pixels for the x dimension if the scale is horizontal, or the y dimension if vertical"), 189 "orient" : ("" ,"HORIZONTAL or VERTICAL"), 190 "overrelief" : ("" ,"Relief to use when the mouse is over the widget SUNKEN RAISED GROOVE RIDGE FLAT"), 191 "padx" : ("" ,"Additional padding left and right of the text."), 192 "pady" : ("" ,"Additional padding above and below the text."), 193 "relief" : ("" ,"Border decoration. SUNKEN RAISED GROOVE RIDGE FLAT"), 194 "state" : ("" ,"NORMAL, ACTIVE or DISABLED"), 195 "style" : ("" ,"TTK style"), 196 "text" : ("" ,"Text displayed on the widget."), 197 "to" : ("" ,"Value that defines one end of the scale's range"), 198 "value" : ("" ,"The initial value of the widget's variable"), 199 "width" : ("" ,"Width in pixels or characters")} 200 201 class _Testdialog(object): 202 def __init__(self, master): 203 frame = Frame(master, width=300, height=300) 204 frame.pack() 205 self.master = master 206 self.x, self.y, self.w, self.h = -1,-1,-1,-1 207 208 self.Button_1 = Button(text="Test Dialog", relief="raised", width="15") 209 self.Button_1.place(x=84, y=36) 210 self.Button_1.bind("<ButtonRelease-1>", self.Button_1_Click) 211 212 def Button_1_Click(self, event): #click method for component ID=1 213 dialog = Edit_Properties_Dialog(self.master, "Test Dialog", dialogOptionsD=test_propsD) 214 print( '===============Result from Dialog====================' ) 215 print( dialog.result ) 216 print( '=====================================================' ) 217 218 def main(): 219 root = Tk() 220 app = _Testdialog(root) 221 root.mainloop() 222 223 if __name__ == '__main__': 224 main() 225