0
我正在嘗試更新QLCDNumber的值。我想要做的是在一個單獨的線程中運行一個函數,輸出一個值(在這種情況下只需向上計數),並將該值顯示在屏幕上。如何在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>
我試着添加這個額外的功能就像你建議的,但它仍然無法正常工作。有沒有需要的另一個步驟,目前櫃檯剛好位於零(現在加載正確)? –
你有沒有觸發'startCounting'功能的按鈕? 如果不是,你需要在'__init__'中運行這個函數來查看一些東西。我在答案的代碼中添加了註釋代碼,您可以取消註釋以查看實際行爲。 – SyedElec
感謝您的幫助 –