Python Tkinter Grid Checkbox -


i wondering if there easy way create grid of checkboxes using tkinter. trying make grid of 10 rows , columns (so 100 checkboxes) 2 checkboxes can selected per row.

edit: i'm using python 2.7 spyder

what have far:

from tkinter import*  master = tk() master.title("select groups")  rows=10 columns=10   x in range(rows):     y in range(columns):         label(master, text= "group %s"%(y+1)).grid(row=0,column=y+1)         label(master, text= "test %s"%(x+1)).grid(row=x+1,column=0)         checkbutton(master).grid(row=x+1, column=y+1)  mainloop() 

i'm trying use state='disabled' grey out row once 2 checkboxes have been selected.

here's example using provided 10x10 grid. should give basic idea of how implement this.

just make sure keep reference every checkbutton (boxes in example) every intvar (boxvars in example).

here's why:

-checkbuttons needed call config(state = disabled/normal).

-intvars needed determine value of each checkbutton.

aside crucial elements 2d array processing.

here's example code (now based off of provided code).

from tkinter import *  master = tk() master.title("select groups")  rows=10 columns=10  boxes = [] boxvars = []  # create intvars, set 0  in range(rows):     boxvars.append([])     j in range(columns):         boxvars[i].append(intvar())         boxvars[i][j].set(0)  def checkrow(i):     global boxvars, boxes     row = boxvars[i]     deselected = []      # loop through row changed, check items not selected      # (so know indeces disable in event 2 have been selected)      j in range(len(row)):         if row[j].get() == 0:             deselected.append(j)      # check if enough buttons have been selected. if so, disable deselected indeces,     # otherwise set of them active (in case have disabled them).      if len(deselected) == (len(row) - 2):         j in deselected:             boxes[i][j].config(state = disabled)     else:         item in boxes[i]:             item.config(state = normal)  def getselected():     selected = {}     in range(len(boxvars)):         temp = []         j in range(len(boxvars[i])):             if boxvars[i][j].get() == 1:                 temp.append(j + 1)         if len(temp) > 1:             selected[i + 1] = temp     print selected   x in range(rows):     boxes.append([])     y in range(columns):         label(master, text= "group %s"%(y+1)).grid(row=0,column=y+1)         label(master, text= "test %s"%(x+1)).grid(row=x+1,column=0)         boxes[x].append(checkbutton(master, variable = boxvars[x][y], command = lambda x = x: checkrow(x)))         boxes[x][y].grid(row=x+1, column=y+1)  b = button(master, text = "get", command = getselected, width = 10) b.grid(row = 12, column = 11) mainloop() 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -