Search code examples
pythonpyside6

Function is executed without click in Python | Pyside


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_())

Solution

  • 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.