Hiding and unhiding frames and/or widgets in general
First, I recall now that I neglected to thank everyone for their help, concerning my last question. The last two months have been extraordinarily busy, although that's a poor excuse for bad manners, so thank you all, belatedly, for your assistance. I am back again, with another (possibly silly) question regarding hiding and unhiding windows. I notice that toplevel windows have the ability to be "withdrawn" or "deiconified" in order to be hidden from view. My widgets are not toplevel widgets, so I'm unclear about whether or not I can use these methods or not. Either way, I am trying (unsuccessfully) to accomplish something similar, in that I would like to destroy or withdraw an entryfield widget if a comboBox is displayed, and visa versa. Also, if neither an entryfield or a comboBox is displayed, nothing should be displayed in the same spot. The following code illustrates, to some degree, what I'm trying to accomplish. But you'll note that the code has other problems as well: import Pmw from Tkinter import * root = Tk() Pmw.initialise(root) class Stuff: def __init__(self, root): self.root = root self.mainframe = Frame(self.root) self.mainframe.pack(fill='both', expand=1, padx=5, pady=5) self.choices = [ '', 'Select the entryField...', 'Select the entryField again...', 'Select the entryField yet again...', ] self.choices2 = [ '', 'Select the comboBox...', 'Select the comboBox again...', 'Select the comboBox yet again...', ] self.stuffList = [ '', 'item 1', 'item 2', 'item 3', 'item 4', 'item 5', 'item 6', 'item 7', 'item 8', 'item 9', 'item 10', ] self.cBox = Pmw.ComboBox( self.mainframe, listheight=225, selectioncommand=self.createEntryField, entry_width=28, labelmargin=1, labelpos='w', label_text='Select stuff:', scrolledlist_items=self.choices+self.choices2) self.cBox.pack() self.eFrame = Frame(self.mainframe, borderwidth=0, width=200, height=22, relief=GROOVE) self.eField = Pmw.EntryField( self.eFrame, labelpos=W, label_text='Enter Stuff:', labelmargin=2, entry_width=12, validate = None, ) self.eField.pack() self.cFrame = Frame(self.mainframe, borderwidth=0, width=200, height=22, relief=GROOVE) self.cBox2 = Pmw.ComboBox( self.cFrame, listheight=165, entry_width=12, labelmargin=9, labelpos='w', label_text='Stuff:', scrolledlist_items=self.stuffList ) self.cBox2.pack() def createEntryField(self, hello): for item in self.choices: if self.cBox.component('entryfield').component('entry').get() == item: self.cFrame.lower() self.eFrame.lift() self.eFrame.place(relx=0.7, rely=0.08, anchor=NW) else: self.eFrame.lower() self.cFrame.lift() self.cFrame.place(relx=0.7, rely=0.08, anchor=NW) if __name__=='__main__': stuff = Stuff(root) root.mainloop() Any ideas? Thanking you in advance, Christopher Nethery
If you are using "pack" to manage your widgets, then "pack_forget" will make the widget disappear. Similarly, there are "grid_forget" and "place_forget" methods. When a widget is packed or unpacked, the size of its containing frame may change, and that propogates up in the widget hierarchy to the toplevel. Having the window change size when a field appears or disappears is probably going to be jarring to the eye. You can "fix" this by turning off "geometry propagation" in the frame that contains the changing widget. This is done by the grid_propagate or pack_propagate method of the *container* object. from Tkinter import * def size_for_alternatives(parent, kids): w = max([k.winfo_reqwidth() for k in kids]) h = max([k.winfo_reqheight() for k in kids]) print ([k.winfo_reqwidth() for k in kids]), w print ([k.winfo_reqheight() for k in kids]), h parent.pack_propagate(0) parent.grid_propagate(0) parent.configure(width=w, height=h) def swap(): if v.get(): e.pack_forget() mb.pack(anchor="w", side="left") else: mb.pack_forget() e.pack(anchor="w", side="left") e.focus() t = Tk() v = IntVar(t) c = Checkbutton(t, command=swap, text="Order from menu?", variable=v) c.pack(anchor="w") f1 = Frame(t) l = Label(f1, text="Your order:") l.pack(side="left") f = Frame(f1) f.pack(side="left") e = Entry(f, width=18) mb = Menubutton(f, width=8, text="eggs", indicatoron=1, relief="sunken", anchor="w") m = Menu(mb, tearoff=0); mb.configure(menu=m) for s in "eggs bacon sausage spam".split(): m.add_command(label=s, command=lambda s=s: mb.configure(text=s)) f.after_idle(lambda: size_for_alternatives(f, (e, mb))) f.pack() f1.pack() swap() b = Button(t, text="Place order", command=t.destroy); b.pack(side="top") f.mainloop()
participants (2)
-
Chris Nethery -
Jeff Epler