gpt4 book ai didi

python - Gio SimpleAction 调用函数

转载 作者:太空宇宙 更新时间:2023-11-04 03:19:11 26 4
gpt4 key购买 nike

我在 Gtk3 应用程序中使用 Gio 操作制作了菜单。菜单项创建为:

#in main file
MenuElem = menu.MenuManager
# Open Menu
action = Gio.SimpleAction(name="open")
action.connect("activate", MenuElem.file_open_clicked)
self.add_action(action)

file_open_clickedmenu.py中,class MenuManager,定义为:

import gi
import pybib
import view
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk


class MenuManager:
def __init__(self):
self.parsing = pybib.parser()
self.TreeView = view.treeview()
#file_open_clicked
#in menu.py
def file_open_clicked(self, widget):
dialog = Gtk.FileChooserDialog("Open an existing fine", None,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL,
Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
response = dialog.run()
if response == Gtk.ResponseType.OK:
filename = dialog.get_filename()
dialog.destroy()
self.TreeView.bookstore.clear()
self.TreeView.viewer(self.parsing.booklist)
# self.TreeView.view.set_model()
elif response == Gtk.ResponseType.CANCEL:
print("Cancel clicked")
dialog.destroy()

我遇到错误:

Traceback (most recent call last):
File "/home/rudra/Devel/mkbib/Python/src/menu.py", line 81, in file_open_clicked
self.TreeView.bookstore.clear()
AttributeError: 'SimpleAction' object has no attribute 'TreeView'

我知道 SimpleAction 还有一个选项,应该调用 TreeView。但我不知道如何。请帮忙

最佳答案

让我为您分解代码。

#in main file
MenuElem = menu.MenuManager

在这里,您将 MenuElem 设置为指向 menu.MenuManager 类。您可能打算在此处初始化对象,使 MenuElem 成为 menu.MenuManager 类的实例。这样就调用了 MenuManager 类的 __init__ 函数。因此代码应该是:

#in main file
MenuElem = menu.MenuManager()

接下来出错的部分就在这里:

def file_open_clicked(self, widget):

如果我们检查 docs对于 activate 信号,我们看到它有 2 个参数。所以目前没有初始化对象 self 被设置为第一个参数,即 SimpleAction 并且 widget 被设置为激活 parameter.

但由于我们现在已经初始化了 MenuManager 对象,file_open_clicked 函数将获得 3 个输入参数,即 selfSimpleAction参数。因此我们需要像这样接受它们:

def file_open_clicked(self, simpleAction, parameter):

现在代码可以工作了,因为 self 实际上是一个具有属性 TreeView 的对象。 (仅供引用,Python 变量和属性通常以小写形式书写)

关于python - Gio SimpleAction 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35235446/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com