ui_mainwindow' object has no attribute 'co

2019-09-20 03:47发布

I am new to PyQt. I want the GUI to be GUI1 from time 0 to time t1, GUI2 from time t1 to t2, ..., GUIn from time t(n-1) to t(n). So I thought I would have to use the multithreading techniques. Does anyone have available code that has the same function as my requirement?

Moreover, I met some problem when I try connect the thread's finished signal to the UpdateUI function. The code is shown below:

class Ui_MainWindow(QtCore.QObject):    


  def setupUi(self, MainWindow):
    MainWindow.setObjectName(_fromUtf8("MainWindow"))
    MainWindow.resize(771, 586)
    MainWindow.setMinimumSize(QtCore.QSize(771, 586))
    font = QtGui.QFont()
    font.setFamily(_fromUtf8("Arial"))
    MainWindow.setFont(font)

    #Feng        
    self.centralWidget = QtGui.QWidget(MainWindow)

    self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
    self.layoutWidget = QtGui.QWidget(self.centralWidget)
    self.layoutWidget.setGeometry(QtCore.QRect(10, 500, 751, 27))
    self.layoutWidget.setObjectName(_fromUtf8("layoutWidget"))
    self.horizontalLayout = QtGui.QHBoxLayout(self.layoutWidget)
    self.horizontalLayout.setSizeConstraint(QtGui.QLayout.SetMinimumSize)
    self.horizontalLayout.setMargin(11)
    self.horizontalLayout.setSpacing(6)
    self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
    self.btnSkip = QtGui.QPushButton(self.layoutWidget)
    sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
    sizePolicy.setHorizontalStretch(0)
    sizePolicy.setVerticalStretch(0)
    sizePolicy.setHeightForWidth(self.btnSkip.sizePolicy().hasHeightForWidth())
    self.btnSkip.setSizePolicy(sizePolicy)
    self.btnSkip.setMinimumSize(QtCore.QSize(0, 23))
    self.btnSkip.setFlat(False)
    self.btnSkip.setObjectName(_fromUtf8("btnSkip"))        
    self.horizontalLayout.addWidget(self.btnSkip)
    self.horizontalSlider = QtGui.QSlider(self.layoutWidget)
    self.horizontalSlider.setMinimumSize(QtCore.QSize(0, 22))
    self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
    self.horizontalSlider.setObjectName(_fromUtf8("horizontalSlider"))



    self.horizontalLayout.addWidget(self.horizontalSlider)
    self.btnPrevious = QtGui.QPushButton(self.layoutWidget)
    sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
    sizePolicy.setHorizontalStretch(0)
    sizePolicy.setVerticalStretch(0)
    sizePolicy.setHeightForWidth(self.btnPrevious.sizePolicy().hasHeightForWidth())
    self.btnPrevious.setSizePolicy(sizePolicy)
    self.btnPrevious.setMinimumSize(QtCore.QSize(0, 23))
    self.btnPrevious.setObjectName(_fromUtf8("btnPrevious"))
    self.horizontalLayout.addWidget(self.btnPrevious)


    self.btnNext = QtGui.QPushButton(self.layoutWidget)
    sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed)
    sizePolicy.setHorizontalStretch(0)
    sizePolicy.setVerticalStretch(0)
    sizePolicy.setHeightForWidth(self.btnNext.sizePolicy().hasHeightForWidth())
    self.btnNext.setSizePolicy(sizePolicy)
    self.btnNext.setMinimumSize(QtCore.QSize(0, 23))   

    self.btnNext.setObjectName(_fromUtf8("btnNext"))
    self.horizontalLayout.addWidget(self.btnNext)
    self.layoutWidget1 = QtGui.QWidget(self.centralWidget)
    self.layoutWidget1.setGeometry(QtCore.QRect(11, 11, 751, 171))
    self.layoutWidget1.setObjectName(_fromUtf8("layoutWidget1"))
    self.verticalLayout = QtGui.QVBoxLayout(self.layoutWidget1)
    self.verticalLayout.setMargin(11)
    self.verticalLayout.setSpacing(6)
    self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
    self.textEditTaskName = QtGui.QTextEdit(self.layoutWidget1)
    self.textEditTaskName.setEnabled(False)

    sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
    sizePolicy.setHorizontalStretch(0)
    sizePolicy.setVerticalStretch(0)
    sizePolicy.setHeightForWidth(self.textEditTaskName.sizePolicy().hasHeightForWidth())
    self.textEditTaskName.setSizePolicy(sizePolicy)
    self.textEditTaskName.setMinimumSize(QtCore.QSize(0, 94))
    font = QtGui.QFont()
    font.setFamily(_fromUtf8("Arial"))
    font.setPointSize(18)
    font.setBold(True)
    font.setWeight(75)
    self.textEditTaskName.setFont(font)
    self.textEditTaskName.setAcceptDrops(False)
    self.textEditTaskName.setFrameShape(QtGui.QFrame.NoFrame)

    #Feng
    self.textEditTaskName.setText("Welcome")

    self.textEditTaskName.setObjectName(_fromUtf8("textEditTaskName"))
    self.verticalLayout.addWidget(self.textEditTaskName)
    self.textEditTaskdesciption = QtGui.QTextEdit(self.layoutWidget1)
    self.textEditTaskdesciption.setEnabled(False)
    sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)
    sizePolicy.setHorizontalStretch(0)
    sizePolicy.setVerticalStretch(1)
    sizePolicy.setHeightForWidth(self.textEditTaskdesciption.sizePolicy().hasHeightForWidth())
    self.textEditTaskdesciption.setSizePolicy(sizePolicy)
    self.textEditTaskdesciption.setMinimumSize(QtCore.QSize(0, 69))
    font = QtGui.QFont()
    font.setFamily(_fromUtf8("Arial"))
    font.setPointSize(12)
    self.textEditTaskdesciption.setFont(font)
    self.textEditTaskdesciption.setAcceptDrops(False)

    #Feng
    self.textEditTaskdesciption.setText('Welcome to my program.')

    self.textEditTaskdesciption.setFrameShape(QtGui.QFrame.NoFrame)
    self.textEditTaskdesciption.setFrameShadow(QtGui.QFrame.Plain)
    self.textEditTaskdesciption.setObjectName(_fromUtf8("textEditTaskdesciption"))
    self.verticalLayout.addWidget(self.textEditTaskdesciption)
    self.layoutWidget2 = QtGui.QWidget(self.centralWidget)
    self.layoutWidget2.setGeometry(QtCore.QRect(10, 190, 751, 311))
    self.layoutWidget2.setObjectName(_fromUtf8("layoutWidget2"))
    self.verticalLayout_2 = QtGui.QVBoxLayout(self.layoutWidget2)
    self.verticalLayout_2.setMargin(11)
    self.verticalLayout_2.setSpacing(6)
    self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
    self.labelTESTImage = QtGui.QLabel(self.layoutWidget2)
    font = QtGui.QFont()
    font.setFamily(_fromUtf8("Arial"))
    font.setPointSize(14)
    font.setBold(True)
    font.setWeight(75)
    self.labelTESTImage.setFont(font) 
    self.labelTESTImage.setAlignment(QtCore.Qt.AlignCenter)   
    image = QtGui.QImage('Images/deptLogo.png')

    #Feng
    self.labelTESTImage.setPixmap(QPixmap.fromImage(image))#("deptLogo.png"))       
    self.labelTESTImage.setScaledContents(True)
    self.labelTESTImage.setObjectName(_fromUtf8("labelTESTImage"))
    self.labelTESTImage.setScaledContents(True)
    #Feng

    self.btnNext.pressed.connect(self.nextSession)
    self.btnPrevious.pressed.connect(self.previousSession)
    #Feng
    self.horizontalSlider.setRange(-1,len(strSettings)-1)
    self.horizontalSlider.setValue(-1)
    self.horizontalSlider.valueChanged.connect(self.dragSlider)
    #self.btnNext.clicked.connect(self.readingSettingsFile)
    #self.btnNext.clicked.connect(self.loadimage)


    self.verticalLayout_2.addWidget(self.labelTESTImage)
    self.progressBar = QtGui.QProgressBar(self.layoutWidget2)
    self.progressBar.setMinimumSize(QtCore.QSize(0, 22))
    self.progressBar.setProperty("value", 24)
    self.progressBar.setObjectName(_fromUtf8("progressBar"))
    self.progressBar.setVisible(False)
    self.verticalLayout_2.addWidget(self.progressBar)
    MainWindow.setCentralWidget(self.centralWidget)
    self.menuBar = QtGui.QMenuBar(MainWindow)
    self.menuBar.setGeometry(QtCore.QRect(0, 0, 771, 21))
    self.menuBar.setObjectName(_fromUtf8("menuBar"))
    self.menuFile = QtGui.QMenu(self.menuBar)
    self.menuFile.setObjectName(_fromUtf8("menuFile"))
    self.menuEdit = QtGui.QMenu(self.menuBar)
    self.menuEdit.setObjectName(_fromUtf8("menuEdit"))
    self.menuHelp = QtGui.QMenu(self.menuBar)
    self.menuHelp.setObjectName(_fromUtf8("menuHelp"))
    MainWindow.setMenuBar(self.menuBar)
    self.mainToolBar = QtGui.QToolBar(MainWindow)
    self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
    MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
    self.statusBar = QtGui.QStatusBar(MainWindow)
    self.statusBar.setObjectName(_fromUtf8("statusBar"))
    MainWindow.setStatusBar(self.statusBar)
    self.menuBar.addAction(self.menuFile.menuAction())
    self.menuBar.addAction(self.menuEdit.menuAction())
    self.menuBar.addAction(self.menuHelp.menuAction())

    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
    MainWindow.setWindowTitle(_translate("MainWindow", "my test", None))
    MainWindow.setWindowIcon(QtGui.QIcon('Images/test.ico')) 
    self.btnSkip.setText(_translate("MainWindow", "Skip", None))
    self.btnPrevious.setText(_translate("MainWindow", "&Previous", None))
    self.btnNext.setText(_translate("MainWindow", "&Next", None))
    #self.labelTESTImage.setText(_translate("MainWindow", "my test", None))
    self.menuFile.setTitle(_translate("MainWindow", "File", None))
    self.menuEdit.setTitle(_translate("MainWindow", "Edit", None))
    self.menuHelp.setTitle(_translate("MainWindow", "Help", None))


    #Feng
