0

I created a GUI with a button and a disabled text field. I made a def, when the button is clicked, the text field should be enabled, but my def function is called without clicking the button when I start the Program. What is my mistake here ?

import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QStyle
from PySide6.QtCore import *
from PySide6.QtGui import QIcon
from ui_mainwindow import *


class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.pushButton.clicked.connect(self.btnclick())

    def btnclick(self):
        self.ui.lineEdit.setEnabled(True)


if __name__ == '__main__':

    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()
    sys.exit(app.exec_())
phos
  • 19
  • 4

1 Answers1

0

It's this:

        self.ui.pushButton.clicked.connect(self.btnclick())

That statement doesn't pass the function, it CALLS the function and passes it's return value. Remove the () and it should work.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • oh yeah it works now,didnt knew it could call the function, doesnt it need the click ? – phos Apr 23 '21 at 06:51
  • No, it calls the function during the call to `__init__`. When you wrote `self.btnclick()`, that CALLS the function, right at that point. That function returns nothing, so you passed `None` to `pushButton.clicked.connect`. There's a huge difference between `print(math.sin(1))` and `print(math.sin)`. It's the same thing. – Tim Roberts Apr 23 '21 at 07:01