Python Forum
doing something after been checked a checkbutton - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: doing something after been checked a checkbutton (/thread-2075.html)



doing something after been checked a checkbutton - gray - Feb-17-2017

i want when i tick  'python' checkbutton in 'lng' checkbar in under program and then i push 'peek' button, it prints 'python'
i dont get any error ...but it doesnt work
why?....please help
this is my code
#!/usr/bin/python3

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)
   def __getitem__(self,key):
        return self.vars[key]
   
   
if __name__ == '__main__':
   root = Tk()
   lng = Checkbar(root, ['Python', 'Ruby', 'Perl', 'C++'])
   tgl = Checkbar(root, ['English','German'])
   lng.pack(side=TOP,  fill=X)
   tgl.pack(side=LEFT)
   lng.config(relief=GROOVE, bd=2)
   

   def allstates(): 
      
##      print(lng[0])
      
      if lng[0] == 1:
         print ('python')
        
      
   Button(root, text='Quit', command=root.quit).pack(side=RIGHT)
   Button(root, text='Peek', command=allstates).pack(side=RIGHT)
   root.mainloop()



RE: doing something after been checked a checkbutton - Axel_Erfurt - Feb-18-2017

def allstates(): 
        if lng[0].get() == 1:
            print ('python')