【问题标题】:How to present user check boxes and then save a list to the checked ones?如何呈现用户复选框,然后将列表保存到选中的复选框?
【发布时间】:2021-07-04 13:40:11
【问题描述】:

如何在不为每个复选框编写单独的函数的情况下执行此操作,就像我在注释掉的代码中所做的那样,“func”函数有什么问题,为什么它不起作用?

from tkinter import *

ws = Tk()
ws.title('Terminator')
ws.geometry('200x80')


# def vlc_func():
#     if vlc.get() == 1:
#         print("selected")
#     elif vlc.get() == 0:
#         print("deselected")

# def notepad_func():
#     if notepad.get() == 1:
#         print("selected")
#     elif notepad.get() == 0:
#         print("deselected")

def func(associated_variable):
    if associated_variable.get() == 1:
        print("selected")
    elif associated_variable.get() == 0:
        print("deselected")


vlc = IntVar()
vlc.set(0)
Checkbutton(ws, text= "vlc.exe", variable=vlc, onvalue=1, offvalue=0, command= func(vlc)).pack()

notepad = IntVar()
Checkbutton(ws, text= "notepad.exe", variable=notepad, onvalue=1, offvalue=0, command= func(notepad)).pack()




ws.mainloop()

    

【问题讨论】:

  • 你快到了!传递参数时需要输入lambda: func(notepad)lambda:

标签: python tkinter checkbox


【解决方案1】:

问题在于您将参数传递给回调函数的方式。如果在设置命令参数的时候直接传参,函数只调用一次,运行一次,只传函数的结果,不传函数本身。这就是导致运行脚本时“取消选择”行为打印两次的原因

检查这个:How to pass an argument to event handler in tkinter\ 一旦你按照这个,一切都应该按照你的预期工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 2023-03-28
    • 2010-12-18
    • 1970-01-01
    • 2017-12-17
    • 2014-07-06
    相关资源
    最近更新 更多