mercredi 4 avril 2018

certain number of choices in TKinter

I made a checkbar in Tkinter with many items but I want the user can only choose the equal or less than a certain number of items.this is my current code

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__':
   conn = sqlite3.connect('test1.db')
   c = conn.cursor()
   candid = []
   for a in c.execute("select info from candid"):
       candid.append(a)


   root = Tk()
   lng = Checkbar(root, candid)

   lng.pack(side=TOP,  fill=BOTH)

   lng.config(relief=GROOVE, bd=2)

   def allstates():
      print( list(lng.state() ) )

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

Aucun commentaire:

Enregistrer un commentaire