【问题标题】:How to press more than one tkinter button simultaneously using python如何使用python同时按下多个tkinter按钮
【发布时间】:2018-04-18 03:40:40
【问题描述】:

我在 tkinter 窗口中有多个按钮,但在计算机上一次只有一个鼠标指针指向一个位置,如何像在触摸屏上一样同时按下多个按钮?

这是我正在尝试做的示例代码,

#!/usr/bin/python
import Tkinter as Tk

def button_pressed_cb(event):
    print("You pressed " + event.widget['text'])

def button_release_cb(event):
    print("You released " + event.widget['text'])

root = Tk.Tk()
button1 = Tk.Button(root, text="button1")
button1.pack()
button2 = Tk.Button(root, text="button2")
button2.pack()

button1.bind("<ButtonPress>", button_pressed_cb)
button1.bind("<ButtonRelease>", button_release_cb)
button2.bind("<ButtonPress>", button_pressed_cb)
button2.bind("<ButtonRelease>", button_release_cb)

root.mainloop()

执行此操作后,我得到类似这样的示例输出,

You pressed button1
You released button1
You pressed button2
You released button2

我想要实现的是能够生成按这个顺序发生的事件,

You pressed button1
You pressed button2
You released button2
You released button1

有人知道实现这一目标的最佳方法是什么吗?非常感谢。

【问题讨论】:

  • 您在寻找合成事件的代码吗?或者只是构建它们,而不通过事件循环调度它们?还是直接调用处理程序?或者即使 Tkinter 不支持它们也能处理多点触控事件?还是……?
  • 是的,基本上我想处理多点触控事件,即使 Tkinter 不支持它们。由于使用鼠标,我们一次只能单击一个按钮,因此需要一种解决方案,使我能够同时处理任意两个按钮按下或 3 个按钮一起按下等。

标签: python button tkinter


【解决方案1】:

如果你只想调用回调,你可以这样做——毕竟它们是普通的 Python 函数。

如果您询问如何创建事件并分派它,您可以使用event_generate 方法。不幸的是,我不知道这方面的任何 tkinter 文档,因此您必须查看 Tcl/Tk docs。但基本思路是:

def hit_the_buttons():
    button1.event_generate('<ButtonPress>', when='tail')
    button2.event_generate('<ButtonPress>', when='tail')
    button2.event_generate('<ButtonRelease>', when='tail')
    button1.event_generate('<ButtonRelease>', when='tail')

when 参数将事件放在事件队列的末尾,而不是立即处理它们,因此它们不会干扰重绘或实际鼠标事件等事情。这里真的没有必要。我包含它主要是为了展示如何将其中一个 Tcl 参数 (-when tail) 映射到 tkinter。

如果您希望它通过正常调度而不是直接发送到按钮,您可以调用 root.event_generate 并传递 xy 参数,这些参数将在按钮内部进行命中测试。

【讨论】:

    【解决方案2】:

    好的,我找到了解决方案。我进行了额外的检查,如果用户在单击按钮之前按住 ctrl ,则该按钮将锁定到该按下状态。然后用户可以去点击其他按钮等。用户可以再次点击该按钮来解锁它。

    我还为用户添加了悬停消息,告诉用户他们可以使用 ctrl 锁定按钮并实现多键按下。

    这是新代码,

    #!/usr/bin/python
    import Tkinter as Tk
    
    class HoverInfo(Tk.Menu):
        def __init__(self, parent, text):
            Tk.Menu.__init__(self, parent, tearoff=0, 
                             background='light goldenrod yellow')
    
            self.__displayed = False
            for line in text.split('\n'):
                self.add_command(label=line)
    
            parent.bind("<Enter>", self.__display)
            parent.bind("<Leave>", self.__remove)
    
        def __display(self, event):
            if event.widget['state'] == Tk.NORMAL:
                if not self.__displayed:
                    self.__displayed = True
                    self.post(event.x_root+5, event.y_root+5)
    
        def __remove(self, event):
            if self.__displayed:
                self.__displayed = False
                self.unpost()
    
    CONTROL = 4
    
    def button_pressed_cb(event):
        if event.widget['state'] != Tk.DISABLED:
            print("You pressed " + event.widget['text'])
            if (event.state & CONTROL) == CONTROL:
                event.widget.config(state=Tk.DISABLED)
                event.widget.config(relief=Tk.SUNKEN)
    
    def button_release_cb(event):
        if (event.state & CONTROL) != CONTROL:
            print("You released " + event.widget['text'])
            event.widget.config(state=Tk.NORMAL)
            event.widget.config(relief=Tk.RAISED)
    
    
    root = Tk.Tk()
    button1 = Tk.Button(root, text="button1")
    button1.pack()
    button2 = Tk.Button(root, text="button2")
    button2.pack()
    
    button1.bind("<ButtonPress>", button_pressed_cb)
    button1.bind("<ButtonRelease>", button_release_cb)
    button2.bind("<ButtonPress>", button_pressed_cb)
    button2.bind("<ButtonRelease>", button_release_cb)
    
    HoverInfo(button1, "Hint:\nYou can hold Ctrl key before\nclicking a button to latch it.")
    
    root.mainloop()
    

    现在,这就是悬停消息的外观, https://ibb.co/dEb6Bn

    当用户按住 ctrl 键并单击按钮 1 和按钮 2 时,这就是它的外观, https://ibb.co/eVzRBn

    现在,我可以轻松生成这一系列事件,

    You pressed button1
    You pressed button2
    You released button2
    You released button1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 2014-06-14
      • 1970-01-01
      • 2019-08-11
      相关资源
      最近更新 更多