将其他小部件放入 gtk.Menu 中

发布于 2024-11-01 13:54:23 字数 594 浏览 1 评论 0原文

我希望能够将 gtk.ProgressBar 放入我的 gtk.Menu 中,但由于菜单仅采用 gtk.MenuItems 及其子类,所以我所做的是采用普通的 gtk.MenuItem 并尝试添加我的进度栏作为一个孩子。由于 gtk.MenuItem 是 gtk.Bin 的子类,因此它应该能够容纳几乎任何小部件。

示例:

menu = gtk.Menu()

item = gtk.MenuItem()

button = gtk.ProgressBar()
button.pulse()
button.show()

item.add(button)
item.show()

menu.append(item)

这运行得很好,pygtk 根本没有抱怨。但是,我的进度条根本不显示:

Screenshot of gtk.Menu

如果我用 gtk.Label 替换进度条,它显示得很好。

现在我的问题是:

  1. 我如何知道它将使用哪些小部件?
  2. 我如何欺骗它让我把其他小部件放在那里?

I'd like to be able to put a gtk.ProgressBar in my gtk.Menu, but since menus only takes gtk.MenuItems and its subclasses, what I've done instead is take a plain gtk.MenuItem and tried adding my progress bar as a child to that. Since gtk.MenuItem is a subclass of gtk.Bin, it should be able to hold pretty much any widget.

Example:

menu = gtk.Menu()

item = gtk.MenuItem()

button = gtk.ProgressBar()
button.pulse()
button.show()

item.add(button)
item.show()

menu.append(item)

This runs just fine without pygtk complaining at all. However, my progress bar is simply not shown:

Screenshot of gtk.Menu

If I replace the progressbar with a gtk.Label, it's shown just fine.

Now to my questions:

  1. How do I know which widgets it will take?
  2. How do I trick it into letting me put other widgets in there?

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

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

发布评论

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

评论(2

甜味超标? 2024-11-08 13:54:23

这是 Ubuntu 的应用程序指示器的限制,请参阅 askubuntu 上的此问题

This is a limitation of Ubuntu's Application Indicators, see this question at askubuntu.

执手闯天涯 2024-11-08 13:54:23

您的示例代码在这里工作(我通过修改将在下面粘贴的 pygtk 示例来测试它)。

也许是你的其余代码或主题的问题?

在此处输入图像描述

#!/usr/bin/env python

# example menu.py

import pygtk
pygtk.require('2.0')
import gtk

class MenuExample:
    def __init__(self):
        # create a new window
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_size_request(200, 100)
        window.set_title("GTK Menu Test")
        window.connect("delete_event", lambda w,e: gtk.main_quit())

        # Init the menu-widget, and remember -- never
        # show() the menu widget!! 
        # This is the menu that holds the menu items, the one that
        # will pop up when you click on the "Root Menu" in the app
        menu = gtk.Menu()

        ### MODIFIED PART!! ###
        item = gtk.MenuItem()

        button = gtk.ProgressBar()
        button.pulse()
        button.show()

        item.add(button)
        item.show()

        menu.append(item)
        #### END MODIFIED PART ####

        # This is the root menu, and will be the label
        # displayed on the menu bar.  There won't be a signal handler attached,
        # as it only pops up the rest of the menu when pressed.
        root_menu = gtk.MenuItem("Root Menu")

        root_menu.show()

        # Now we specify that we want our newly created "menu" to be the
        # menu for the "root menu"
        root_menu.set_submenu(menu)

        # A vbox to put a menu and a button in:
        vbox = gtk.VBox(False, 0)
        window.add(vbox)
        vbox.show()

        # Create a menu-bar to hold the menus and add it to our main window
        menu_bar = gtk.MenuBar()
        vbox.pack_start(menu_bar, False, False, 2)
        menu_bar.show()

        # Create a button to which to attach menu as a popup
        button = gtk.Button("press me")
        button.connect_object("event", self.button_press, menu)
        vbox.pack_end(button, True, True, 2)
        button.show()

        # And finally we append the menu-item to the menu-bar -- this is the
        # "root" menu-item I have been raving about =)
        menu_bar.append (root_menu)

        # always display the window as the last step so it all splashes on
        # the screen at once.
        window.show()

    # Respond to a button-press by posting a menu passed in as widget.
    #
    # Note that the "widget" argument is the menu being posted, NOT
    # the button that was pressed.
    def button_press(self, widget, event):
        if event.type == gtk.gdk.BUTTON_PRESS:
            widget.popup(None, None, None, event.button, event.time)
            # Tell calling code that we have handled this event the buck
            # stops here.
            return True
        # Tell calling code that we have not handled this event pass it on.
        return False

    # Print a string when a menu item is selected
    def menuitem_response(self, widget, string):
        print "%s" % string

def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    MenuExample()
    main()

Your example code works here(I tested it by modifying a pygtk example that I will paste below).

Maybe its an issue with the rest of your code or your theme?

enter image description here

#!/usr/bin/env python

# example menu.py

import pygtk
pygtk.require('2.0')
import gtk

class MenuExample:
    def __init__(self):
        # create a new window
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_size_request(200, 100)
        window.set_title("GTK Menu Test")
        window.connect("delete_event", lambda w,e: gtk.main_quit())

        # Init the menu-widget, and remember -- never
        # show() the menu widget!! 
        # This is the menu that holds the menu items, the one that
        # will pop up when you click on the "Root Menu" in the app
        menu = gtk.Menu()

        ### MODIFIED PART!! ###
        item = gtk.MenuItem()

        button = gtk.ProgressBar()
        button.pulse()
        button.show()

        item.add(button)
        item.show()

        menu.append(item)
        #### END MODIFIED PART ####

        # This is the root menu, and will be the label
        # displayed on the menu bar.  There won't be a signal handler attached,
        # as it only pops up the rest of the menu when pressed.
        root_menu = gtk.MenuItem("Root Menu")

        root_menu.show()

        # Now we specify that we want our newly created "menu" to be the
        # menu for the "root menu"
        root_menu.set_submenu(menu)

        # A vbox to put a menu and a button in:
        vbox = gtk.VBox(False, 0)
        window.add(vbox)
        vbox.show()

        # Create a menu-bar to hold the menus and add it to our main window
        menu_bar = gtk.MenuBar()
        vbox.pack_start(menu_bar, False, False, 2)
        menu_bar.show()

        # Create a button to which to attach menu as a popup
        button = gtk.Button("press me")
        button.connect_object("event", self.button_press, menu)
        vbox.pack_end(button, True, True, 2)
        button.show()

        # And finally we append the menu-item to the menu-bar -- this is the
        # "root" menu-item I have been raving about =)
        menu_bar.append (root_menu)

        # always display the window as the last step so it all splashes on
        # the screen at once.
        window.show()

    # Respond to a button-press by posting a menu passed in as widget.
    #
    # Note that the "widget" argument is the menu being posted, NOT
    # the button that was pressed.
    def button_press(self, widget, event):
        if event.type == gtk.gdk.BUTTON_PRESS:
            widget.popup(None, None, None, event.button, event.time)
            # Tell calling code that we have handled this event the buck
            # stops here.
            return True
        # Tell calling code that we have not handled this event pass it on.
        return False

    # Print a string when a menu item is selected
    def menuitem_response(self, widget, string):
        print "%s" % string

def main():
    gtk.main()
    return 0

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