from gi.repository import Gtk
class aStatusIcon:
def __init__(self):
self.statusicon = Gtk.StatusIcon()
self.statusicon.set_from_stock(Gtk.STOCK_HOME)
self.statusicon.connect("popup-menu", self.right_click_event)
window = Gtk.Window()
window.connect("destroy", lambda w: Gtk.main_quit())
window.show_all()
def right_click_event(self, icon, button, time):
menu = Gtk.Menu()
about = Gtk.MenuItem()
about.set_label("About")
quit = Gtk.MenuItem()
quit.set_label("Quit")
about.connect("activate", self.show_about_dialog)
quit.connect("activate", Gtk.main_quit)
menu.append(about)
menu.append(quit)
menu.show_all()
#menu.popup(None, None, gtk.status_icon_position_menu, button, time, self.statusicon) # previous working pygtk line
menu.popup(None, None, None, Gtk.StatusIcon.position_menu, button, time) #i assume this is problem line
def show_about_dialog(self, widget):
about_dialog = Gtk.AboutDialog()
about_dialog.set_destroy_with_parent(True)
about_dialog.set_name("StatusIcon Example")
about_dialog.set_version("1.0")
about_dialog.set_authors(["Andrew Steele"])
about_dialog.run()
about_dialog.destroy()
aStatusIcon()
Gtk.main()
我认为问题是我不会在那里告诉关于self.statusicon的菜单,但它在任何args中都不起作用,因为它们都需要一个小部件arg或none,而不是一个statusicon,这里的任何聪明人都有了一个想法哪里出错?
答案 0 :(得分:16)
啊,最后,如果有其他人有这个问题,它得到了解决,感谢gimpnet上的一个人的一些非常棒的帮助#python你必须保持你的菜单范围或它得到垃圾收集因此没有错误但没有菜单这是工作代码
from gi.repository import Gtk
class aStatusIcon:
def __init__(self):
self.statusicon = Gtk.StatusIcon()
self.statusicon.set_from_stock(Gtk.STOCK_HOME)
self.statusicon.connect("popup-menu", self.right_click_event)
window = Gtk.Window()
window.connect("destroy", lambda w: Gtk.main_quit())
window.show_all()
def right_click_event(self, icon, button, time):
self.menu = Gtk.Menu()
about = Gtk.MenuItem()
about.set_label("About")
quit = Gtk.MenuItem()
quit.set_label("Quit")
about.connect("activate", self.show_about_dialog)
quit.connect("activate", Gtk.main_quit)
self.menu.append(about)
self.menu.append(quit)
self.menu.show_all()
def pos(menu, icon):
return (Gtk.StatusIcon.position_menu(menu, icon))
self.menu.popup(None, None, pos, self.statusicon, button, time)
def show_about_dialog(self, widget):
about_dialog = Gtk.AboutDialog()
about_dialog.set_destroy_with_parent(True)
about_dialog.set_name("StatusIcon Example")
about_dialog.set_version("1.0")
about_dialog.set_authors(["Andrew Steele"])
about_dialog.run()
about_dialog.destroy()
aStatusIcon()
Gtk.main()
答案 1 :(得分:0)
从上面复制Mike's solution,对较新的gtk3进行一些小的清理和修复:
#!/usr/bin/python3
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MyStatusIconApp:
def __init__(self):
self.status_icon = Gtk.StatusIcon()
self.status_icon.set_from_stock(Gtk.STOCK_HOME)
self.status_icon.connect("popup-menu", self.right_click_event)
def right_click_event(self, icon, button, time):
self.menu = Gtk.Menu()
about = Gtk.MenuItem()
about.set_label("About")
about.connect("activate", self.show_about_dialog)
self.menu.append(about)
quit = Gtk.MenuItem()
quit.set_label("Quit")
quit.connect("activate", Gtk.main_quit)
self.menu.append(quit)
self.menu.show_all()
self.menu.popup(None, None, None, self.status_icon, button, time)
def show_about_dialog(self, widget):
about_dialog = Gtk.AboutDialog()
about_dialog.set_destroy_with_parent(True)
about_dialog.set_name("StatusIcon Example")
about_dialog.set_version("1.0")
about_dialog.set_authors(["Andrew Steele"])
about_dialog.run()
about_dialog.destroy()
app = MyStatusIconApp()
Gtk.main()
(如果gtk再次更改,请随意更新)