【发布时间】: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: