2014-12-05 29 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

相關問題