Python PyQt5和设计器类型错误:(“顶级小部件的基类错误”,(〈class '__main__. mainScreen'>,' QMainWindow '))

x4shl7ld  于 2022-12-28  发布在  Python
关注(0)|答案(1)|浏览(158)

我不知道为什么我会得到下面的错误。我试图在一个线程中使用PyQt5在parralel中执行额外的任务。现在它应该只是启动应用程序并从文本文件更新屏幕上的1个文本框。
错误:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\kevil\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Users\kevil\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "C:/Users/kevil/OneDrive/Desktop/Projects/Load Cell Project/ui/mainUI.py", line 27, in startUI
    mainwindow = mainScreen()
  File "C:/Users/kevil/OneDrive/Desktop/Projects/Load Cell Project/ui/mainUI.py", line 12, in __init__
    loadUi("KMLScales_UI_V0.0.ui", self)
  File "C:\Users\kevil\OneDrive\Desktop\Projects\Load Cell Project\venv\lib\site-packages\PyQt5\uic\__init__.py", line 238, in loadUi
    return DynamicUILoader(package).loadUi(uifile, baseinstance, resource_suffix)
  File "C:\Users\kevil\OneDrive\Desktop\Projects\Load Cell Project\venv\lib\site-packages\PyQt5\uic\Loader\loader.py", line 66, in loadUi
    return self.parse(filename, resource_suffix)
  File "C:\Users\kevil\OneDrive\Desktop\Projects\Load Cell Project\venv\lib\site-packages\PyQt5\uic\uiparser.py", line 1037, in parse
    actor(elem)
  File "C:\Users\kevil\OneDrive\Desktop\Projects\Load Cell Project\venv\lib\site-packages\PyQt5\uic\uiparser.py", line 822, in createUserInterface
    self.toplevelWidget = self.createToplevelWidget(cname, wname)
  File "C:\Users\kevil\OneDrive\Desktop\Projects\Load Cell Project\venv\lib\site-packages\PyQt5\uic\Loader\loader.py", line 59, in createToplevelWidget
    (type(self.toplevelInst), classname)))
TypeError: ('Wrong base class of toplevel widget', (<class '__main__.mainScreen'>, 'QMainWindow'))

代码:

import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.uic import loadUi
from PyQt5.QtCore import QTimer
import threading

class mainScreen(QDialog):
    def __init__(self):
        super(mainScreen, self).__init__()
        loadUi("KMLScales_UI_V0.0.ui", self)
        # Frequently Update
        self.step = 250
        self.timer = QTimer()
        self.timer.timeout.connect(self.update)
        self.timer.start(self.step)

    def update(self):
        with open("lastWeight.txt", encoding='utf-8') as weightFile:  # Get Last Weight Value
            lastWeight = weightFile.read()
        self.LF_Weight_QLineEdit.setText(lastWeight)

def startUI():
    app = QApplication(sys.argv)
    mainwindow = mainScreen()
    widget = QtWidgets.QStackedWidget()
    widget.addWidget(mainwindow)
    widget.setFixedWidth(800)
    widget.setFixedHeight(480)
    widget.show()
    app.exec_()

UIapplication = threading.Thread(target=startUI)
UIapplication.start()
jdzmm42g

jdzmm42g1#

PyQt5提供了自己的read more about QThread线程化方式,如果您打算更新GUI,那么推荐使用这种方式。
这些线程称为Workers,并继承自类QThread
在您的mainScreen构造函数中,如下所示初始化并启动Worker:

def __init__(self):
    self.worker = WorkerThread()
    self.worker.start()
    self.worker.text.connect(self.function_to_update_text)

def function_to_update_text(self, text):
    self.textbox.setText(text)

在您的Worker中:

class WorkerThread(QThread):
test = pyqtSignal(str)
edited = False

def run(self):
    if not edited:
        #The emit method takes the same parameter as the method you passed to your worker object
        self.text.emit("Any Text to update");
        edited = True

相关问题