我有一个列列表colList = ['A','B','C','D']
,它与复选框一起显示给用户,用户选中复选框并单击提交按钮。在提交时,我只想得到检查值的列。你知道吗
我用的是Tkinter,网格结构。你知道吗
代码如下。你知道吗
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()
当我只需要选择几个复选框时,问题就出现了,然后如何区分一个复选框和另一个复选框。我还需要获得列名,比如weithervariable = var[]
,以便使用或以其他方式使用。请帮帮我。你知道吗
最简单的解决方案是将变量存储在字典中。你知道吗
然后可以使用
vars[(row, column)]
访问每一行和每一列的checkbutton。你知道吗所以这里我刚刚使用了checkbutton的command函数,当它检测到按下checkbutton时,它会将按钮的名称传递给回调函数,回调函数会将按钮附加到列表中。如果松开按钮,则会将其拆下。这应该适用于您想要的任意多个按钮,您所要做的就是将该列表的值导出到csv。你知道吗
我在for循环中添加了按钮的名称作为显式变量,这样就可以将它传递给函数,也可以导入functools.部分将变量传递给回调函数。你知道吗
另一个小改动是删除了变量=var[]部分,因为这似乎没有任何作用。你知道吗
希望这是你问的问题!你知道吗
相关问题 更多 >
编程相关推荐