2016-12-01 11 views
0

QLCDNumberの値を更新しようとしています。私ができるようにしたいのは、値を出力する別のスレッド(この場合は上向きにカウントする)で関数を実行し、この値を画面に表示することです。ここでPythonでQLCDNumberを増やす方法

は、私が使用していますPythonスクリプトであり、その下に(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

私はあなたが示唆したようにこの余分な機能を追加しようとしましたが、それでも機能しません。カウンターがゼロに座っている瞬間に、必要なステップがもう1つありますか? –

+0

'startCounting'関数をトリガするボタンなどがありますか? もしあなたが何かを見るために '__init__'でこの関数を実行する必要がなければ。私は実際の動作を見るためにコメントを外すことができる答えのコードにコメント付きのコードを追加しました。 – SyedElec

+0

あなたの助けをありがとう –

関連する問題