我正在使用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)将给出点击坐标。
答案 0 :(得分:0)
您需要捕捉MouseEvents,如here所述。您可以按照我从here
粘贴的教程进行操作不同鼠标按钮的标志如下:wx.MOUSE_BTN_LEFT
wx.MOUSE_BTN_MIDDLE
和wx.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()