简体   繁体   English

如何在Python GUI中检测右键?

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

I am making a minesweeper game in python with GUI. 我正在使用GUI在python中制作扫雷游戏。 I want to use the right click of the mouse to flag a field on the GUI. 我想使用鼠标右键单击以在GUI上标记一个字段。 I have a graphics.py library (give to me by my teacher) which has a function to detect left-clicks. 我有一个graphics.py库(由老师给我),它具有检测左键单击的功能。 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. 点(x,y)会给我点击坐标。

You need to catch MouseEvents, as described here . 你需要捕捉MouseEvents,描述在这里 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 不同鼠标按钮的标志如下: 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