2

OK I figured out a way. If you have another way you can still give your answer. You may have an easier way. I'll be trying to format a reply for my answer in the meantime. The method I discovered is a little messy so I have to take my time. For now I will leave my answer unaccepted to see if someone else has a better way.

I'm using python with gtk3.

For gtk3 there is a menutoolbutton, but the description of it's usage in the documentation is not clear enough for me.

Besides that I would also like a plain button without the drop arrow for the toolbutton used for this.

If there is a way to do this with the gtk uimanager I would prefer that.

4

1 回答 1

0

这是我的答案。

这有点花很长的路要走,但它确实有效。

主要问题之一是我认为我可以在网格中使用最近的按钮,但 gtk 似乎只有工具栏和菜单的最近操作。因此,如果我还想要另一个带有弹出窗口的工具栏按钮,那将变得复杂,因为工具栏按钮不容易定制为没有下拉箭头。

长话短说。我用常规的弹出按钮走了很长一段路,并制作了一个网格来包含它和工具栏。

#the popup you see here will get used for the menu button
TOOLBAR_UI = """
<ui>
    <toolbar name="ToolBar">
        <toolitem action="FileOpen" />
        <toolitem action="FileSave"  />
        <toolitem action="FileSaveAs" />
        <toolitem action="Undo" />
        <toolitem action="Redo" />
    </toolbar>
    <popup name="Menu">
        <menuitem action="Bla" />
    </popup>
</ui>
"""
class GroupView(Gtk.Grid):

    def __init__(self):
        Gtk.Grid.__init__(self, orientation=Gtk.Orientation.HORIZONTAL)
        #the toolbar grid is placed in another grid to be
        #placed in a gtk.window
        group_toolbar = Toolbar()
        self.add(group_toolbar)


class Toolbar(Gtk.Grid):
    def __init__(self):
        Gtk.Grid.__init__(self, orientation=Gtk.Orientation.HORIZONTAL)
        toolbar = Tools()
        #toolbar is actually a grid with an actual toolbar
        #and the menu button is in the grid
        self.add(toolbar.display())
        self.add(toolbar.extra_menu)

class Tools(Gtk.UIManager):
    text_view = None
    def __init__(self):
        Gtk.UIManager.__init__(self)
        self.add_ui_from_string(TOOLBAR_UI)
        action_group = Gtk.ActionGroup("actions")
        self.add_toolbar_actions(action_group)

    def set_text_view(self, textview):
        self.textview = textview

    def add_toolbar_actions(self, action_group):
        self.insert_action_group(action_group)
        #
        #..normal toolbar construction here
        #

        #its a menu button so only the menu actions
        #need to be taken care of
        action_bla = Gtk.Action("Bla", 'bla', None, None)
        action_bla.connect("activate", self.action_bla_clicked)
        action_group.add_action(action_bla)
        #..the menu button is created here
        self.extra_menu = ExtraMenu()
        self.extra_menu.set_popup(self.get_widget("/Menu"))

        #start action callbacks
        #...placeholder for callbacks
        #end action callbacks
    #get ready to export the toolbar
    def display(self):
        toolbar = self.get_widget("/ToolBar")
        toolbar.set_hexpand(True)
        return toolbar

class ExtraMenu(Gtk.MenuButton):
    def __init__(self):
        Gtk.MenuButton.__init__(self)
        arrow = self.get_children()[0]
        #the arrow is destroyed and a custom image is placed in the button
        arrow.destroy()
        self.add(Gtk.Image.new_from_stock(Gtk.STOCK_EXECUTE, Gtk.IconSize.BUTTON))

如果您尝试上面的代码,请不要忘记添加一个窗口和一些操作回调。如果你不能告诉我我喜欢继承 gtk 很多。:)

于 2013-04-09T17:24:51.053 回答