-1

I am new to QT, and I am having difficulty using a function that has arguments in a QT-designer generated GUI. How do you tie a button to a function that has arguments? I can get the button to work with a print "hello world" function. Functions with arguments result in a boolean "False" when I click the button.

import GUITESTFUNCS    
self.pushButton.clicked.connect(GUITESTFUNCS.display)

The function I import for my GUI button to use is...

#practice functions for GUITEST

a = 8

def display(a):
    print(str(a))
Fillups
  • 55
  • 1
  • 5
  • You should read about the underlying signal&slot mechanisms before you start messing with them: http://doc.qt.io/qt-5/signalsandslots.html. Thus http://idownvotedbecau.se/noresearch/ – Murphy Jan 12 '18 at 10:23

1 Answers1

3

When you connect a signal to a function, any arguments passed to the function are generated by the signal that is emitted, not some arbitrary value that you define elsewhere in your code. You can look up the clicked signal in the QT docs and see that the argument for that signal is a boolean that is only relevant if the button is checkable. In your case, the button is either not checkable or isn't checked, that is why you're getting False as the parameter to your display function.

If you want to connect a signal to a function and for that function to receive a parameter that the signal doesn't generate, I usually do that like this:

from functools import partial

def display(arg):
    print(arg)

a = 8

display_a = partial(display, a)

self.pushButton.clicked.connect(display_a)
Turn
  • 6,656
  • 32
  • 41