如何在python中增加QLCDNumber

时间:2016-12-01 15:34:44

标签: python multithreading pyqt pyqt4 qlcdnumber

我正在尝试更新QLCDNumber的值。我想要做的是在一个单独的线程中运行一个函数,该线程输出一个值(在这种情况下只是向上计数)并将此值显示在屏幕上。

这是我正在使用的python脚本,下面是.ui文件的内容(其中QLCDNumber被命名为“disp”):

import sys
import threading
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import uic
from time import sleep


Ui_MainWindow, QtBaseClass = uic.loadUiType("./disp.ui")

class MainWindow(QMainWindow, Ui_MainWindow):
    counter = pyqtSignal(int)
    counting = False

    def __init__(self):
        QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)

        self.disp.display(?????) #<---- 


    def startCounting(self):
        if not self.counting:
            self.counting = True
            thread = threading.Thread(target=self.something)
            thread.start()

    def something(self):
        for i in range(100):
            self.counter.emit(int(i))
            sleep(0.5)
        self.counting = False


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

.ui文件:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>577</width>
    <height>504</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <widget class="QLCDNumber" name="disp"/>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>577</width>
     <height>24</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

1 个答案:

答案 0 :(得分:1)

你几乎就在那里,你发出一个带有新值的信号但是你的信号没有连接到任何函数,你只需要创建一个函数来更新QLCDNumber的值并将你的信号计数器连接到这个函数:

import sys
import threading

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4 import uic

from time import sleep


Ui_MainWindow, QtBaseClass = uic.loadUiType("./disp.ui")

class MainWindow(QMainWindow, Ui_MainWindow):
    counter = pyqtSignal(int)
    counting = False

    def __init__(self):
        QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)

        self.counter.connect(self.update_lcd)
        # self.startCounting()

    def update_lcd(self, value):
        self.disp.display(value)

    def startCounting(self):
        if not self.counting:
            self.counting = True
            thread = threading.Thread(target=self.something)
            thread.start()

    def something(self):
        for i in range(100):
            self.counter.emit(int(i))
            sleep(0.5)
        self.counting = False


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

我建议使用QThreadQRunnable代替线程模块来启动后台任务。可以找到差异的一个很好的解释here