I have made menu using Gio action in a Gtk3 app. The menu item is created as:
#in main file
MenuElem = menu.MenuManager
# Open Menu
action = Gio.SimpleAction(name="open")
action.connect("activate", MenuElem.file_open_clicked)
self.add_action(action)
The file_open_clicked
is in menu.py
, class MenuManager
, defined as:
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()
I am getting error:
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'
I know SimpleAction
takes one more option, and TreeView
should be called.
But I dont know how.
Kindly help
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.MenuManager
class. 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)