Wie würdest Du dann das machen?Sirius3 hat geschrieben:@Alfons Mittelmeyer: Du hast es noch nicht ganz erfasst: Deine Wörterbuchklasse ist ganz und gar überflüssig. Definition der Formularelemente und Inhalt sind zwei verschiedene Dinge und gehören nicht in einer Klasse gemischt. Die Definition kannst Du noch um mehrere Sprachen, Anzeigeoptionen und Validierung erweitern, trotzdem ist das kein Wörterbuch und erst recht keins, wofür eine eigene Klasse nötig wäre.
Code: Alles auswählen
# -*- coding: utf-8 -*-
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
class ConfigAdvanced:
KEYS = [
"tags",
"title",
"geometry",
'minsize',
'maxsize',
'resizable',
"from", # Spinbox (decimal default 0.0)
"to", # Spinbox, Scale (decimal Spinbox default 0,0, Scale default 100.0)
"increment", # Spinbox, (decimal default 1.0)
"resolution", # Scale (decimal default 1.0)
"bigincrement", # Scale (decimal default 0.0)
"showvalue",
"tickinterval", # Scale (decimal default 0.0)
"digits", # Scale (Integer default 0)
"orient",
"label",
"text",
"underline",
"myclass",
"baseclass",
"call Code(self)",
"link",
"photoimage",
"activephotoimage",
"disabledphotoimage",
'bitmap',
'activebitmap',
'disabledbitmap',
"compound",
"type",
"selectmode",
"state",
"default",
"relief",
"sliderrelief",
"overrelief",
"buttondownrelief",
'style',
'start',
'extent',
'arrow',
'arrowshape',
'capstyle',
'joinstyle',
'smooth',
'splinesteps',
"width",
"height",
"outline",
"dash",
"dashoffset",
"outlinestipple",
"outlineoffset",
"fill",
"stipple",
"offset",
"activewidth",
"activeoutline",
"activeoutlineoffset",
"activedash",
"activeoutlinestipple",
"activefill",
"activestipple",
"disabledwidth",
"disabledoutline",
"disabledoutlineoffset",
"disableddash",
"disabledoutlinestipple",
"disabledfill",
"disabledstipple",
"length",
"sliderlength",
"wraplength",
"padx", # often (Label: Integer default 0, Button ? default 3m)
"pady", # often (Label: Integer default 0, Button ? default 1m)
"bd",
"anchor",
"justify",
"font",
"fg",
"bg",
"foreground",
"background",
"troughcolor",
"selectforeground",
"selectbackground",
"insertwidth",
"insertborderwidth",
"insertontime",
"selectborderwidth",
"activeforeground",
"activebackground",
"disabledforeground",
"disabledbackground",
"highlightcolor",
"highlightbackground",
"highlightthickness",
"sashrelief",
"sashwidth",
"sashpad",
"opaqueresize",
"sashcursor",
"showhandle",
"handlesize",
"handlepad",
"takefocus",
"cursor" ]
def __init__(self,extern_dictionary):
self.extern_dictionary = extern_dictionary
def get_dictionary(self):
dictionary = {}
for key in self.extern_dictionary.config():
dictionary[key] = self.extern_dictionary[key]
return dictionary
def config(self,**kwargs):
if kwargs:
self.extern_dictionary.config(**kwargs)
return self.get_dictionary()
def __getitem__(self,key):
return self.config()[key]
def __setitem__(self,key,value):
try:
self.config(**{key : value})
except tk.TclError:
pass
def keys(self):
keys = []
dictionary = dict(self.get_dictionary())
for key in KEYS:
if key in dictionary:
keys.append(key)
dictionary.pop(key,None)
for key in dictionary:
keys.append(key)
return keys
class Application(tk.Tk):
def __init__(self,**kwargs):
tk.Tk.__init__(self,**kwargs)
# widget definitions ===================================
self.Eingabemaske = Eingabemaske(self) # basisklasse.Eingabemaske
self.Eingabemaske.pack(fill='x',expand =1)
self.Eingabemaske.show_config(tk.Entry())
class Eingabemaske(tk.LabelFrame):
def __init__(self,master,**kwargs):
tk.LabelFrame.__init__(self,master,**kwargs)
self.config(text='Eingabemaske')
# code =================================================
def show_config(self,widget):
class Code_Eingabemaske:
def __init__(self,container):
self.container = container
def create_Eingabemaske(self,input_object):
self.input_object = input_object # for OK button
keys = input_object.keys()
maxlen = max(map(len, keys))
self.content = tk.Frame(self.container)
self.content.pack(fill = 'x' , expand = 1)
def set_value(event,me,widget,key):
widget[key] = me.get()
for key in keys:
frame = tk.Frame(self.content)
frame.pack(fill='x',expand = 1)
label = tk.Label(frame,text = key, width = maxlen+1,anchor = 'e')
label.pack(side = 'left')
width = 30
expand = 0
fill = 'x'
# ===========================================
entry = tk.Entry(frame,width = width)
entry.delete(0,'end')
entry.insert(0,str(input_object[key]))
entry.pack(side = 'left',fill = fill,expand= expand,padx = 5,pady=1)
entry.bind('<Return>', lambda event, entry = entry, widget = ConfigAdvanced(entry), key = key: set_value(event,entry,widget,key))
Code_Eingabemaske(self).create_Eingabemaske(widget)
if __name__ == '__main__':
Application().mainloop()