由Tkinter中的列表框和单选按钮触发的事件 [英] Event triggered by Listbox and Radiobutton in Tkinter
问题描述
我想创建一个由列表框"选定项的更改或单选按钮"选定项的更改触发的事件.是否可以?我使用以下代码:
I want to make an event that is triggered by either a change in List box selected item or a change in Radio button selected item. Is it possible? I use this code:
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)
只要我在列表框"中选择一个项目之前选择了一个单选按钮,一切就可以正常进行.但是每次我选择另一个单选按钮时,它都会返回:
Everything goes fine as long as I've selected a radiobutton before selecting an item in Listbox. But every time I select another radio button, it return:
TypeError: getScript() takes exactly 1 argument (0 given)
推荐答案
选择单选按钮时会发生以下情况:
What happen when you select a Radiobutton is :
1)将intVar rb
设置为按钮值
1) The intVar rb
is set to the button value
2)调用命令getScript.
2) The command getScript is called.
但是,单选按钮选择不会生成任何事件,因此在这种情况下有两个选项:
But, a Radiobutton selection do not generates any event, that's why there is 2 options in this case :
使用lambda函数调用该函数以提供一个参数,因为您的命令 getScript
需要一个参数(这就是错误消息).
Call the function using a lambda function to provide a parameter as your command getScript
expect one parameter ( that's the error message ).
R1 = Radiobutton(frame2, text = "Parallel Test",
variable = rb, value = 1,
command = lambda : getScript(None) )
另一个选择是-似乎您的目标是知道选择了哪个小部件-使用这样的命令:
An other option is - as it seem that your aim is to know what widget was selected - to use such a command:
R1 = Radiobutton(frame2, text = "Parallel Test",
variable = rb, value = 1,
command = lambda : getScript(R1) )
使用带有小部件的 getSript
函数.像这样:
with your getSript
function that takes a widget. Like this:
def getSript(widget):
print widget["text"]
这篇关于由Tkinter中的列表框和单选按钮触发的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!