如何在Python GUI中检测右键单击?

如何在Python GUI中检测右键单击?,python,minesweeper,Python,Minesweeper,我正在用python和GUI制作一个扫雷游戏。我想使用鼠标右键单击在GUI上标记一个字段。我有一个graphics.py库(老师给我的),它有一个检测左键点击的功能。如何检测右键单击? 检测左击的功能是: def getMouse(self): self.update() # flush any prior clicks self.mouseX = None self.mouseY = None while self.mouseX == None or

我正在用python和GUI制作一个扫雷游戏。我想使用鼠标右键单击在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)将给出单击坐标。

您需要捕获鼠标事件,如前所述。您可以按照我从中粘贴的教程进行操作

不同鼠标按钮的标志如下:
wx.mouse\u BTN\u LEFT
wx.MOUSE\u BTN\u MIDDLE
wx.MOUSE\u BTN\u 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()

我想有你想要的。我一定会尝试这种方法。(Y) @M_G绝对希望它能帮助你,至少在理解如何使用特定的鼠标按钮方面。也修正了格式。