Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
131 lines (109 sloc) 4.59 KB
#!/usr/bin/env python3
# Author: Randall Nagy
# 2018/12/20: Created
# Mission: Create a Tkinter native-type, ordered, data-dictionary display / Frame.
# Release: Blogged:
from tkinter import *
from collections import OrderedDict
class FieldFrame():
''' Support a slightly more pythonic, ad-hoc way of displaying
Tkinter's 'smart variables.' Additonal masking / data type
validaiton / support as time permits.
def __init__(self, parent, type_dict, title, entry_width=60):
''' Create + initialize your FieldFrame.
parent = Tkinter window / toplevel
type_dict = Dictionary 'key' is label, value is smart-variable.
title = Title frame / Labelframe title
entry_width = Character width for all text-entry fields.
self.parent = parent
self.entry_width = entry_width = "Light Green"
self.display_info = type_dict
self.ztitle = title
self.frames = dict()
self.frames["root"] = Frame(parent,
self.frames["title"] = self.mk_title(self.frames["root"]) # Container
self.frames["buttons"] = self.mk_buttons(self.frames["title"]) # Siblings
self.frames["body"] = self.mk_body(self.frames["title"]) # Siblings
def do_pack(self):
''' An opportunity to provide your own packing logic. '''
def get_frames(self):
''' Returns a dictionary of the frames in-play. '''
return self.frames
def get_data(self):
''' Retrieve the data / smart variables used to create instance. '''
return self.display_info
def show_data(self, zdict):
''' Update the smart variables ('show') by using a more classic means. '''
for key in zdict:
def apply(self):
''' An opportunity to perform additional "okay" validation. '''
def cancel(self):
''' An opportunity to perform additional "cancel" validation. '''
def mk_title(self, zframe):
''' The title frame contains the button and body frames. '''
return LabelFrame(
text=" {} ".format(self.ztitle),
def mk_buttons(self, zframe):
''' The button frame contains the display's lifecycle buttons. '''
my_frame = Frame(zframe,
Button(my_frame, text=" {} ".format("Okay"),, command=self.apply
).grid(column=0, row=0)
Label(my_frame, text=" ",, row=0)
Button(my_frame, text=" {} ".format("Cancel"),, command=self.cancel
).grid(column=2, row=0)
return my_frame
def mk_body(self, zframe):
''' The body frame is where the data-entry options are displayed. '''
my_frame = Frame(zframe,
for ss, key in enumerate(self.display_info):
Label(my_frame, text=" {}: ".format(key),, row=ss)
Entry(my_frame, width=self.entry_width,
textvariable=self.display_info[key]).grid(column=1, row=ss)
return my_frame
def __dict__(self):
''' Expresses smart variables using a classic dictionary. '''
results = dict()
for key in self.display_info:
results[key] = self.display_info[key].get()
return results
def __iter__(self):
''' Iterate thru the presently displayed / smart variable values. '''
for key in self.display_info:
yield key, self.display_info[key].get()
def __str__(self):
''' Handy for comparisons, but returns the present, smart-variable, values. '''
results = self.__dict__()
return str(results)
if __name__ == "__main__":
zroot = Tk()
data = OrderedDict()
# The Variable Classes (BooleanVar, DoubleVar, IntVar, StringVar)
data["Name"] = StringVar()
data["User Age"] = StringVar()
data["Account Balance"] = StringVar()
zroot.zdata = FieldFrame(zroot, data, "Your Customers")
stuff = dict()
for key in zroot.zdata.get_data():
stuff[key] = "This is " + key
for line in zroot.zdata: