0

Can someone explain why Python is showing abnormal behavior code is not running in a serial manner (line by line)? timer_my is a button clicked method, label is a label which image I want to change on click but it changes after running tf_s1 method even it is written before it, in timer_my method. self.ts1.setPixmap(QtGui.QPixmap("green_traffic.png"))

import time

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(280, 320, 231, 41))
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.timer_my)
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(250, 70, 311, 211))
        self.label.setText("")
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))

    def tf_s1(self):

        for i in reversed(range(5)):
            time.sleep(1)
            # print(stat)
            print(i)

    def timer_my(self):
        self.label.setPixmap(QtGui.QPixmap("green_traffic.png"))
        self.tf_s1()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Heike
  • 24,102
  • 2
  • 31
  • 45
  • 1
    What is the "_abnormal behavior_"? Please be specific. – DYZ May 22 '20 at 19:53
  • @DYZ it is running First self.tf_s1 line than self.label.setPixmap(QtGui.QPixmap("green_traffic.png")) where as it should run first self.label.setPixmap(QtGui.QPixmap("green_traffic.png")) – Mohsin Ali May 22 '20 at 19:54
  • I wonder if it could be a Qt message loop thing. In a GUI such as Qt, things don't always happen immediately. – Fred Larson May 22 '20 at 19:58
  • @FredLarson No, I don't think so if I increase the range from 5 to 10 of the for loop in tf_s1 method image appears after 10sec I don't think it is a lag – Mohsin Ali May 22 '20 at 20:07
  • But I don't think the message loop is able to run until after you return from `timer_my`. – Fred Larson May 22 '20 at 20:18
  • But when I comment self.tf_s1 method in timer_my(self) method image is displayed immediately so why it is not displaying immediately if I call tf_s1 method after it. – Mohsin Ali May 22 '20 at 20:33
  • But without the `tf_s1` call, `timer_my` returns immediately. I don't believe the `setPixmap` call immediately sets the pixmap. Rather, it puts an event on the Qt queue, which is waiting for your callback to return before it can continue processing events. – Fred Larson May 22 '20 at 20:39
  • @FredLarson fixed issue by https://stackoverflow.com/questions/41545300/equivalent-to-time-sleep this – Mohsin Ali May 22 '20 at 20:50

0 Answers0