M
M
Maria Berestovaya2019-04-27 10:50:45
Python
Maria Berestovaya, 2019-04-27 10:50:45

How to bind click on buttons in python3 PyQt5?

Hello!
I tied it via setText, but I need the text in the Line Edit to be displayed every time the button is pressed, and it is displayed only once.
I'm making a calculator.
And if you can recommend PyQt documentation...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bbkmzzzz, 2019-04-27
@bbkmzzzz

Documentation for 5.12
C++ documentation .
PyQt uses an event system. Objects generate events that you can subscribe to and attach a function (called a slot) that will be called when the event occurs.
For example, a QPushButton generates a clicked signal when it is clicked.

def on_click():
    print("clicked!")

btn.clicked.connect(on_click)  # btn <- наша кнопка, сигнал clicked, метод сигнала connect (функция, которая будет вызвана). 
# Нужно обратить внимание, что функцию вызывать не надо (скобки не нужны)
# Так же сигналы могут передавать данные, в таком случае функция, привязанная к сигналу так же должна принимать на вход данные

import sys

from PyQt5.QtWidgets import QWidget, QPushButton
from PyQt5.QtCore import QSize


class Main(QWidget):
    def __init__(self):
        super(Main, self).__init__()
        self.resize(QSize(100, 100))
        self.btn = QPushButton("TEST BUTTON", self)

        self.btn.clicked.connect(self.on_click)  # соединение сигнала и слота (сигнал clicked и слот on_click)

    def on_click(self):
        print('Clicked!')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Main()
    ex.show()
    sys.exit(app.exec_())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question