D
D
Danila Rumyantsev2021-08-31 15:13:46
PyQt
Danila Rumyantsev, 2021-08-31 15:13:46

Why doesn't the pyqt5 button fire?

the function is attached to the button, but it doesn't fire when clicked, it should change the button's name when clicked

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
class Thread(QThread):
    signal = pyqtSignal(str)

    def __init__(self):
        super().__init__()
        self.flag = True


    def run(self):
        while self.flag:
            print('b')
            self.msleep(2000)

    def stop(self):
        self.flag = False
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(480, 640)
        MainWindow.setMinimumSize(QtCore.QSize(480, 640))
        MainWindow.setMaximumSize(QtCore.QSize(480, 640))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.btn_start = QtWidgets.QPushButton(self.centralwidget)
        self.btn_start.setGeometry(QtCore.QRect(80, 450, 311, 111))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(15)
        self.btn_start.setFont(font)
        self.btn_start.setStyleSheet("color: rgb(0, 0, 0);\n"
"")
        self.btn_start.setObjectName("btn_start")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(0, 0, 481, 391))
        self.label.setText("")
        self.label.setPixmap(QtGui.QPixmap("upload-004-pic685-685x390-91733.jpg"))
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.thread = Thread()


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "АнтиАфк"))
        self.btn_start.setText(_translate("MainWindow", "Запуск"))

    def add_functionts(self):
        self.btn_start.clicked.connect(self.OnClicked)
    def OnClicked(self):
        _translate = QtCore.QCoreApplication.translate
        if self.btn_start.text()=='Запуск':
            self.thread.flag = True
            self.thread.start()
            print('Работает')
            self.btn_start.setText("Стоп")
        else:
            self.thread.flag = False
            self.btn_start.setText("Запуск")
            print('Все')

UPD: Sorry for posting so much code, just xs where exactly is the error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
PRoGRamm_InG, 2021-08-31
@Bubunduc

You forgot to add the add_functionts function to retranslateUi()
Correct:

spoiler
def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "АнтиАфк"))
    self.btn_start.setText(_translate("MainWindow", "Запуск"))
    add_functionts()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question