&quot“ qwidget:必须在qwidget之前构造Qapplication”。 ,(新表格(new_main.ui)在pyqt5()中溅起屏幕之后不显示
import sys
import time
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QProgressBar, QLabel, QFrame, QHBoxLayout, QVBoxLayout
from PyQt5.QtCore import Qt, QTimer
from PyQt5 import QtWidgets, uic
from PyQt6.QtWidgets import QLineEdit, QMainWindow
import new_main
class SplashScreen(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Spash Screen Example')
self.setFixedSize(1100, 500)
self.setWindowFlag(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.counter = 0
self.n = 300 # total instance
self.initUI()
self.timer = QTimer()
self.timer.timeout.connect(self.loading)
self.timer.start(15)
def initUI(self):
layout = QVBoxLayout()
self.setLayout(layout)
self.frame = QFrame()
layout.addWidget(self.frame)
self.labelTitle = QLabel(self.frame)
self.labelTitle.setObjectName('LabelTitle')
# center labels
self.labelTitle.resize(self.width() - 10, 150)
self.labelTitle.move(0, 40) # x, y
#self.labelTitle.setText(' 3PP Sheet Automation')
self.labelTitle.setAlignment(Qt.AlignCenter)
self.labelDescription = QLabel(self.frame)
self.labelDescription.resize(self.width() - 10, 50)
self.labelDescription.move(0, self.labelTitle.height())
self.labelDescription.setObjectName('LabelDesc')
self.labelDescription.setText('<strong>Loading.</strong>')
self.labelDescription.setAlignment(Qt.AlignCenter)
self.progressBar = QProgressBar(self.frame)
self.progressBar.resize(self.width() - 200 - 10, 50)
self.progressBar.move(100, self.labelDescription.y() + 130)
self.progressBar.setAlignment(Qt.AlignCenter)
self.progressBar.setFormat('%p%')
self.progressBar.setTextVisible(True)
self.progressBar.setRange(0, self.n)
self.progressBar.setValue(20)
self.labelLoading = QLabel(self.frame)
self.labelLoading.resize(self.width() - 10, 50)
self.labelLoading.move(0, self.progressBar.y() + 70)
self.labelLoading.setObjectName('LabelLoading')
self.labelLoading.setAlignment(Qt.AlignCenter)
#self.labelLoading.setText('loading...')
def loading(self):
self.progressBar.setValue(self.counter)
if self.counter == int(self.n * 0.3):
self.labelDescription.setText('<strong>Loading...</strong>')
elif self.counter == int(self.n * 0.6):
self.labelDescription.setText('<strong>Loading.....</strong>')
elif self.counter == int(self.n * 0.9):
self.labelDescription.setText('<strong>Loading.......</strong>')
elif self.counter == int(self.n * 0.9):
self.labelDescription.setText('<strong>Loading........</strong>')
elif self.counter >= self.n:
self.timer.stop()
self.close()
time.sleep(1)
mainform = Input_form()
self.mainform.show()
self.counter += 1
class Input_form(QMainWindow):
def __init__(self):
super().__init__()
self.uic.loadUi('new_mian.ui', self)
self.show()
if __name__ == '__main__':
# don't auto scale when drag app to a different monitor.
# QApplication.setAttribute(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
app = QApplication(sys.argv)
app.setStyleSheet('''
#LabelTitle {
font-size: 60px;
color: #93deed;
}
#LabelDesc {
font-size: 30px;
color: #c2ced1;
}
#LabelLoading {
font-size: 30px;
color: #e8e8eb;
}
QFrame {
background-color: black;
color: rgb(220, 220, 220);
}
QProgressBar {
background-color: #DA7B93;
color: rgb(200, 200, 200);
border-style: none;
border-radius: 10px;
text-align: center;
font-size: 30px;
}
QProgressBar::chunk {
border-radius: 10px;
background-color: qlineargradient(spread:pad x1:0, x2:1, y1:0.511364, y2:0.523, stop:0 #1C3334, stop:1 #376E6F);
}
''')
splash = SplashScreen()
splash.show()
try:
sys.exit(app.exec_())
except SystemExit:
print('Closing Window...')
在成功的飞溅屏幕加载后,我试图分配通过QT Designer创建的new_mian.ui(qmainwindow)表单,并在代码中导入。
我还使用此“ pyuic5 new_main.ui -o new_main.py”命令生成了python文件,但其提示错误“ qwidget” qwidget:必须在qwidget之前构造qapplication”。我是PYQT的新手,无法理解为什么如此。请指导。提前致谢
import sys
import time
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QProgressBar, QLabel, QFrame, QHBoxLayout, QVBoxLayout
from PyQt5.QtCore import Qt, QTimer
from PyQt5 import QtWidgets, uic
from PyQt6.QtWidgets import QLineEdit, QMainWindow
import new_main
class SplashScreen(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('Spash Screen Example')
self.setFixedSize(1100, 500)
self.setWindowFlag(Qt.FramelessWindowHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.counter = 0
self.n = 300 # total instance
self.initUI()
self.timer = QTimer()
self.timer.timeout.connect(self.loading)
self.timer.start(15)
def initUI(self):
layout = QVBoxLayout()
self.setLayout(layout)
self.frame = QFrame()
layout.addWidget(self.frame)
self.labelTitle = QLabel(self.frame)
self.labelTitle.setObjectName('LabelTitle')
# center labels
self.labelTitle.resize(self.width() - 10, 150)
self.labelTitle.move(0, 40) # x, y
#self.labelTitle.setText(' 3PP Sheet Automation')
self.labelTitle.setAlignment(Qt.AlignCenter)
self.labelDescription = QLabel(self.frame)
self.labelDescription.resize(self.width() - 10, 50)
self.labelDescription.move(0, self.labelTitle.height())
self.labelDescription.setObjectName('LabelDesc')
self.labelDescription.setText('<strong>Loading.</strong>')
self.labelDescription.setAlignment(Qt.AlignCenter)
self.progressBar = QProgressBar(self.frame)
self.progressBar.resize(self.width() - 200 - 10, 50)
self.progressBar.move(100, self.labelDescription.y() + 130)
self.progressBar.setAlignment(Qt.AlignCenter)
self.progressBar.setFormat('%p%')
self.progressBar.setTextVisible(True)
self.progressBar.setRange(0, self.n)
self.progressBar.setValue(20)
self.labelLoading = QLabel(self.frame)
self.labelLoading.resize(self.width() - 10, 50)
self.labelLoading.move(0, self.progressBar.y() + 70)
self.labelLoading.setObjectName('LabelLoading')
self.labelLoading.setAlignment(Qt.AlignCenter)
#self.labelLoading.setText('loading...')
def loading(self):
self.progressBar.setValue(self.counter)
if self.counter == int(self.n * 0.3):
self.labelDescription.setText('<strong>Loading...</strong>')
elif self.counter == int(self.n * 0.6):
self.labelDescription.setText('<strong>Loading.....</strong>')
elif self.counter == int(self.n * 0.9):
self.labelDescription.setText('<strong>Loading.......</strong>')
elif self.counter == int(self.n * 0.9):
self.labelDescription.setText('<strong>Loading........</strong>')
elif self.counter >= self.n:
self.timer.stop()
self.close()
time.sleep(1)
mainform = Input_form()
self.mainform.show()
self.counter += 1
class Input_form(QMainWindow):
def __init__(self):
super().__init__()
self.uic.loadUi('new_mian.ui', self)
self.show()
if __name__ == '__main__':
# don't auto scale when drag app to a different monitor.
# QApplication.setAttribute(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
app = QApplication(sys.argv)
app.setStyleSheet('''
#LabelTitle {
font-size: 60px;
color: #93deed;
}
#LabelDesc {
font-size: 30px;
color: #c2ced1;
}
#LabelLoading {
font-size: 30px;
color: #e8e8eb;
}
QFrame {
background-color: black;
color: rgb(220, 220, 220);
}
QProgressBar {
background-color: #DA7B93;
color: rgb(200, 200, 200);
border-style: none;
border-radius: 10px;
text-align: center;
font-size: 30px;
}
QProgressBar::chunk {
border-radius: 10px;
background-color: qlineargradient(spread:pad x1:0, x2:1, y1:0.511364, y2:0.523, stop:0 #1C3334, stop:1 #376E6F);
}
''')
splash = SplashScreen()
splash.show()
try:
sys.exit(app.exec_())
except SystemExit:
print('Closing Window...')
After successful splash screen loading , i am trying to disply new_mian.ui (QmainWindow) form created through QT Designer and imported in code as well.
I also generated python file with this "pyuic5 new_main.ui -o new_main.py" command but its prompting error "QWidget: Must construct a QApplication before a QWidget" . I am new to Pyqt and not able to understand why it is so. Please guide. Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论