pygtk 系统托盘图标不起作用

发布于 2024-10-21 11:24:08 字数 1085 浏览 1 评论 0原文

我正在尝试在我的程序中显示系统托盘图标。 当我启动程序时,它显示窗口,当我关闭窗口时,它会隐藏。 然后,如果我单击系统托盘图标,它会显示一个空白窗口,但不显示窗口的内容。为什么会发生这种情况? 这是我的代码:

class Main(gtk.Window):

    def __init__(self):
        super(Main,self).__init__()
        self.set_title("Virtual Machine Monitor")
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_default_size(640,600)
        self.set_geometry_hints(min_width=640,min_height=600)
        self.set_icon_from_file("../images/activity_monitor2.png")
        self.connect("destroy",self.window_destroy)
        menubar = self.add_menubar()

        pixbuf = gdk.pixbuf_new_from_file_at_size("../images/activity_monitor2.png",25,25)
        statusicon = gtk.StatusIcon()
        statusicon = gtk.status_icon_new_from_pixbuf(pixbuf)
        statusicon.connect("activate",self.tray_activate)
        self.show_all()

    def tray_activate(self,widget):
        self.show_all()

    def window_destroy(self,widget):
        self.hide_all()

if __name__ == "__main__":
    Main()
    gtk.main()

当我单击系统托盘图标时,它会显示窗口,但是是一个空白窗口。

所以请帮帮我。 提前致谢。

i am trying to display system tray icon in my program.
when i start my program it shows window and when i colse window it gets hidden.
Then if i click on system tray icon it shows me a blank window, but not the contents of window.Why is this happen?
Heres the my code:

class Main(gtk.Window):

    def __init__(self):
        super(Main,self).__init__()
        self.set_title("Virtual Machine Monitor")
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_default_size(640,600)
        self.set_geometry_hints(min_width=640,min_height=600)
        self.set_icon_from_file("../images/activity_monitor2.png")
        self.connect("destroy",self.window_destroy)
        menubar = self.add_menubar()

        pixbuf = gdk.pixbuf_new_from_file_at_size("../images/activity_monitor2.png",25,25)
        statusicon = gtk.StatusIcon()
        statusicon = gtk.status_icon_new_from_pixbuf(pixbuf)
        statusicon.connect("activate",self.tray_activate)
        self.show_all()

    def tray_activate(self,widget):
        self.show_all()

    def window_destroy(self,widget):
        self.hide_all()

if __name__ == "__main__":
    Main()
    gtk.main()

when i click on system tray icon it shows me window , but a blank window.

So please help me out.
Thanks in advanced.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸里的快感 2024-10-28 11:24:08

您必须处理delete-event才能隐藏窗口而不破坏它(默认情况下)。

class Main(gtk.Window):

    def __init__(self):
        super(Main, self).__init__()
        self.connect('delete-event', self.on_delete_event)
        self.set_title("Virtual Machine Monitor")
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_default_size(640,600)
        self.set_geometry_hints(min_width=640, min_height=600)
        self.set_icon_from_file("../images/activity_monitor2.png")
        menubar = self.add_menubar()

        pixbuf = gdk.pixbuf_new_from_file_at_size("../images/activity_monitor2.png",25,25)
        statusicon = gtk.StatusIcon()
        statusicon = gtk.status_icon_new_from_pixbuf(pixbuf)
        statusicon.connect("activate",self.tray_activate)
        self.show_all()

    def on_delete_event(self, widget, event):
        self.hide()
        return True    

    def tray_activate(self, widget):
        self.present()


if __name__ == "__main__":
    Main()
    gtk.main()

You must handle delete-event to just hide the window and don't destroy that (as by default).

