tkinter frames problem
Jerome Quelin
jerome.quelin at insalien.org
Thu Jul 6 07:53:50 EDT 2000
walter hanagriff <walter111 at structurex.net> wrote:
>this is just a framework i am using as a simplified test case for adding
[snip]
>i think that best explains what i want, any help would be appreciated
I don't know if this is what you want, but check the code below (run it as a
standalone app, it should work):
-- File test.py
#!/usr/bin/python
from Tkinter import *
class Main:
def __init__(self, root):
# The top label.
text = 'this program figures the area, perimeter, and power of n'
text = text + '\n' + '_'*len(text)
Label(root, text=text).pack(side=TOP, fill=X)
# The 'main' panel to command the option frame to show.
self.var = StringVar()
self.var.set('power')
main = Frame(root, bd=2, relief=SUNKEN)
Radiobutton(main, text="1 = Find the power of a number", value='power',
command=self.update_frame, variable=self.var).pack(anchor=W)
Radiobutton(main, text="2 = Find the area of a shape", value='area',
command=self.update_frame, variable=self.var).pack(anchor=W)
Radiobutton(main, text="3 = Find the perimeter of a shape", value='perim',
command=self.update_frame, variable=self.var).pack(anchor=W)
Button(main, text="Close", command=root.destroy).pack(padx=10, pady=10)
main.pack(side=LEFT, padx=5, pady=5)
# Create 3 frames, one for each option.
self.frames = { 'power': Frame(root),
'area': Frame(root),
'perim': Frame(root) }
# Populate the option frames at your convenience.
prefix = 'this frame is where you put information to get '
list = (('power', 'power of n'),
('area', 'the area of a shape'),
('perim', 'the perimeter of a shape'))
for (frame_name, text) in list:
frame = self.frames[frame_name]
text = prefix + text
text = text + '\n' + '_'*len(text)
Label(frame, text=text).pack(expand=YES, fill=BOTH)
# We start with one frame.
self.update_frame()
def update_frame(self):
# Unpack every option frame.
for n in self.frames.keys(): self.frames[n].pack_forget()
# Pack the current option frame.
self.frames[self.var.get()].pack(side=RIGHT)
if __name__== '__main__':
root = Tk()
root.title('test')
start = Main(root)
root.mainloop()
-- End of file
I throw up your class Func and swept a little your code. Hope this helps ?
Regards,
Jerome
--
jerome.quelin at insalien.org
More information about the Python-list
mailing list