2

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もう1つのオプションが必要であり、呼び出される必要があることはわかっていますTreeView。しかし、方法がわかりません。親切に助けて

4

2 に答える 2

2

Let me break down your code for you.

#in main file
MenuElem = menu.MenuManager

Here you set MenuElem to point to menu.MenuManager class. Probably you meant to initialize the object here such that MenuElem become an instance of the menu.MenuManagerclass. Such that the __init__ function of the MenuManager class was called. Thus the code should be:

#in main file
MenuElem = menu.MenuManager()

Then the next part where something goes wrong is in here:

def file_open_clicked(self, widget):

If we check the docs for the activate signal we see that it has 2 parameters. So currently without initializing the object self is set to the first parameter namely the SimpleAction and the widget is set to the activation parameter.

But as we now have initialized the MenuManager object, the file_open_clicked function will get 3 input parameters namely self, SimpleAction and parameter. Thus we need to accept them all like this:

def file_open_clicked(self, simpleAction, parameter):

Now the code will work as self is actually an object with the attribute TreeView. (Just for your information in Python variables and attributes are normally written in lowercase)

于 2016-02-06T10:50:52.237 に答える