Ошибка: NameError: name 'QtGui' is not defined
Хотел запустить программу с графическим интерфейсом.
if __name__ == "__main__": (и то что внутри) написал вручную, потому что после конвертации (.ui в .py) этого кода не было.
После запуска программы выходит ошибка
NameError: name 'QtGui' is not defined
Что не так?
from PySide2.QtCore import (QCoreApplication, QDate, QDateTime, QMetaObject,
QObject, QPoint, QRect, QSize, QTime, QUrl, Qt)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
QFontDatabase, QIcon, QKeySequence, QLinearGradient, QPalette, QPainter,
QPixmap, QRadialGradient)
from PySide2.QtWidgets import *
class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(326, 206)
self.pushButton = QPushButton(Form)
self.pushButton.setObjectName(u"pushButton")
self.pushButton.setGeometry(QRect(130, 110, 75, 23))
self.label = QLabel(Form)
self.label.setObjectName(u"label")
self.label.setGeometry(QRect(80, 30, 381, 31))
self.label.setStyleSheet(u"font-size: 20px;")
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
# setupUi
def retranslateUi(self, Form):
Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
self.pushButton.setText(QCoreApplication.translate("Form", u"PushButton", None))
self.label.setText(QCoreApplication.translate("Form", u"TextLabel", None))
# retranslateUi
if __name__ == "__main__":
app = QtGui.QCoreApplication(sys.argv)
Form = QtGui.QtWidgets()
ui = Ui_widget()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
я же вам написал Form = QWidget(), а не Form = QtWidgets()
В общем ваше приложение может выглядеть так, закомментируйте мои импорты и раскомментируйте свои. Я отметил строки, в которые внес изменения.
'''
from PySide2.QtCore import (QCoreApplication, QDate, QDateTime, QMetaObject,
QObject, QPoint, QRect, QSize, QTime, QUrl, Qt)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
QFontDatabase, QIcon, QKeySequence, QLinearGradient, QPalette, QPainter,
QPixmap, QRadialGradient)
from PySide2.QtWidgets import QApplication, QWidget, QPushButton, QLabel
'''
from PyQt5.QtCore import (QCoreApplication, QDate, QDateTime, QMetaObject,
QObject, QPoint, QRect, QSize, QTime, QUrl, Qt)
from PyQt5.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
QFontDatabase, QIcon, QKeySequence, QLinearGradient, QPalette, QPainter,
QPixmap, QRadialGradient)
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel
class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(326, 206)
self.pushButton = QPushButton(Form)
self.pushButton.setObjectName(u"pushButton")
self.pushButton.setGeometry(QRect(130, 110, 75, 23))
self.label = QLabel(Form)
self.label.setObjectName(u"label")
self.label.setGeometry(QRect(80, 30, 381, 31))
self.label.setStyleSheet(u"font-size: 20px;")
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QApplication.translate("Form", u"Form", None))
self.pushButton.setText(QApplication.translate("Form", u"PushButton", None))
self.label.setText(QApplication.translate("Form", u"TextLabel", None))
if __name__ == "__main__":
import sys # +++
# app = QtGui.QCoreApplication(sys.argv) # ---
app = QApplication(sys.argv) # +++
# Form = QtGui.QtWidgets() # ---
Form = QWidget() # +++
# ui = Ui_widget() # ---
ui = Ui_Form() # +++
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
Но учитывая, что вы вероятно захотите оживить свое приложение и добавить какую-то логику, я вам предложу попробовать вариант как правильно это сделать.
'''
from PySide2.QtCore import (QCoreApplication, QDate, QDateTime, QMetaObject,
QObject, QPoint, QRect, QSize, QTime, QUrl, Qt)
from PySide2.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
QFontDatabase, QIcon, QKeySequence, QLinearGradient, QPalette, QPainter,
QPixmap, QRadialGradient)
from PySide2.QtWidgets import QApplication, QWidget, QPushButton, QLabel
'''
from PyQt5.QtCore import (QCoreApplication, QDate, QDateTime, QMetaObject,
QObject, QPoint, QRect, QSize, QTime, QUrl, Qt)
from PyQt5.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont,
QFontDatabase, QIcon, QKeySequence, QLinearGradient, QPalette, QPainter,
QPixmap, QRadialGradient)
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel
class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(326, 206)
self.pushButton = QPushButton(Form)
self.pushButton.setObjectName(u"pushButton")
self.pushButton.setGeometry(QRect(130, 110, 75, 23))
self.label = QLabel(Form)
self.label.setObjectName(u"label")
self.label.setGeometry(QRect(80, 30, 381, 31))
self.label.setStyleSheet(u"font-size: 20px;")
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(QApplication.translate("Form", u"Form", None))
self.pushButton.setText(QApplication.translate("Form", u"PushButton", None))
self.label.setText(QApplication.translate("Form", u"TextLabel", None))
class Window(QWidget, Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
# тут пишем логику приложения
self.pushButton.clicked.connect(self.onClicked)
def onClicked(self):
print(f'Вы нажали кнопку `PushButton`')
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
