0

我正在使用带有 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)

Point(x,y) 会给我点击坐标。

4

1 回答 1

0

您需要捕获 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()
于 2013-08-08T17:58:53.520 回答