Dan*_*nia 5 python combobox tkinter
我使用Tkinter在python中创建了一个简单的组合框,我想检索用户选择的值.搜索之后,我想我可以通过绑定选择事件并调用一个将使用类似box.get()的函数来完成此操作,但这不起作用.程序启动时,会自动调用该方法,并且不会打印当前选择.当我从组合框中选择任何项目时,不会调用任何方法.这是我的代码片段:
self.box_value = StringVar()
self.locationBox = Combobox(self.master, textvariable=self.box_value)
self.locationBox.bind("<<ComboboxSelected>>", self.justamethod())
self.locationBox['values'] = ('one', 'two', 'three')
self.locationBox.current(0)
Run Code Online (Sandbox Code Playgroud)
这是我从框中选择项目时应该调用的方法:
def justamethod (self):
print("method is called")
print (self.locationBox.get())
Run Code Online (Sandbox Code Playgroud)
谁能告诉我如何获得所选值?
编辑:我已经纠正了对justamethod的调用,通过在将盒子绑定到James Kent建议的函数时删除括号.但现在我收到了这个错误:
TypeError:justamethod()只取1个参数(给定2个)
编辑2:我已经发布了这个问题的解决方案.
谢谢.
我已经弄清楚代码中有什么问题了.
首先,正如James所说,当将justamethod绑定到组合框时,应该删除括号.
其次,关于类型错误,这是因为justamethod是一个事件处理程序,所以它应该采用两个参数,self和event,就像这样,
def justamethod (self, event):
Run Code Online (Sandbox Code Playgroud)
进行这些更改后,代码运行良好.
| 归档时间: |
|
| 查看次数: |
19402 次 |
| 最近记录: |