【问题标题】:Putting other widgets in gtk.Menu将其他小部件放入 gtk.Menu
【发布时间】:2011-04-16 07:21:41
【问题描述】:

我希望能够在我的 gtk.Menu 中放置一个 gtk.ProgressBar,但是由于菜单只需要 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 抱怨。但是,我的进度条根本没有显示:

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

现在回答我的问题:

  1. 我如何知道需要哪些小部件?
  2. 如何欺骗它让我将其他小部件放入其中?

【问题讨论】:

    标签: python pygtk appindicator


    【解决方案1】:

    这是 Ubuntu 应用程序指标的限制,请参阅 this question at askubuntu

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    【解决方案2】:

    您的示例代码在这里有效(我通过修改我将在下面粘贴的 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()
    

    【讨论】:

    • 我正在制作一个应用程序,所以我想这可能是一个限制。 Here's the whole code 为子孙后代。
    猜你喜欢
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多