Abhishek Kulkarni
Abhishek Kulkarni

Reputation: 676

Hiding tkinter checkboxes

I am new to tkinter and learning the basics.

The following code opens up a GUI and gives two set of checkboxes.

I want the second set of checkboxes to be hidden.

Python 2 and Python 3 should be visible only when I click on Python from the first set of checkboxes.

from tkinter import *
class Checkbar(Frame):
   def __init__(self, parent=None, picks=[], side=LEFT, anchor=W):
      Frame.__init__(self, parent)
      self.vars = []
      for pick in picks:
         var = IntVar()
         chk = Checkbutton(self, text=pick, variable=var)
         chk.pack(side=side, anchor=anchor, expand=YES)
         self.vars.append(var)
   def state(self):
      return map((lambda var: var.get()), self.vars)
if __name__ == '__main__':
   root = Tk()
   lng = Checkbar(root, ['Python', 'Ruby', 'Perl', 'C++'])
   tgl = Checkbar(root, ['Python 2','Python 3'])
   #tgl = Checkbar(root, ['Ruby 2','Ruby 3'])
   #tgl = Checkbar(root, ['Perl 2','Perl 3'])
   #tgl = Checkbar(root, ['C++ 2','C++ 3'])
   lng.pack(side=TOP,  fill=X)
   tgl.pack(side=LEFT)
   lng.config(relief=GROOVE, bd=2)

   def allstates(): 
      print(list(lng.state()), list(tgl.state()))
   Button(root, text='Quit', command=root.quit).pack(side=RIGHT)
   Button(root, text='Peek', command=allstates).pack(side=RIGHT)
   root.mainloop()

Upvotes: 0

Views: 1106

Answers (1)

temp123
temp123

Reputation: 382

Perhaps something like this might help you, when peek is pressed and the Python checkbox is active the Checkbar is shown. Simply use pack_forget() to hide it and pack() again when you want it to be shown:

from tkinter import *
class Checkbar(Frame):
   def __init__(self, parent=None, picks=[], side=LEFT, anchor=W):
      Frame.__init__(self, parent)
      self.vars = []
      for pick in picks:
         var = IntVar()
         chk = Checkbutton(self, text=pick, variable=var, command=self.foo)
         chk.pack(side=side, anchor=anchor, expand=YES)
         self.vars.append(var)
   def state(self):
      return map((lambda var: var.get()), self.vars)


   def foo(self):
    temp = [str(x) for x in lng.state()]

    if temp[0] == "1":
        tgl.pack(side=LEFT)
        lng.pack(side=TOP,  fill=X)
        lng.config(relief=GROOVE, bd=2)

    if temp[1] == "1":

        lng2.pack(side=TOP,  fill=X)
        lng.config(relief=GROOVE, bd=2)


    if temp[2] == "1":

        lng3.pack(side=TOP,  fill=X)
        lng.config(relief=GROOVE, bd=2)

    if temp[3] == "1":

        lng4.pack(side=TOP,  fill=X)
        lng.config(relief=GROOVE, bd=2)

if __name__ == '__main__':
   root = Tk()
   lng = Checkbar(root, ['Python', 'Ruby', 'Perl', 'C++'])
   tgl = Checkbar(root, ['Python 2','Python 3'])
   lng2 = Checkbar(root, ['Ruby 2','Ruby 3'])
   lng3 = Checkbar(root, ['Perl 2','Perl 3'])
   lng4 = Checkbar(root, ['C++ 2','C++ 3'])
   lng.pack(side=TOP,  fill=X)
   lng2.pack(side=TOP,  fill=X)
   lng2.pack_forget()
   lng3.pack(side=TOP,  fill=X)
   lng3.pack_forget()
   lng4.pack(side=TOP,  fill=X)
   lng4.pack_forget()

   tgl.pack(side=LEFT)
   tgl.pack_forget()

   lng.config(relief=GROOVE, bd=2)
   lng2.config(relief=GROOVE, bd=2)
   lng3.config(relief=GROOVE, bd=2)
   lng4.config(relief=GROOVE, bd=2)



   def allstates(): 
      temp = [str(x) for x in lng.state()]

      if temp[0] == "1":
        tgl.pack(side=LEFT)
        lng.pack(side=TOP,  fill=X)
        lng.config(relief=GROOVE, bd=2)



   Button(root, text='Quit', command=root.quit).pack(side=RIGHT)
   Button(root, text='Peek', command=allstates).pack(side=RIGHT)
   root.mainloop()

Upvotes: 1

Related Questions