2014-12-05 2 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을 변경 기능에 클릭을 결합하지 않는 경우 포함 된 변수를 만들 수 있습니다 . 이 같은

뭔가 : 이것은 당신이 자기와 기능에 leftright 변수를 전달 할 수 있기 때문에

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

나는, 클래스에 응용 프로그램을 만들었습니다.

+0

감사! 얼마 후에 정기적으로 클릭 수를 확인하고 싶습니다. 이전 프로그램에서 어디에서 루프를 만들어야합니까? – Pink

+0

'잠시 후'란 무엇을 의미합니까? 첫 번째'root.after (...)'호출을 설정하여 원하는 곳 어디에서도 te loop을 시작할 수 있습니다. – fhdrsdg

관련 문제