当我在pyqt和python中使用while循环作为启动函数时,我的gui没有出现

时间:2019-07-26 12:56:47

标签: python-3.x pyqt5

我想使用应该具有while循环的启动功能。 但我运行的代码只有在while循环结束时才显示我的GUI。

我尝试使用self.show()它可以显示show gui,但不允许使用sys.exit()

import sys
from PyQt5.QtWidgets import QApplication
from PyQt5 import uic
import time

form_class,QMainWindow=uic.loadUiType('youhua.ui')

class MyWindow(QMainWindow,form_class):

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        #self.show()
        self.myfunc()

    def myfunc(self):

        k=1
        stat=True
        while stat:
            k=k+1
            time.sleep(1)
            self.statusMessage.append(str(k))
            QApplication.processEvents()
            if k>10:
                stat=False
                #sys.exit()


if __name__=='__main__':
    app=QApplication(sys.argv)
    myWindow=MyWindow()
    myWindow.show()
    app.exec_()

2 个答案:

答案 0 :(得分:1)

如果需要再次执行操作,则有几个选项。 例如,如果每次迭代都花费很少的时间,而又没有阻塞主循环的可能性,则可以用计时器(QTimer)替换周期并每次调用该方法,该方法负责获取新数据并更新必要的接口符合它们的元素:

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import uic
from PyQt5.QtCore import QThread, QTimer
import time

#form_class, QMainWindow = uic.loadUiType('youhua.ui')

class MyWindow(QMainWindow): #, form_class):
    def __init__(self):
        super().__init__() 
        self.k = 0

        centralWidget = QtWidgets.QWidget(self)
        self.setCentralWidget(centralWidget)        

        self.button = QtWidgets.QPushButton('Start', self)
        self.button.clicked.connect(self.read_data)

        self.label_data = QtWidgets.QLabel(self, alignment=QtCore.Qt.AlignCenter)
        self.label_data.setText('Pending')

        layout = QtWidgets.QGridLayout(centralWidget)        
        layout.addWidget(self.label_data)
        layout.addWidget(self.button)

        self.timer = QtCore.QTimer(self)
        self.timer.setInterval(1000)                             
        self.timer.timeout.connect(self.read_data_from_sensor)

    @QtCore.pyqtSlot()
    def read_data(self):
        ''' Start / Stop reading at the touch of a button '''

        if not self.timer.isActive():
            self.timer.start()
            self.button.setText("Stop")
        else:
            self.timer.stop()
            self.button.setText("Start")
            self.label_data.setText("Pending")

    @QtCore.pyqtSlot()
    def read_data_from_sensor(self):
        dt  = time.strftime("%Y-%m-%d %H:%M:%S")
        self.label_data.setText(dt)
        self.label_data.adjustSize()

        self.k += 1
        self.statusBar().showMessage('{} item(s)'.format(self.k))

        if self.k > 10:        
            self.timer.stop()
            self.button.setText("Start")
            self.label_data.setText("Pending")        
            self.k = 0

if __name__=='__main__':
    app = QApplication(sys.argv)
    myWindow = MyWindow()
    myWindow.show()
    app.exec_()

enter image description here


您写的内容也可能有效,但这不是很好。您可以进行比较。

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import uic
from PyQt5.QtCore import QThread
#import time

#form_class, QMainWindow = uic.loadUiType('youhua.ui')

class MyWindow(QMainWindow): #, form_class):

    def __init__(self):
        super().__init__() 
#        self.setupUi(self)

        self.show()            
        self.myfunc()

    def myfunc(self):
        k = 0
        stat = True
        while stat:
            k += 1
#            time.sleep(1)
#            self.statusMessage.append(str(k))

            self.statusBar().showMessage('{} item(s)'.format(k))
            QThread.msleep(1000)
            QApplication.processEvents()

            if k>10:
                stat=False
                #sys.exit()


if __name__=='__main__':
    app = QApplication(sys.argv)
    myWindow = MyWindow()
#    myWindow.show()
    app.exec_()

答案 1 :(得分:0)

在循环中您睡了10秒钟,因为您正在主线程上创建一个while循环,因此在循环完成之前,GUI不会显示,因为它会阻塞主线程。您可以删除time.sleep(1)来进行测试。

无需太多更改代码,请尝试以下操作:

import sys,threading, time
from PyQt5.QtWidgets import QApplication
from PyQt5 import uic

form_class,QMainWindow=uic.loadUiType('youhua.ui')

class MyWindow(QMainWindow,form_class):

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        #self.show()
        t = threading.Thread(target=self.myfunc)
        t.start()

    def myfunc(self):
        k=1
        stat=True
        while stat:
            k=k+1
            time.sleep(1)
            self.statusMessage.append(str(k))
            QApplication.processEvents()
            if k>10:
                stat=False
                #sys.exit() - if you are trying to close the window here use self.close()


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