我是PyQt和Qt设计师的新手。为了测试,我使用At Designer创建了一个基本GUI。我将布局保存为test.ui
。因为我正在使用Pycharm,所以我将test.ui文件复制到了一个新的pycharm项目,并在下面的终端中执行了:pyuic5 test.ui -o testgui.py
此后aa testgui.py
出现在文件夹中,看起来像这样
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 601)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(445, 251, 221, 51))
self.textEdit.setObjectName("textEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(270, 200, 79, 24))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 19))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
没有办法打电话给班级。我试过的时候
app = QtGui.QGuiApplication(sys.argv)
window = QtGui.QtWidget()
window.show()
我收到错误:
Traceback (most recent call last):
File "/home/ravindra/PycharmProjects/PyQtGUI/testgui.py", line 41, in <module>
window = QtGui.QtWidget()
AttributeError: 'module' object has no attribute 'QtWidget'
Process finished with exit code 1
我该如何解决?请帮助
答案 0 :(得分:0)
我不能直接回答你的问题,但我可能会提出适用于我的替代方法,也许也适合你。
首先我使用qtpy,包装PyQt,你可能想尝试一下。
我也没有将.ui
文件翻译成python,而是直接导入.ui
文件:
from qtpy.QtWidgets import QMainWindow
from qtpy.uic import loadui
class Ui_MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
loadUi('test.ui', self)
然后在某处:
mainWindow = Ui_MainWindow()
mainWindow.show()
app_exec()
我认为这会使未来的ui更改变得更容易。