def dragSlider(self): 
    global strSettings, timeSleep, lineCurrent 
    lineCurrent =  int(self.horizontalSlider.value())

    if (lineCurrent != -1):
        line = strSettings[lineCurrent]      
        wavFilename, showProgressBar, valPauseTime, valName, valDescription, valImageLoad = line.split("\t")
        self.time_thread = timerThread(float(valPauseTime))
        self.connect(self.time_thread, SIGNAL("finished()"),self, self.updateUIFromFile())            
    else:
        self.btnNext.setText(_translate("MainWindow", "&Start", None))
        self.textEditTaskName.setText("Welcome")
        self.textEditTaskdesciption.setText('This program is designed by Feng.')
        self.labelOTCImage.setPixmap(QPixmap.fromImage(QtGui.QImage('Images/deptLogo.png')))
        self.labelOTCImage.setScaledContents(True)
        self.progressBar.setVisible(False)


def nextSession(self): 
    global strSettings, timeSleep, lineCurrent  
    if(lineCurrent<len(strSettings)-1): 
        lineCurrent += 1
        self.horizontalSlider.setValue(lineCurrent)
        #self.updateUIFromFile()

def previousSession(self): 
    global strSettings, timeSleep, lineCurrent        
    if  lineCurrent>0:
        lineCurrent -= 1            
    else:
        lineCurrent = -1         
    self.horizontalSlider.setValue(lineCurrent)
    #self.updateUIFromFile()            

