简体   繁体   English

使用 PyGTK 的右键单击菜单(上下文菜单)

[英]Right Click Menu (context menu) using PyGTK

So I'm still fairly new to Python, and have been learning for a couple months, but one thing I'm trying to figure out is say you have a basic window...所以我对 Python 还是很陌生,并且已经学习了几个月,但我想弄清楚的一件事是说你有一个基本的 window ...

#!/usr/bin/env python

import sys, os
import pygtk, gtk, gobject

class app:

   def __init__(self):
    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    window.set_title("TestApp")
    window.set_default_size(320, 240)
    window.connect("destroy", gtk.main_quit)
    window.show_all()

app()
gtk.main()

I wanna right click inside this window, and have a menu pop up like alert, copy, exit, whatever I feel like putting down.我想在这个 window 内单击鼠标右键,然后弹出一个菜单,如警告、复制、退出,无论我想放下什么。

How would I accomplish that?我将如何做到这一点?

There is a example for doing this very thing found at http://www.pygtk.org/pygtk2tutorial/sec-ManualMenuExample.htmlhttp://www.pygtk.org/pygtk2tutorial/sec-ManualMenuExample.html有一个做这件事的例子

It shows you how to create a menu attach it to a menu bar and also listen for a mouse button click event and popup the very same menu that was created.它向您展示了如何创建一个将其附加到菜单栏的菜单,以及侦听鼠标按钮单击事件并弹出与创建的完全相同的菜单。

I think this is what you are after.我想这就是你所追求的。

EDIT: (added further explanation to show how to respond to only right mouse button events)编辑:(添加了进一步的解释以显示如何仅响应鼠标右键事件)

To summarise.总结一下。

Create a widget to listen for mouse events on.创建一个小部件来监听鼠标事件。 In this case it's a button.在这种情况下,它是一个按钮。

button = gtk.Button("A Button")

Create a menu创建菜单

menu = gtk.Menu()

Fill it with menu items用菜单项填充它

menu_item = gtk.MenuItem("A menu item")
menu.append(menu_item)
menu_item.show()

Make the widget listen for mouse press events, attaching the menu to it.使小部件侦听鼠标按下事件,将菜单附加到它。

button.connect_object("event", self.button_press, menu)

Then define the method which handles these events.然后定义处理这些事件的方法。 As is stated in the example in the link, the widget passed to this method is the menu that you want popping up not the widget that is listening for these events.如链接中的示例所述,传递给此方法的小部件是您要弹出的菜单,而不是正在侦听这些事件的小部件。

def button_press(self, widget, event):
    if event.type == gtk.gdk.BUTTON_PRESS and event.button == 3:
        #make widget popup
        widget.popup(None, None, None, event.button, event.time)
        pass

You will see that the if statement checks to see if the button was pressed, if that is true it will then check to see which of the buttons was pressed.您将看到 if 语句检查按钮是否被按下,如果是真的,它将检查哪个按钮被按下。 The event.button is a integer value, representing which mouse button was pressed. event.button 是一个 integer 值,表示按下了哪个鼠标按钮。 So 1 is the left button, 2 is the middle and 3 is the right mouse button.所以1是左键,2是中键,3是鼠标右键。 By checking to see if the event.button is 3, you are only responding to mouse press events for the right mouse button.通过检查 event.button 是否为 3,您仅响应鼠标右键的鼠标按下事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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