将 gtk3 菜单栏添加到主菜单 window
add gtk3 menubar to main window
我是 gtk3 的新手,可能也搞砸了我的 python。
在我的 menu.py
中,我在 xml 中定义了我的菜单栏:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, Pango
UI_INFO = """
<ui>
<menubar name='MenuBar'>
<menu action='FileNew'>
<menuitem action='FileNewStandard' />
<menuitem action='FileOpenStandard' />
<menuitem action='FileQuit' />
</menu>
<menu action='EditMenu'>
<menuitem action='EditCopy' />
<menuitem action='EditPaste' />
</menu>
<menu action='ChoicesMenu'>
<menuitem action='Book'/>
<menuitem action='Wine'/>
</menu>
</menubar>
<popup name='PopupMenu'>
<menuitem action='EditCopy' />
<menuitem action='EditPaste' />
</popup>
</ui>
"""
class MenuWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Menu Example")
action_group = Gtk.ActionGroup("my_actions")
self.add_file_menu_actions(action_group)
self.add_edit_menu_actions(action_group)
self.add_choices_menu_actions(action_group)
uimanager = self.create_ui_manager()
uimanager.insert_action_group(action_group)
menubar = uimanager.get_widget("/MenuBar")
# button = Gtk.Button("Open") # Submit button to write to
# button.connect("clicked", self.on_button_clicked)
def add_file_menu_actions(self, action_group):
action_filenewmenu = Gtk.Action("FileNew", None, None, Gtk.STOCK_NEW)
action_group.add_action(action_filenewmenu)
action_new = Gtk.Action("FileNewStandard", "_New",
"Create a new file", Gtk.STOCK_NEW)
# action_new.connect("activate", self.on_menu_file_new_generic)
action_group.add_action_with_accel(action_new, None)
action_fileopen = Gtk.Action("FileOpen", None, None, Gtk.STOCK_OPEN)
action_group.add_action(action_fileopen)
action_open = Gtk.Action("FileOpenStandard", "_Open",
"Open an existing file", Gtk.STOCK_OPEN)
# action_open.connect("activate", self.file_open_clicked)
action_group.add_action_with_accel(action_open, None)
action_filequit = Gtk.Action("FileQuit", None, None, Gtk.STOCK_QUIT)
# action_filequit.connect("activate", self.on_menu_file_quit)
action_group.add_action(action_filequit)
def add_edit_menu_actions(self, action_group):
action_group.add_actions([
("EditMenu", None, "Edit"),
("EditCopy", Gtk.STOCK_COPY, None, None, None,
self.on_menu_others),
("EditPaste", Gtk.STOCK_PASTE, None, None, None,
self.on_menu_others),
])
def add_choices_menu_actions(self, action_group):
action_group.add_action(Gtk.Action("ChoicesMenu", "Choices", None,
None))
action_group.add_radio_actions([
("Book", None, "Book", None, None, 1),
("Wine", None, "Wine", None, None, 2)
], 1, self.on_menu_choices_changed)
def create_ui_manager(self):
uimanager = Gtk.UIManager()
# Throws exception if something went wrong
uimanager.add_ui_from_string(UI_INFO)
# Add the accelerator group to the toplevel window
accelgroup = uimanager.get_accel_group()
self.add_accel_group(accelgroup)
return uimanager
我在我的 main
函数中将其称为:
#!/usr/bin/python3
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk
import menu
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Collection Manager")
self.set_default_size(1000, 20)
self.set_border_width(10)
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
MenuElem = menu.MenuWindow()
box.pack_start(MenuElem, False, False, 0)
self.add(box)
window = MainWindow()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()
我一定是做错了什么,因为我得到了错误:
python3 main2.py
(main2.py:15800): Gtk-WARNING **: Can't set a parent on a toplevel widget
(main2.py:15800): Gtk-CRITICAL **: gtk_widget_destroy: assertion 'GTK_IS_WIDGET (widget)' failed
我得到两个 window 而不是菜单栏:
请帮忙
编辑:来自 Cilyan 的回复
我收到错误:
python3 main_mini.py
Traceback (most recent call last):
File "main_mini.py", line 24, in <module>
window = MainWindow()
File "main_mini.py", line 15, in __init__
MenuElem = menu.MenuManager()
File "/home/rudra/Devel/Cmanage/menu.py", line 32, in __init__
super.__init__()
TypeError: descriptor '__init__' of 'super' object needs an argument
解释 Blabla
重要的一点是,当写类似 class SubClass(BaseClass)
的内容时,您 扩展 BaseClass
,这意味着 SubClass
是一个BaseClass
加上了。在您的代码中,这意味着 MenuWindow
是一个 Gtk.Window
(UI 的分离矩形区域,带有标题栏和可能的一些小部件)。实际上,您不想将 window 打包到另一个 window 中。您想将菜单栏打包到主 window.
我的理解是你想把菜单相关的代码封装在它自己的class中,但你不需要这个class本身就是一个widget。为此,您通常不需要继承任何东西(*). However, as your class will probably manipulate GObject signals, you should at least derive from GObject.GObject。
实用的解决方案
不过,我采用了另一种方法。您的 MainWindow
需要的只是一个 UI 经理。但是 UIManager 会根据应用程序的目的专门调整大小,这样 MainWindow
代码就不会被菜单相关任务弄得乱七八糟。我选择直接继承 Gtk.UIManager
并扩展它以包含应用程序所需的一切,并为应用程序创建某种专门的 UIManager。这是我从 MenuWindow
更改的内容:
# New base class, new name to better define what it really is (optional)
# Now, our object will be UIManager by itself, and we can extend it
class MenuManager(Gtk.UIManager):
def __init__(self):
# Use super, it's much easier and flexible
super().__init__()
action_group = Gtk.ActionGroup("my_actions")
self.add_file_menu_actions(action_group)
self.add_edit_menu_actions(action_group)
self.add_choices_menu_actions(action_group)
# This code comes from the create_ui_manager function.
# No need to instanciate a Gtk.UIManager, self is the UIManager already
self.add_ui_from_string(UI_INFO)
self.insert_action_group(action_group)
然后,除了删除 create_ui_manager
之外,我什么也没做。现在,集成到 MainWindow
中略有不同:
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Collection Manager")
self.set_default_size(1000, 20)
self.set_border_width(10)
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
# Don't forget to change the class's name
MenuElem = menu.MenuManager()
# Code moved from MenuWindow's __init__ function
menubar = MenuElem.get_widget("/MenuBar")
# Now you really pack the menubar widget, not a window containing a menu
box.pack_start(menubar, False, False, 0)
self.add(box)
# Come from create_ui_manager. The AccelGroup must be added to the main window, not to the UIManager itself
self.add_accel_group(MenuElem.get_accel_group())
还有一件事
您可能还需要做最后一件事,但我无法查看,因为您没有为其提供代码:on_menu_choices_changed
和 on_menu_others
函数。这些功能可能会对整个程序产生影响,因此您需要它们访问某种 back-reference 到您的应用程序,并可能访问其他 UI 元素,即其他将被打包的小部件进入 MainWindow
.
一个可能的解决方案是在实例化菜单时传递对 MainWindow
object 的引用并保存它,以便回调在发出信号时可以使用它:
class MenuManager(Gtk.UIManager):
def __init__(self, mainwindow):
super().__init__()
self.mainwindow = mainwindow
# ...
def on_menu_choices_changed(self, widget, current):
# For example
if current.get_name() == "Wine":
self.mainwindow.set_icon_to_glass_of_wine()
else:
self.mainwindow.set_icon_to_book()
# ...
class MainWindow(Gtk.Window):
def __init__(self):
# ...
MenuElem = menu.MenuManager(self)
# ...
也就是说,当您开始处理更大的应用程序时,切换到更强大的软件设计(例如 Model-View-Controller 架构)会更容易、更清晰。
我是 gtk3 的新手,可能也搞砸了我的 python。
在我的 menu.py
中,我在 xml 中定义了我的菜单栏:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, Pango
UI_INFO = """
<ui>
<menubar name='MenuBar'>
<menu action='FileNew'>
<menuitem action='FileNewStandard' />
<menuitem action='FileOpenStandard' />
<menuitem action='FileQuit' />
</menu>
<menu action='EditMenu'>
<menuitem action='EditCopy' />
<menuitem action='EditPaste' />
</menu>
<menu action='ChoicesMenu'>
<menuitem action='Book'/>
<menuitem action='Wine'/>
</menu>
</menubar>
<popup name='PopupMenu'>
<menuitem action='EditCopy' />
<menuitem action='EditPaste' />
</popup>
</ui>
"""
class MenuWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Menu Example")
action_group = Gtk.ActionGroup("my_actions")
self.add_file_menu_actions(action_group)
self.add_edit_menu_actions(action_group)
self.add_choices_menu_actions(action_group)
uimanager = self.create_ui_manager()
uimanager.insert_action_group(action_group)
menubar = uimanager.get_widget("/MenuBar")
# button = Gtk.Button("Open") # Submit button to write to
# button.connect("clicked", self.on_button_clicked)
def add_file_menu_actions(self, action_group):
action_filenewmenu = Gtk.Action("FileNew", None, None, Gtk.STOCK_NEW)
action_group.add_action(action_filenewmenu)
action_new = Gtk.Action("FileNewStandard", "_New",
"Create a new file", Gtk.STOCK_NEW)
# action_new.connect("activate", self.on_menu_file_new_generic)
action_group.add_action_with_accel(action_new, None)
action_fileopen = Gtk.Action("FileOpen", None, None, Gtk.STOCK_OPEN)
action_group.add_action(action_fileopen)
action_open = Gtk.Action("FileOpenStandard", "_Open",
"Open an existing file", Gtk.STOCK_OPEN)
# action_open.connect("activate", self.file_open_clicked)
action_group.add_action_with_accel(action_open, None)
action_filequit = Gtk.Action("FileQuit", None, None, Gtk.STOCK_QUIT)
# action_filequit.connect("activate", self.on_menu_file_quit)
action_group.add_action(action_filequit)
def add_edit_menu_actions(self, action_group):
action_group.add_actions([
("EditMenu", None, "Edit"),
("EditCopy", Gtk.STOCK_COPY, None, None, None,
self.on_menu_others),
("EditPaste", Gtk.STOCK_PASTE, None, None, None,
self.on_menu_others),
])
def add_choices_menu_actions(self, action_group):
action_group.add_action(Gtk.Action("ChoicesMenu", "Choices", None,
None))
action_group.add_radio_actions([
("Book", None, "Book", None, None, 1),
("Wine", None, "Wine", None, None, 2)
], 1, self.on_menu_choices_changed)
def create_ui_manager(self):
uimanager = Gtk.UIManager()
# Throws exception if something went wrong
uimanager.add_ui_from_string(UI_INFO)
# Add the accelerator group to the toplevel window
accelgroup = uimanager.get_accel_group()
self.add_accel_group(accelgroup)
return uimanager
我在我的 main
函数中将其称为:
#!/usr/bin/python3
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk
import menu
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Collection Manager")
self.set_default_size(1000, 20)
self.set_border_width(10)
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
MenuElem = menu.MenuWindow()
box.pack_start(MenuElem, False, False, 0)
self.add(box)
window = MainWindow()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()
我一定是做错了什么,因为我得到了错误:
python3 main2.py
(main2.py:15800): Gtk-WARNING **: Can't set a parent on a toplevel widget
(main2.py:15800): Gtk-CRITICAL **: gtk_widget_destroy: assertion 'GTK_IS_WIDGET (widget)' failed
我得到两个 window 而不是菜单栏:
请帮忙
编辑:来自 Cilyan 的回复 我收到错误:
python3 main_mini.py
Traceback (most recent call last):
File "main_mini.py", line 24, in <module>
window = MainWindow()
File "main_mini.py", line 15, in __init__
MenuElem = menu.MenuManager()
File "/home/rudra/Devel/Cmanage/menu.py", line 32, in __init__
super.__init__()
TypeError: descriptor '__init__' of 'super' object needs an argument
解释 Blabla
重要的一点是,当写类似 class SubClass(BaseClass)
的内容时,您 扩展 BaseClass
,这意味着 SubClass
是一个BaseClass
加上了。在您的代码中,这意味着 MenuWindow
是一个 Gtk.Window
(UI 的分离矩形区域,带有标题栏和可能的一些小部件)。实际上,您不想将 window 打包到另一个 window 中。您想将菜单栏打包到主 window.
我的理解是你想把菜单相关的代码封装在它自己的class中,但你不需要这个class本身就是一个widget。为此,您通常不需要继承任何东西(*). However, as your class will probably manipulate GObject signals, you should at least derive from GObject.GObject。
实用的解决方案
不过,我采用了另一种方法。您的 MainWindow
需要的只是一个 UI 经理。但是 UIManager 会根据应用程序的目的专门调整大小,这样 MainWindow
代码就不会被菜单相关任务弄得乱七八糟。我选择直接继承 Gtk.UIManager
并扩展它以包含应用程序所需的一切,并为应用程序创建某种专门的 UIManager。这是我从 MenuWindow
更改的内容:
# New base class, new name to better define what it really is (optional)
# Now, our object will be UIManager by itself, and we can extend it
class MenuManager(Gtk.UIManager):
def __init__(self):
# Use super, it's much easier and flexible
super().__init__()
action_group = Gtk.ActionGroup("my_actions")
self.add_file_menu_actions(action_group)
self.add_edit_menu_actions(action_group)
self.add_choices_menu_actions(action_group)
# This code comes from the create_ui_manager function.
# No need to instanciate a Gtk.UIManager, self is the UIManager already
self.add_ui_from_string(UI_INFO)
self.insert_action_group(action_group)
然后,除了删除 create_ui_manager
之外,我什么也没做。现在,集成到 MainWindow
中略有不同:
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Collection Manager")
self.set_default_size(1000, 20)
self.set_border_width(10)
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
# Don't forget to change the class's name
MenuElem = menu.MenuManager()
# Code moved from MenuWindow's __init__ function
menubar = MenuElem.get_widget("/MenuBar")
# Now you really pack the menubar widget, not a window containing a menu
box.pack_start(menubar, False, False, 0)
self.add(box)
# Come from create_ui_manager. The AccelGroup must be added to the main window, not to the UIManager itself
self.add_accel_group(MenuElem.get_accel_group())
还有一件事
您可能还需要做最后一件事,但我无法查看,因为您没有为其提供代码:on_menu_choices_changed
和 on_menu_others
函数。这些功能可能会对整个程序产生影响,因此您需要它们访问某种 back-reference 到您的应用程序,并可能访问其他 UI 元素,即其他将被打包的小部件进入 MainWindow
.
一个可能的解决方案是在实例化菜单时传递对 MainWindow
object 的引用并保存它,以便回调在发出信号时可以使用它:
class MenuManager(Gtk.UIManager):
def __init__(self, mainwindow):
super().__init__()
self.mainwindow = mainwindow
# ...
def on_menu_choices_changed(self, widget, current):
# For example
if current.get_name() == "Wine":
self.mainwindow.set_icon_to_glass_of_wine()
else:
self.mainwindow.set_icon_to_book()
# ...
class MainWindow(Gtk.Window):
def __init__(self):
# ...
MenuElem = menu.MenuManager(self)
# ...
也就是说,当您开始处理更大的应用程序时,切换到更强大的软件设计(例如 Model-View-Controller 架构)会更容易、更清晰。