我想创建一个事件,该事件由列表框选中项目的更改或单选按钮所选项目的更改触发。可能吗?我使用这段代码:
def getScript(event):
state = rb.get()
listScript = []
processor = ()
processor = lb1.get(lb1.curselection())
if processor :
if (state == 1):
print processor
if (state == 2):
pass
if (state == 3):
pass
frame2 = Frame(top)
frame2.pack(fill = X)
rb = IntVar()
R1 = Radiobutton(frame2, text = "Parallel Test",
variable = rb, value = 1, command = getScript)
R2 = Radiobutton(frame2, text = "Non Parallel Test",
variable = rb, value = 2, command = getScript)
R3 = Radiobutton(frame2, text = "Specific Test",
variable = rb, value = 3, command = getScript)
R1.grid(row = 0, column = 0, padx = 10)
R2.grid(row = 0, column = 1, padx = 10)
R3.grid(row = 0, column = 2, padx = 10)
frame3 = Frame(top)
frame3.pack(fill = X)
space_frame3 = Frame(frame3, width = 10)
l5 = Label(frame3, text = "Processor Unit")
l6 = Label(frame3, text = "Script for test")
lb1 = Listbox(frame3, height = 7, exportselection = 0)
lb1.bind('<<ListboxSelect>>',getScript)
scrollbar = Scrollbar(frame3)
lb2 = Listbox(frame3, height = 7, width = 40,
yscrollcommand = scrollbar.set, exportselection = 0)
只要我在选择列表框中的项目之前选择了一个单选按钮,一切都会顺利进行。但每次我选择另一个单选按钮时,它都会返回:
TypeError: getScript() takes exactly 1 argument (0 given)
答案 0 :(得分:3)
选择Radiobutton时会发生什么:
1)intVar rb
设置为按钮值
2)调用getScript命令。
但是,Radiobutton选择不会产生任何事件,这就是为什么在这种情况下有2个选项:
使用lambda函数调用函数来提供参数,因为命令getScript
需要一个参数(这是错误消息)。
R1 = Radiobutton(frame2, text = "Parallel Test",
variable = rb, value = 1,
command = lambda : getScript(None) )
另一个选择是 - 因为您的目标似乎是知道选择了哪个小部件 - 使用这样的命令:
R1 = Radiobutton(frame2, text = "Parallel Test",
variable = rb, value = 1,
command = lambda : getScript(R1) )
使用带有小部件的getSript
函数。像这样:
def getSript(widget):
print widget["text"]