def updateUIFromFile(self):         

    self.btnNext.setText(_translate("MainWindow", "&Next", None))
    line = strSettings[lineCurrent]

    wavFilename, showProgressBar, valPauseTime, valName, valDescription, valImageLoad = line.split("\t")
    self.textEditTaskName.setText(valName)
    self.textEditTaskdesciption.setText(valDescription)
    self.labelOTCImage.setPixmap(QPixmap.fromImage(QtGui.QImage('resources/'+valImageLoad.replace("\n", ""))))
    self.labelOTCImage.setScaledContents(False)
    self.progressBar.setVisible(int(showProgressBar))
    self.progressBar.setValue(50)

This is the thread class

class timerThread(QtCore.QThread):
def __init__(self,sleepTime):
    QtCore.QThread.__init__(self)
    self.sleepTime = sleepTime

def __del__(self):
    self.wait()

def run(self):
    time.sleep(self.sleepTime) # artificial time delay

Main

if __name__ == "__main__":

try:
    fileSettings = open('protocol.txt','r')    
except IOError as e:   
    print ("Unable to open file") #Does not exist OR no read permissions
    sys.exit(0)
strSettings = fileSettings.readlines()
strSettings = list(filter(None, strSettings))
fileSettings.close()  
timeSleep = 0
lineCurrent = -1


app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()

sys.exit(app.exec_())

The file 'protocol.txt' contains the contents for each QtWidget and a timeline for when the GUI would change contents

2条回答
We Are One
2楼-- · 2019-09-20 04:21

You're using the Ui_MainWindow class incorrectly. See this answer for how to use the auto-generated UI classes.

You should be either inheriting from the UI class, or setting it as an instance attribute on one of your own classes.

class MainWIndow(QtGui.QMainWIndow,Ui_MainWindow):

    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)                
        self.setupUi(self)

Sometimes, people won't inherit, but will assign it to an attribute of the class:

class MainWIndow(QtGui.QMainWIndow):

    def __init__(self, parent=None):
        QtGui.QMainWIndow.__init__(self, parent)                
        self.ui = Ui_MainWindow(self)
        self.ui.setupUi(self)
查看更多
你好瞎i
3楼-- · 2019-09-20 04:30

So although nobody wants to give the answer to this question, I think I found the answer from another question asked by me in Update GUI at different time interval in QT.

I hope someone could give me the pyqt version of this answer because this is in C++.

查看更多
登录 后发表回答