gpt4 book ai didi

python - 将 gtk3 菜单栏添加到主窗口

转载 作者:行者123 更新时间:2023-12-01 04:17:43 27 4
gpt4 key购买 nike

我对 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

我得到的不是菜单栏,而是两个窗口:shown here

请帮忙

编辑:来自 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有补充。在您的代码中,这意味着 MenuWindowGtk.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_changedon_menu_others功能。这些函数可能会对整个程序产生影响,因此您需要它们来访问对您的应用程序的某种反向引用,并可能访问其他 UI 元素,即将打包到 MainWindow 中的其他小部件。 .

一种可能的解决方案是传递对 MainWindow 的引用实例化菜单时的对象并保存它,以便回调可以在收到信号时使用它:

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架构。

关于python - 将 gtk3 菜单栏添加到主窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34116952/

27 4 0