Mailing List Archive

Tkinter Object show and hide
Hy everyone,

Somebody can talk me if there are some option or function to hide and
show objects in Tkinter.

Example:

---- Here is a object definition ----
first_obj = Button(root, text="Button 1")
first_obj.pack()

second_obj = Button(root, text="Button 2")
second_obj.pack()
---- Finish ----

I wanna know to hide the second object for example. But I need it in
Tkinter because I'm working in Windows.

Thanks to everyone...
Mauro
Tkinter Object show and hide [ In reply to ]
"Mauro" <mauro@mr-potatohead.com> wrote in message
news:3b55ea60.0208191517.96dc162@posting.google.com...
> Hy everyone,
>
> Somebody can talk me if there are some option or function to hide and
> show objects in Tkinter.

Sure there is. You can use the pack_forget() function. Look at
http://www.pythonware.com/library/tkinter/introduction/x6487-methods.htm.
Below I have written a small program to demonstrate its usage.

>
> Example:
>
> ---- Here is a object definition ----
> first_obj = Button(root, text="Button 1")
> first_obj.pack()
>
> second_obj = Button(root, text="Button 2")
> second_obj.pack()
> ---- Finish ----
>

from Tkinter import *

class ShowHide:
def __init__(self, master):
self.master = master
self.show = 1
self.makeWidgets()

def makeWidgets(self):
self.showhidebutton = Button(self.master, text="Show/Hide",
command=self.showhide)
self.showhidebutton.pack()

self.first_obj = Button(self.master, text="Button 1")
self.first_obj.pack()

self.second_obj = Button(self.master, text="Button 2")
self.second_obj.pack()

def showhide(self):
if self.show:
self.second_obj.pack_forget()
self.show = 0
else:
self.second_obj.pack()
self.show = 1

def main():
root = Tk()
ShowHide(root)
root.mainloop()

if __name__ == '__main__':
main()



Regards

Jorgen Cederberg
Tkinter Object show and hide [ In reply to ]
In article <3b55ea60.0208191517.96dc162@posting.google.com>,
mauro@mr-potatohead.com (Mauro) wrote:

>Somebody can talk me if there are some option or function to hide and
>show objects in Tkinter...

What you do is ungrid or unpack the object to hide it, then grid or pack
it again to show it. If you want to it to reappear where it was before
then I strongly suggest using the grid geometry manager for two reasons:
- The gridder offers the grid_remove() method which remembers all your
grid options. Pack has no equivalent.
- Even if you don't take advantage of grid_remove() you can more easily
put something back where it was by specifying row and column (which does
not change) than by trying to reinsert a packed object in the right
place (which requires using the "before" or "after" option to pack().

Example:
myobj = Label(...)
myobj.grid(row=1, column=2)
myobj.grid_remove()
myobj.grid() # to regrid at the old location

The other methods that hide objects are pack_forget() (if you packed the
object) or grid_forget() (if you gridded it). Both of these methods
forget the old configuration, so you'll have to provide full info when
you re-show the object with pack(...) or grid(...).

-- Russell

P.S. I assume you know, but never mix grid and pack. Use one geometry
manager or the other within a particular master frame or widget.