如何在 python 中增加 QLCDNumber

How to increment QLCDNumber in python

我正在尝试更新 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>

你就快完成了,你正在发射一个带有新值的信号,但是你的信号没有连接到任何函数,你只需要创建一个函数来更新 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_())

我建议使用 QThread or QRunnable instead of threading module to start your background task. A nice explanation of the difference can be found here