我是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
请帮助
编辑:来自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
答案 0 :(得分:2)
重要的一点是,在撰写类似class SubClass(BaseClass)
的内容时,您延伸 BaseClass
,这意味着SubClass
一个BaseClass
添加。在您的代码中,这意味着MenuWindow
是Gtk.Window
(UI的一个独立矩形区域,带有标题栏,可能还有一些小部件)。实际上,您并不想将窗口打包到另一个窗口。您想将菜单栏打包到主窗口中。
我的理解是你想在自己的类中封装与菜单相关的代码,但是你不需要这个类本身就是一个小部件。为此,您通常不需要继承任何内容(*)。但是,由于您的类可能会操纵GObject信号,因此您至少应该从GObject.GObject派生。
MainWindow
需要的只是一个UIManager。但是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
函数。这些函数可能会对整个程序产生影响,因此您需要它们访问应用程序的某种反向引用,并可能访问其他UI元素,即将打包到{{1}中的其他小部件}。
一种可能的解决方案是在实例化菜单时传递对MainWindow
对象的引用并保存,以便回调可以在发出信号时使用它:
MainWindow
也就是说,当您开始处理更大的应用程序时,切换到更强大的软件设计(如Model-View-Controller架构)会更容易,更清晰。