2016-08-15 10 views
0

これは私の最初の投稿です。pyqt5の生成コード内の変数にアクセスできません

しかし、広範な研究の後、私は私の問題の解決策を見つけることができませんでした。私は、プログレスバーを含むQtCreatorによって生成されたファイルを持っています。 私のコードでは2つのクラスがあり、1つはスレッドです。このスレッドはプログレスバーの値を変更する必要がありますが、私は完全に失敗します。

スレッドから変数にアクセスできませんが、私はMainwindowのinitからアクセスできます。私はこの問題はsetprogressBarにおける「自己」変数の性質だと思うが、私は文字通りそれが何であるかを見つけ出すこだわっている..

私はこのコードを実行しようとしたとき、ここでの結果である:

File "C:\test.py", line 14, in setprogressBar 
self.progressBar.setProperty("value", pourcentage) 
AttributeError: type object 'MainWindow' has no attribute 'progressBar' 

QtCreatorで生成されたファイルA、:

from PyQt5 import QtCore, QtGui, QtWidgets 

class Ui_MainWindow(object): 
    def setupUi(self, MainWindow): 
     MainWindow.setObjectName("MainWindow") 
     self.progressBar = QtWidgets.QProgressBar(self.widget) 
     self.progressBar.setObjectName("progressBar") 

ファイルB、私のコード:

from PyQt5 import QtWidgets 
from UImainwindow import Ui_MainWindow 
from threading import Thread 
import sys 

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): 
# access variables inside of the UI's file 
    def __init__(self): 
     super(MainWindow, self).__init__() 
     self.setupUi(self) # gets defined in the UI file 
     self.progressBar.setProperty("value", 24) #This is working 

    def setprogressBar(self, pourcentage): 
     self.progressBar.setProperty("value", pourcentage) #This is not 

class B(Thread): 
    def __init__(self): 
     Thread.__init__(self) 

    def run(self):   
     MainWindow.setprogressBar(MainWindow ,48) 

APP = QtWidgets.QApplication(sys.argv) 

Bi = B() 
Bi.start() 

MAINWIN = MainWindow() 
MAINWIN.show() 

APP.exec_() 

Thxは助けの人のためにたくさん!

+0

Qtメソッドはメインスレッドからのみ呼び出すことができます。 B.runからそれらを呼び出すことはできません。 B.runからの呼び出しをメインスレッドに戻すように整理する必要があります。 –

答えて

0

MainWindowはオブジェクトではなくクラスです。代わりにあなたがするべきことは次のようなものです:

class B(Thread): 
    def __init__(self, target): 
     self.__target = target 

    def run(self): 
     self.__target.setprogressBar(48) 

MAINWIN = MainWindow() 

bi = B(MAINWIN) 
bi.start() 

MAINWIN.show() 
+0

ありがとう!これはうまく働いた –

関連する問題