2016-12-01 5 views
0

QLCDNumber의 값을 업데이트하려고합니다. 내가 할 수 있기를 원하는 것은 값을 출력하는 별도의 스레드에서 함수를 실행하는 것입니다.이 경우에는 위쪽으로 세고이 값을 화면에 표시합니다. 여기 Python에서 QLCDNumber를 증가시키는 방법

내가 사용하고있는 파이썬 스크립트이며, 그 이하로합니다 (QLCDNumber는 "DISP"이름을 붙일 수있다) .ui 파일의 내용입니다 :

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

신호가 아무 기능과도 연결되어 있지 않지만 새로운 값으로 신호를 내보내는 경우 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 또는 QRunnable을 사용하여 백그라운드 작업을 시작하는 것이 좋습니다. 그 차이에 대한 좋은 설명은 here입니다.

+0

제안한 추가 기능을 추가하려고했지만 여전히 작동하지 않습니다. 필요한 다른 단계가 있습니까? 현재 카운터가 제로에 있습니다 (플러스에서는 제대로로드됩니다)? –

+0

'startCounting'기능을 트리거하는 버튼이 있습니까? 그렇지 않으면 뭔가를보기 위해'__init__'에서이 함수를 실행할 필요가 없습니다. 답변의 코드에 주석 처리 된 코드를 추가하여 주석 처리를 취소하면 실제 동작을 볼 수 있습니다. – SyedElec

+0

도움 주셔서 감사합니다. –