class Main(gtk.Window):

    def __init__(self):
        super(Main, self).__init__()
        self.connect('delete-event', self.on_delete_event)
        self.set_title("Virtual Machine Monitor")
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_default_size(640,600)
        self.set_geometry_hints(min_width=640, min_height=600)
        self.set_icon_from_file("../images/activity_monitor2.png")
        menubar = self.add_menubar()

        pixbuf = gdk.pixbuf_new_from_file_at_size("../images/activity_monitor2.png",25,25)
        statusicon = gtk.StatusIcon()
        statusicon = gtk.status_icon_new_from_pixbuf(pixbuf)
        statusicon.connect("activate",self.tray_activate)
        self.show_all()

    def on_delete_event(self, widget, event):
        self.hide()
        return True    

    def tray_activate(self, widget):
        self.present()


if __name__ == "__main__":
    Main()
    gtk.main()
给妤﹃绝世温柔 2024-10-28 11:24:08

我在你的窗口上没有看到任何控件,我猜你只是从代码片段中删除了这段代码。看看下面的代码是否适合您。每次单击托盘图标时,它都会打开一个窗口。还为托盘注册了一个弹出菜单。

import sys
import gtk

class MainWindow(gtk.Window):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.set_title("Virtual Machine Monitor")
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_default_size(640, 600)
        self.set_geometry_hints(min_width=640, min_height=600)
        self.set_icon_from_file("../images/activity_monitor2.png")
        self.connect("destroy", self.window_destroy)

        box = gtk.VBox()
        button = gtk.Button("Test Button")
        box.pack_start(button, False)
        self.add(box)

        self.show_all()

    def window_destroy(self,widget):
        self.hide_all()

class TrayIcon(gtk.StatusIcon):
    def __init__(self):
        gtk.StatusIcon.__init__(self)

        # using a stock icon, load your pixmap here
        self.set_from_stock(gtk.STOCK_FIND)
        self.set_tooltip('Tracker Desktop Search')
        self.set_visible(True)

        self.menu = menu = gtk.Menu()

        window_item = gtk.MenuItem("Show Window")
        window_item.connect("activate", self.show_window, "about")
        menu.append(window_item)

        quit_item = gtk.MenuItem("Quit")
        quit_item.connect("activate", self.quit, "file.quit")
        menu.append(quit_item)
        menu.show_all()

        self.connect("activate", self.show_window)
        self.connect('popup-menu', self.icon_clicked)

    def show_window(self, widget, event=None):
        MainWindow()    

    def icon_clicked(self, status, button, time):
        self.menu.popup(None, None, None, button, time)

    def quit(self, widget, event=None):
        sys.exit(0)

if __name__ == "__main__":
    TrayIcon() 
    gtk.main()

I don't see any controls on your window, I guess you just cut out this code from your snippet. See if the code below would work for you. It should open a window every time you click on the tray icon. Also a popup menu gets registered for the tray.

import sys
import gtk

class MainWindow(gtk.Window):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.set_title("Virtual Machine Monitor")
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_default_size(640, 600)
        self.set_geometry_hints(min_width=640, min_height=600)
        self.set_icon_from_file("../images/activity_monitor2.png")
        self.connect("destroy", self.window_destroy)

        box = gtk.VBox()
        button = gtk.Button("Test Button")
        box.pack_start(button, False)
        self.add(box)

        self.show_all()

    def window_destroy(self,widget):
        self.hide_all()

class TrayIcon(gtk.StatusIcon):
    def __init__(self):
        gtk.StatusIcon.__init__(self)

        # using a stock icon, load your pixmap here
        self.set_from_stock(gtk.STOCK_FIND)
        self.set_tooltip('Tracker Desktop Search')
        self.set_visible(True)

        self.menu = menu = gtk.Menu()

        window_item = gtk.MenuItem("Show Window")
        window_item.connect("activate", self.show_window, "about")
        menu.append(window_item)

        quit_item = gtk.MenuItem("Quit")
        quit_item.connect("activate", self.quit, "file.quit")
        menu.append(quit_item)
        menu.show_all()

        self.connect("activate", self.show_window)
        self.connect('popup-menu', self.icon_clicked)

    def show_window(self, widget, event=None):
        MainWindow()    

    def icon_clicked(self, status, button, time):
        self.menu.popup(None, None, None, button, time)

    def quit(self, widget, event=None):
        sys.exit(0)

if __name__ == "__main__":
    TrayIcon() 
    gtk.main()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文