-1

I am trying to use function when I clicked the button with below source codes:

from PySide2.QtWidgets import QApplication

from ui_interface import *
from Custom_Widgets.Widgets import *
from PyQt5.QtGui import QIcon

# from PyQt5.QtWidgets import *
from Custom_Widgets.Widgets import QCustomSlideMenu

import sys
class MainWindow(QMainWindow):
    def __init__(self,parent = None):
        QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        ########################################################################
        # APPLY JSON STYLESHEET
        ########################################################################
        # self = QMainWindow class
        # self.ui = Ui_MainWindow / user interface class
        loadJsonStyle(self, self.ui)
        ########################################################################
        self.show()
        self.ui.pushButton_2.clicked.connect(self.my_text())
    @pyqtSlot()
    def on_button1(self):
        print("Button #1")
    def my_text(self):
        index = 1
        print("{0} button clicked".format(index))
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    # app.setWindowIcon(QIcon(':/icons/home.ico'))
    window.show()
    sys.exit(app.exec_())

When I using like this:

self.ui.pushButton_2.clicked.connect(self.my_text())

When I clicked the button, does not show anything.

But if I use like this:

self.ui.pushButton_2.clicked.connect(lambda: self.my_text())

It works.

And Also when I use like this:

self.ui.pushButton_2.clicked.connect(self.on_button1())

it works. But I dont understand why the first step does not working?

gogogo
  • 529
  • 1
  • 3
  • 11
  • 1
    When you are connecting the signal to a slot, you should just pass a function, but what you passed is a function call. Basically, you need to remove parenthesis. `self.ui.pushButton_2.clicked.connect(self.my_text)` should work fine which is similar to `self.ui.pushButton_2.clicked.connect(lambda: self.my_text())` – ThePyGuy Nov 03 '21 at 12:06
  • But if I use the parameter how can I use the parameter without function type. For example If I have def my_text(self,index): function how can I give a index to my_text funtion? – gogogo Nov 03 '21 at 12:08
  • In that case, it needs to be passed through the lambda function. – ThePyGuy Nov 03 '21 at 12:09

1 Answers1

1

try this

from PyQt5.QtWidgets import * 
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys
  
  
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # setting geometry
        self.setGeometry(100, 100, 600, 400)
  
        # calling method
        self.UiComponents()
  
        # showing all the widgets
        self.show()
  
    # method for widgets
    def UiComponents(self):
  
        # creating a push button
        button = QPushButton("CLICK", self)
  
        # setting geometry of button
        button.setGeometry(200, 150, 100, 30)
  
        # adding action to a button
        button.clicked.connect(self.clickme)
  
    # action method
    def clickme(self):
        print("pressed")
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

is you're looking for this???

ItsMe
  • 395
  • 2
  • 13