简体   繁体   中英

How to detect right click in Python GUI?

I am making a minesweeper game in python with GUI. I want to use the right click of the mouse to flag a field on the GUI. I have a graphics.py library (give to me by my teacher) which has a function to detect left-clicks. How can I detect right click? The function for detecting left-click is:

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) will give me the click-coordinates.

You need to catch MouseEvents, as described here . You can follow the tutorial I've pasted from here

The flags for the different mouse buttons are as follows: wx.MOUSE_BTN_LEFT wx.MOUSE_BTN_MIDDLE and 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM