复选框列表值仅说明所有未选中的内容吗?

时间:2019-05-20 09:08:52

标签: python tkinter

我正在尝试设置一个定义,以便当人员选择所需的复选框时,当他们按下保存按钮时,它将打印其已选中还是未选中。但是,所有这些都未经检查就出现了。

它正在使用Tkinter btw

我已经尝试在checkbutton事件中使用onvalue和offvalues,但是它不起作用

def checker():
    global var_list
    var_list = [var1.get(), var2.get(), var3.get(), var4.get(), 
    var5.get(), 
    var6.get(), var7.get(), var8.get(), var9.get(), var10.get()]
for var in var_list:
        if var == 1:
            print('checked')
        else:
            print('unchecked')


var1 = IntVar()
var2 = IntVar()
var3 = IntVar()
var4 = IntVar()
var5 = IntVar()
var6 = IntVar()
var7 = IntVar()
var8 = IntVar()
var9 = IntVar()
var10 = IntVar()
Checkbutton(event_1_window, text= web_1_title[0] + " " + web_1_date[0], 
variable=var1).grid(row=0, sticky=W)
Checkbutton(event_1_window, text= web_1_title[1] + " " + web_1_date[1], 
variable=var2).grid(row=1, sticky=W)
Checkbutton(event_1_window, text= web_1_title[2] + " " + web_1_date[2], 
variable=var3).grid(row=2, sticky=W)
Checkbutton(event_1_window, text= web_1_title[3] + " " + web_1_date[3], 
variable=var4).grid(row=3, sticky=W)
Checkbutton(event_1_window, text= web_1_title[4] + " " + web_1_date[4], 
variable=var5).grid(row=4, sticky=W)
Checkbutton(event_1_window, text= web_1_title[5] + " " + web_1_date[5], 
variable=var6).grid(row=5, sticky=W)
Checkbutton(event_1_window, text= web_1_title[6] + " " + web_1_date[6], 
variable=var7).grid(row=6, sticky=W)
Checkbutton(event_1_window, text= web_1_title[7] + " " + web_1_date[7], 
variable=var8).grid(row=7, sticky=W)
Checkbutton(event_1_window, text= web_1_title[8] + " " + web_1_date[8], 
variable=var9).grid(row=8, sticky=W)
Checkbutton(event_1_window, text= web_1_title[9] + " " + web_1_date[9], 
variable=var10).grid(row=9, sticky=W)


var_list = [var1.get(), var2.get(), var3.get(), var4.get(), var5.get(), 
var6.get(), var7.get(), var8.get(), var9.get(), var10.get()]

它说所有内容都未被选中,即使我已经选中了程序中的每个框。

1 个答案:

答案 0 :(得分:0)

似乎您可能有缩进错误或类似的错误。我没有修改您的checker函数,只是添加了代码中缺少的一些变量,而且看起来工作正常。

from tkinter import *



def checker():
    global var_list
    var_list = [var1.get(), var2.get(), var3.get(), var4.get(), 
    var5.get(), 
    var6.get(), var7.get(), var8.get(), var9.get(), var10.get()]
    for var in var_list:
        if var == 1:
            print('checked')
        else:
            print('unchecked')

event_1_window = Tk()

web_1_title = ["title{0}".format(i) for i in range(10)]
web_1_date = ["date{0}".format(i) for i in range(10)]

var1 = IntVar()
var2 = IntVar()
var3 = IntVar()
var4 = IntVar()
var5 = IntVar()
var6 = IntVar()
var7 = IntVar()
var8 = IntVar()
var9 = IntVar()
var10 = IntVar()
Checkbutton(event_1_window, text= web_1_title[0] + " " + web_1_date[0], 
variable=var1).grid(row=0, sticky=W)
Checkbutton(event_1_window, text= web_1_title[1] + " " + web_1_date[1], 
variable=var2).grid(row=1, sticky=W)
Checkbutton(event_1_window, text= web_1_title[2] + " " + web_1_date[2], 
variable=var3).grid(row=2, sticky=W)
Checkbutton(event_1_window, text= web_1_title[3] + " " + web_1_date[3], 
variable=var4).grid(row=3, sticky=W)
Checkbutton(event_1_window, text= web_1_title[4] + " " + web_1_date[4], 
variable=var5).grid(row=4, sticky=W)
Checkbutton(event_1_window, text= web_1_title[5] + " " + web_1_date[5], 
variable=var6).grid(row=5, sticky=W)
Checkbutton(event_1_window, text= web_1_title[6] + " " + web_1_date[6], 
variable=var7).grid(row=6, sticky=W)
Checkbutton(event_1_window, text= web_1_title[7] + " " + web_1_date[7], 
variable=var8).grid(row=7, sticky=W)
Checkbutton(event_1_window, text= web_1_title[8] + " " + web_1_date[8], 
variable=var9).grid(row=8, sticky=W)
Checkbutton(event_1_window, text= web_1_title[9] + " " + web_1_date[9], 
variable=var10).grid(row=9, sticky=W)

button = Button(event_1_window, text="Check", command=checker)
button.grid(row=10,sticky=W)

var_list = [var1.get(), var2.get(), var3.get(), var4.get(), var5.get(), 
var6.get(), var7.get(), var8.get(), var9.get(), var10.get()]