Python+tkinter两个不同的控件对应不同的事件,可是第二个事件却被自动触发

icy_qin 2014-10-31 09:15:40
我的需求是:点击按钮b1,然后回调crateCase(),createCase生成4个控件。第四个控件是Combobox下拉框。
当这个下拉框中的某个选项被选中之后,会再去做一些事情,这个事情我暂时使用print()来代替。
code是这么实现的:

import collections
from functools import partial
import tkinter
from tkinter import ttk

def createCase(event):
myLabel = partial(tkinter.Label, root, fg='white', bg='blue')
myEntry = partial(tkinter.Entry, root)
myListbox = partial(tkinter.Listbox,)
myOptionMenu = partial(tkinter.OptionMenu)
myCombobox = partial(ttk.Combobox, state='normal')

caseIDLabel = myLabel(text='CaseID: ')
caseIDLabel.pack()

caseIDElement = myEntry()
caseIDElement.pack()

functionLabel = myLabel(text='Function: ')
functionLabel.pack()
funcCombobox = myCombobox(values=["CreateSite", "OpenSite", "DeleteSite"])
funcCombobox.pack()
funcCombobox.bind('<Button-1>', generalParameters())

def generalParameters():
print('hello')

if __name__ == '__main__':
functions = ['CreateSite', 'DeleteSite']
createSite = ['CreateSite', 'ParentSite', 'SiteName', 'Category', 'SiteTemplate']
parameters = collections.defaultdict(list)
parameters = {'CreateSite':['ParentSite', 'SiteName', 'Category', 'SiteTemplate'], 'DeleteSite':['ParentSite', 'SiteName']}

root = tkinter.Tk()
myButton = partial(tkinter.Button, root, fg='white', bg='blue')
b1 = myButton(text='Add new Case after this case')
b1.bind('<Button-1>', createCase)
b1.pack()
root.mainloop()

可是运行之后,我得到的结果是:
点击b1之后,四个控件同时出现(这个是对的),但是同时控制台上print()也把hello打印出来了!
我尝试过把这一行:
funcCombobox.bind('<Button-1>', generalParameters())的绑定事件改成<ButtonPress-1>,也还是不变。
弄了两天了,还是没有进展,谁能给我点意见吗?
...全文
359 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
icy_qin 2014-11-03
  • 打赏
  • 举报
回复
引用 1 楼 panghuhu250 的回复:

funcCombobox.bind('<Button-1>', generalParameters())
应该为:

funcCombobox.bind('<Button-1>', generalParameters)
噢!原来如此,找了半天没找到原因,非常感谢,这分就给你了。 现在按照你的建议修改了之后还有个问题,就是funcCombobox要bind的事件,没有选对,我想要的是,点击下拉框之后,选择其中一个选项,然后再打印hello,现在的做法是,我点击下拉框,就打印了,选中了某个选项之后,反而没有打印。不过这个我可以自己去解决的。
icy_qin 2014-11-03
  • 打赏
  • 举报
回复
噢!原来如此,找了半天没找到原因,非常感谢,这分就给你了。 现在按照你的建议修改了之后还有个问题,就是funcCombobox要bind的事件,没有选对,我想要的是,点击下拉框之后,选择其中一个选项,然后再打印hello,现在的做法是,我点击下拉框,就打印了,选中了某个选项之后,反而没有打印。不过这个我可以自己去解决的。
panghuhu250 2014-10-31
  • 打赏
  • 举报
回复

funcCombobox.bind('<Button-1>', generalParameters())
应该为:

funcCombobox.bind('<Button-1>', generalParameters)

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