簡體   English   中英

如何在Python GUI中檢測右鍵?

[英]How to detect right click in Python GUI?

我正在使用GUI在python中制作掃雷游戲。 我想使用鼠標右鍵單擊以在GUI上標記一個字段。 我有一個graphics.py庫(由老師給我),它具有檢測左鍵單擊的功能。 如何檢測右鍵? 檢測鼠標左鍵的功能是:

def getMouse(self):
    self.update()      # flush any prior clicks
    self.mouseX = None
    self.mouseY = None
    while self.mouseX == None or self.mouseY == None:
        self.update()
        if self.isClosed(): raise GraphicsError("getMouse in closed window")
        time.sleep(.1) # give up thread
    x,y = self.toWorld(self.mouseX, self.mouseY)
    self.mouseX = None
    self.mouseY = None
    return Point(x,y)

點(x,y)會給我點擊坐標。

你需要捕捉MouseEvents,描述在這里 您可以按照我從此處粘貼的教程進行操作

不同鼠標按鈕的標志如下: wx.MOUSE_BTN_LEFT wx.MOUSE_BTN_MIDDLEwx.MOUSE_BTN_RIGHT

#!/usr/bin/python

# mousegestures.py

import wx
import wx.lib.gestures as gest

class MyMouseGestures(wx.Frame):
    def __init__ (self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(600, 500))

        panel = wx.Panel(self, -1)
        mg = gest.MouseGestures(panel, True, wx.MOUSE_BTN_LEFT)
        mg.SetGesturePen(wx.Colour(255, 0, 0), 2)
        mg.SetGesturesVisible(True)
        mg.AddGesture('DR', self.OnDownRight)

    def OnDownRight(self):
          self.Close()

class MyApp(wx.App):
    def OnInit(self):
        frame = MyMouseGestures(None, -1, "mousegestures.py")
        frame.Show(True)
        frame.Centre()
        return True

app = MyApp(0)
app.MainLoop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM