Tkinter Object show and hide

Jørgen Cederberg jorgencederberg at hotmail.com
Tue Aug 20 08:52:12 CEST 2002


"Mauro" <mauro at mr-potatohead.com> wrote in message
news:3b55ea60.0208191517.96dc162 at 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





More information about the Python-list mailing list