【问题标题】:How to know whether right clicked is done in Tkinter python? [closed]如何知道在 Tkinter python 中是否完成了右键单击? [关闭]
【发布时间】:2014-12-05 10:23:40
【问题描述】:

我想定期检查按钮是否被按下。如果没有,那么我想打印一些东西。我需要简单的例子来实现这一点。提前致谢。


from Tkinter import *
import subprocess

def execute_querie1():
    counter = 0
    global a 
    a = 0
    def onRightClick(event):
        print 'Got right mouse button click:', 
        showPosEvent(event)
        print ("Right clickkkk")
        close_window()
        a = 1

        return a

    def close_window (): 
      # root.destroy()
      tkroot.destroy()

    def showPosEvent(event):
        print 'Widget=%s X=%s Y=%s' % (event.widget, event.x, event.y)

    def quit(event):                           
        print("Double Click, so let's stop") 
        import sys; sys.exit()

    def onLeftClick(event):
        a = True

        print 'Got light mouse button click:', 
        showPosEvent(event)
        print ("Left clickkkk" )
        close_window()
        return a

    subprocess.call(["xdotool", "mousemove", "700", "400"]) 
    tkroot = Tk()
    labelfont = ('courier', 20, 'bold')               
    widget = Label(tkroot, text='Hello bind world')
    widget.config(bg='red', font=labelfont)          
    widget.config(height=640, width=480)                
    widget.pack(expand=YES, fill=BOTH)

    g = widget.bind('<Button-3>',  onRightClick)        
    h = widget.bind('<Button-1>',  onLeftClick)        
    print g
    print h
    widget.focus()                                    
    tkroot.title('Click Me')
    tkroot.mainloop()


if __name__ == "__main__":
    execute_querie1()

【问题讨论】:

    标签: python button tkinter click


    【解决方案1】:

    您可以创建包含按钮是否被按下的变量,然后将点击绑定到改变这些变量的函数,并使用after 定期运行一个函数来检查按钮是否已被点击。

    类似这样的:

    from Tkinter import *
    
    class App():
        def __init__(self):
            self.root = Tk()
            self.root.geometry('300x300+100+100')
    
            self.left = False
            self.right = False
            self.root.bind('<Button-1>', self.lefttclick)
            self.root.bind('<Button-3>', self.rightclick)
    
            self.root.after(10, self.clicked)
            self.root.mainloop()
    
        def clicked(self):
            if not self.right and not self.left:
                print 'Both not clicked'
            elif not self.left:
                print 'Left not clicked'
            elif not self.right:
                print 'Right not clicked'
    
            self.right = False
            self.left = False
            self.root.after(1000, self.clicked)
    
        def rightclick(self, event):
            self.right = True
    
        def lefttclick(self, event):
            self.left = True
    
    App()
    

    我已经将应用程序变成了一个类,因为这样您就可以将 leftright 变量传递给带有 self 的函数。

    【讨论】:

    • 谢谢!我想在一段时间后定期检查点击次数。在以前的程序中我应该在哪里循环?
    • “一段时间后”是什么意思?通过设置第一个root.after(...) 调用,您可以在任何地方开始循环。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 2015-04-17
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    相关资源
    最近更新 更多