Reputation: 148
I have a column list colList = ['A','B','C','D']
which is displayed along with checkbox to user, user checks the checkbox and clicks submit button. On submit i want to get the column of the check values only.
I am using Tkinter, Grid structure.
Code is as follows.
from tkinter import filedialog
from tkinter import *
import tkinter as tk
def command_to_extract():
print('Extract button pressed')
#this function will display which checkbox is checked and display the column name - But I am not able to write the check condition for checking the value of checkbox
root = Tk()
button3 = Button(text="Extract", command=command_to_extract, width=30, anchor=NW)
button3.grid(row=4, column=4)
lbl31 = Label(master=root, text="Select the column(s) for output csv file", width=30, anchor=NW)
lbl31.grid(row=5, column=2)
colList = ['A','B','C','D']
columnDictionary = { i : colList[i] for i in range(0, len(colList) ) } # To Make a dictionary from a List
p = 0 #variable for column
#this double for loop for 2X2 display of checkbox
for i in range(0, 2): #row
for j in range(0, 2): #column
Checkbutton(master=root, text = colList[p], variable = var[], onvalue = 1, offvalue = 0, width=40, anchor=NW).grid(row = i, column = j)
p = p + 1
mainloop()
Problem arises when i need to select only few checkbox, then how to distinguish one checkbox with other. Also i need to get the column name such as weither variable = var[]
to use or some other way. Please help me out.
Upvotes: 0
Views: 1120
Reputation: 385940
The simplest solution is to store the variables in a dictionary.
vars = {}
for row in range(0, 2):
for column in range(0, 2):
vars[(row,column)] = IntVar()
Checkbutton(..., variable=vars[(row,column)], ...)
You can then access the checkbutton for each row and column with vars[(row, column)]
.
def command_to_extract():
print('Extract button pressed')
for row in range(0,2):
for column in range(0,2):
var = vars[(row, column)]
print("{},{}: {}".format(row, column, var.get())
Upvotes: 1
Reputation: 26
from functools import partial
optionsticked = []
def callback(name):
if name in optionsticked:
optionsticked.remove(name)
optionsticked.sort()
print(optionsticked)
return
optionsticked.append(name)
optionsticked.sort()
print(optionsticked)
for i in range(0, 2): #row
for j in range(0, 2): #column
name = colList[p]
Checkbutton(master=root, text = name, onvalue = 1, offvalue = 0, width=40, anchor=NW, command = partial(callback,name) ).grid(row = i, column = j)
p += 1
mainloop()
So here I've just used the command function of the checkbutton, so that when it detects a press of the checkbutton, it passes the name of the button to the callback function, which appends the button to a list. If the button is unticked, it is removed. This should work for as many buttons as you wish and all you have to do is export the values of that list to a csv.
I've added the name of the button as an explicit variable within the for loop so that it can be passed to the function, and also imported functools.partial to pass variables to the callback function.
Another minor change was removing the variable = var[] part, as this didn't seem to do anything.
Hope this was the question you asked!
Upvotes: 1