Topics

Calling functions

Rui Fontes
 

Hello!


I am creating an add-on and I have the following:


class GlobalPlugin(globalPluginHandler.GlobalPlugin):
    def __init__(self):
        super(globalPluginHandler.GlobalPlugin, self).__init__()

    __gestures={
        "kb:NVDA+x": "something",
    }

  def script_something(self, gesture):
    Do something!

    self.function1()

  def function1(self):
    Do more something...
    self.function3()

  def function3(self):

    Do yet more one thing...

self.function4()

  def function4(self):
    Almost done...
    self.function5()

  def function6(self):
    At last, all ready!
    gui.mainFrame._popupSettingsDialog(MyDialog)

class MyDialog(wx.Dialog):
  _instance = None
  def __new__(cls, *args, **kwargs):
    global variables
    if MyDialog._instance is None:
      return wx.Dialog.__new__(cls)
      return MyDialog._instance

  def __init__ (self, parent):
    MyDialog._instance = self
    self.title = title
    super (spellCheckerDialog, self).__init__(parent, wx.ID_ANY, title)
    self.makeSettings()

  def makeSettings(self):


And there one of the functions bind to a button is:


  def onIgnoreAll(self,evt):
    candidates.append(newWord)
    GlobalPlugin.function5(self = GlobalPlugin)

And my problem is:

After running function5, calling function6 gaves an error...


  File "C:\Users\Admin\AppData\Roaming\nvda\addons\MyAddon\globalPlugins\MyAddon.py", line 102, in function5
    self.function6()
TypeError: function6() missing 1 required positional argument: 'self'

What to do?


Rui Fontes

NVDA portuguese team

James Scholes
 

First, do the functions rely on data from self? I.e. do they need access to instance variables from your global plugin? If not, just define the functions outside of the class, without a self attribute. This isn't Java; not everything needs to be inside a class.

def function1():
...
def function2():
...

def script_something(self, gesture):
function1()

However, if they do need to be inside the class, you really need an instance of your global plugin to pass around, not just the GlobalPlugin class. That cannot act as an instance of GlobalPlugin, because it's just... GlobalPlugin directly.

Easiest way of achieving the above would be to pass your global plugin instance as a parameter to your dialog, but I guarantee you that there is a better, cleaner way to do this. If you could describe what you're actually trying to do, it would help.

Regards,

James Scholes

On 06/04/2020 at 7:23 pm, Rui Fontes wrote:
Hello!
I am creating an add-on and I have the following:
class GlobalPlugin(globalPluginHandler.GlobalPlugin):
    def __init__(self):
        super(globalPluginHandler.GlobalPlugin, self).__init__()
    __gestures={
        "kb:NVDA+x": "something",
    }
  def script_something(self, gesture):
    Do something!
    self.function1()
  def function1(self):
    Do more something...
    self.function3()
  def function3(self):
    Do yet more one thing...
self.function4()
  def function4(self):
    Almost done...
    self.function5()
  def function6(self):
    At last, all ready!
    gui.mainFrame._popupSettingsDialog(MyDialog)
class MyDialog(wx.Dialog):
  _instance = None
  def __new__(cls, *args, **kwargs):
    global variables
    if MyDialog._instance is None:
      return wx.Dialog.__new__(cls)
      return MyDialog._instance
  def __init__ (self, parent):
    MyDialog._instance = self
    self.title = title
    super (spellCheckerDialog, self).__init__(parent, wx.ID_ANY, title)
    self.makeSettings()
  def makeSettings(self):
And there one of the functions bind to a button is:
  def onIgnoreAll(self,evt):
    candidates.append(newWord)
    GlobalPlugin.function5(self = GlobalPlugin)
And my problem is:
After running function5, calling function6 gaves an error...
  File "C:\Users\Admin\AppData\Roaming\nvda\addons\MyAddon\globalPlugins\MyAddon.py", line 102, in function5
    self.function6()
TypeError: function6() missing 1 required positional argument: 'self'
What to do?
Rui Fontes
NVDA portuguese team

Rui Fontes
 

Thanks James!


As usual, it worked like a charm!

I don't want yet to talk about the purpose of the add-on, because I don't know yet if it will be of any use...

It seems it will be, but let me finish all the routines...


Rui Fontes

NVDA portuguese team



Às 02:53 de 07/04/2020, James Scholes escreveu:

First, do the functions rely on data from self?  I.e. do they need access to instance variables from your global plugin?  If not, just define the functions outside of the class, without a self attribute. This isn't Java; not everything needs to be inside a class.

def function1():
...
def function2():
...

    def script_something(self, gesture):
        function1()

However, if they do need to be inside the class, you really need an instance of your global plugin to pass around, not just the GlobalPlugin class.  That cannot act as an instance of GlobalPlugin, because it's just... GlobalPlugin directly.

Easiest way of achieving the above would be to pass your global plugin instance as a parameter to your dialog, but I guarantee you that there is a better, cleaner way to do this.  If you could describe what you're actually trying to do, it would help.

Regards,

James Scholes

On 06/04/2020 at 7:23 pm, Rui Fontes wrote:
Hello!


I am creating an add-on and I have the following:


class GlobalPlugin(globalPluginHandler.GlobalPlugin):
     def __init__(self):
         super(globalPluginHandler.GlobalPlugin, self).__init__()

     __gestures={
         "kb:NVDA+x": "something",
     }

   def script_something(self, gesture):
     Do something!

     self.function1()

   def function1(self):
     Do more something...
     self.function3()

   def function3(self):

     Do yet more one thing...

self.function4()

   def function4(self):
     Almost done...
     self.function5()

   def function6(self):
     At last, all ready!
     gui.mainFrame._popupSettingsDialog(MyDialog)

class MyDialog(wx.Dialog):
   _instance = None
   def __new__(cls, *args, **kwargs):
     global variables
     if MyDialog._instance is None:
       return wx.Dialog.__new__(cls)
       return MyDialog._instance

   def __init__ (self, parent):
     MyDialog._instance = self
     self.title = title
     super (spellCheckerDialog, self).__init__(parent, wx.ID_ANY, title)
     self.makeSettings()

   def makeSettings(self):


And there one of the functions bind to a button is:


   def onIgnoreAll(self,evt):
     candidates.append(newWord)
     GlobalPlugin.function5(self = GlobalPlugin)

And my problem is:

After running function5, calling function6 gaves an error...


   File "C:\Users\Admin\AppData\Roaming\nvda\addons\MyAddon\globalPlugins\MyAddon.py", line 102, in function5
     self.function6()
TypeError: function6() missing 1 required positional argument: 'self'

What to do?


Rui Fontes

NVDA portuguese team