1

I am trying to learn how to develop QGIS plugins but I struggle to get going by some simple principles.

As a test I am just trying to connect a function to a button. So when pressed the function run. Where to put the function and connections are not obvious to me.

This example give me an error message TypeError: test_print() takes no arguments (2 given) I cannot see how this give test_print 2 arguments?

def run(self):

    # Run test_print method when pushButton is clicked
    self.dlg.pushButton.clicked.connect(self.test_print)

    # show the dialog
    self.dlg.show()

def test_print():
    print 'It works!'

Does anyone has any general advice how to wire up buttons and functions properly?

EDIT: Working test example

This worked.

def run(self):

        def test_print():
            QMessageBox.about(None,'test','It works!')

        # Run test_print method when pushButton is clicked
        self.dlg.pushButton.clicked.connect(test_print)

Now, how can i run the test_print method if its not inside def run(self)?

6
  • 2
    About your specific issue: you have to add self as an argument to test_print (i.e. test_print(self)).
    – ArMoraer
    Jan 25, 2017 at 10:38
  • It did not work. This works however (see initial question). How can I call a function that is not inside the run method?
    – geogrow
    Jan 25, 2017 at 11:04
  • The method provided by @ArMoraer worked for me. Did you receive any errors in the Python Console when you tried using test_print(self)?
    – Joseph
    Jan 25, 2017 at 11:09
  • Maybe I am missing something very obvious here but I get this NameError: global name 'test_print' is not defined
    – geogrow
    Jan 25, 2017 at 11:23
  • Is this your entire plugin? Just one file with a "run" function in it?
    – Spacedman
    Jan 25, 2017 at 11:26

1 Answer 1

1
  1. Try moving the line:

    self.dlg.pushButton.clicked.connect(self.test_print)
    

    to the initGui(self) function instead of in the run(self) function.


  1. As @ArMoraer mentioned, put your test_print(self) outside the run(self) function.

  1. Here is the part of the Example.py script I used to test it:

    def initGui(self):
        """Create the menu entries and toolbar icons inside the QGIS GUI."""
        icon_path = ':/plugins/Example/icon.png'
        self.add_action(
            icon_path,
            text=self.tr(u'Example'),
            callback=self.run,
            parent=self.iface.mainWindow())
    
        # Run test_print method when pushButton is clicked
        self.dlg.pushButton.clicked.connect(self.test_print)
    
    def run(self):        
        """Run method that performs all the real work"""
        # show the dialog
        self.dlg.show()
        result = self.dlg.exec_()
        if result:
            pass
    
    def test_print(self):
        print 'It works!'
    

You can also take a look at the following post which describes the fundamental parts of a QGIS plugin:

What is the purpose of some functions and files in QGIS Python plugins?

2
  • 1
    Great now it works! Really appreciate it, a good simple starting point. I created a new plugin from the Plugin builder and both @Joseph and @ArMoraer works. I just found that i had moved self.dlg = TestPluginDialog() from the add_action method to the init (followed an example before) that made it not functioning properly. Thanks for all your help!
    – geogrow
    Jan 25, 2017 at 12:18
  • @geogrow - Most welcome and nice one! Glad you got it working =)
    – Joseph
    Jan 25, 2017 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.