2014-12-05 77 views
-2

我想定期检查是否按下按钮。如果没有,那么我想打印一些东西。我需要一个简单的例子来实现这个。提前致谢。如何知道右键单击是否在Tkinter python中完成?


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() 

回答

2

您可以包含如果按下按钮或者没有,那么点击绑定到改变这些变量的广告使用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变量的函数自。

+0

谢谢!我想在一段时间后定期检查点击。我在哪里应该在之前的程序中做出循环? – Pink 2014-12-07 13:52:31

+0

'一段时间后'是什么意思?您可以通过设置第一个'root.after(...)'调用来启动您想要的循环。 – fhdrsdg 2014-12-08 08:57:59

相关问题