【发布时间】:2019-07-26 10:46:59
【问题描述】:
我有一个列列表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()
当我只需要选择几个复选框时,就会出现问题,然后如何区分一个复选框与另一个复选框。我还需要获取列名,例如weither variable = var[] 以使用或其他方式。请帮帮我。
【问题讨论】:
-
Checkbutton 返回一个对象。使用变量或列表来存储对象以供进一步操作。祝你好运!
-
您可以将
name参数添加到您的复选框(name="unique_name"),然后只需检查名称。 -
for loop中的variable = var[]在 HTML 中无法正常工作 -
@Ardweaden Name 参数是否可以在
Checkbutton()中使用?在 python tkinter 中 -
@SANTOSHKUMARDESAI 请解释一下,因为我是 python 新手