【问题标题】:Tkinter, when button pressed (not released), do command [closed]Tkinter,当按下按钮(未释放)时,执行命令 [关闭]
【发布时间】:2017-12-21 09:39:40
【问题描述】:

我正在使用 Python 2.7.13 和 Tkinter。

这是我的程序的一部分:

PAButton = Tkinter.ButtonPress(PAFrame, text='PA', fg='white', bg='blue')
PAButton.pack()
self.PAButton.bind("<ButtonPress>", self.playPA)


这是我正在尝试做的事情的要点:

当 PAButton 被按下(未释放)时,运行脚本前面定义的函数 playPA()


但是,我得到了这个错误
AttributeError: 'module' object has no attribute 'ButtonPress'


我将如何正确地实现我的目标?

【问题讨论】:

标签: python python-2.7 user-interface tkinter


【解决方案1】:

没有ButtonPress 这样的东西。我是Button

编辑:对于.bind() 函数,请使用"&lt;Button-1&gt;",而不是"&lt;ButtonPress&gt;"

【讨论】:

  • 但是,该命令仅在释放按钮时发生。按下按钮时如何运行命令?
  • 使用您的编辑,我不得不像这样写playPA()PAButton.bind("&lt;Button-1&gt;", playPA()),因为我收到了一个错误:TypeError: playPA() takes no arguments (1 given)。但是,当我现在运行程序时,playPA() 函数在 GUI 启动之前发生,当我按下 PAButton 时什么也没有发生。
  • 在使用bind() 的函数绑定中,不应将() 添加到函数名称的末尾。此外,如果您还没有,请将 event 添加到函数的参数中。
  • 这解决了这个问题。谢谢!
猜你喜欢
  • 2017-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-19
  • 1970-01-01
  • 2016-01-28
  • 2018-08-11
  • 1970-01-01
相关资源
最近更新 更多