我正在为 ubuntu 12.04 制作一个应用程序。我想做的是向菜单栏添加一个选项,当我们右键单击某个选择选项时会出现该选项。
为了更清楚-
在通常情况下,当我们选择一些文本并右键单击时,会出现一些选项,例如
剪切
复制
粘贴
我想添加另一个选项我该怎么做。
单击该选项时,只需执行另一个应用程序并将所选数据发送到该应用程序。
我会使用 Glade 和 python 进行开发。
请您参考如下方法:
你应该首先了解一些关于 glade 和 gtk 的基础知识。以下仅为注释:
1-On glade you can use the menu button to create menu.
2-Right click on it an dchoose "edit ..."
3-from the window you can add items.(the right part display the name and type of the menu item , the left part display the properties of the selected item, the lower part display the signals which could connected to the menu item)
4- now connect the menu item with the function which do what you want (When clicked the option would just have to execute another application and send the selected data to that applicaion.)
5- go to your code. get the menu as usual .
self.menu = self.builder.get_object("menu")
6- to popup the menu on right click on an object, you should connect that object with the function ahich execute the menu ( assuming that its name is :on_button_press ) :
def on_button_press(self, treeview, event):
if event.button == 3:
x = int(event.x)
y = int(event.y)
time = event.time
pthinfo = treeview.get_path_at_pos(x, y)
if pthinfo is not None:
path, col, cellx, celly = pthinfo
treeview.grab_focus()
treeview.set_cursor( path, col, 0)
self.popupmenu.popup( None, None, None, event.button, time)
return True
