需要注意,转换后的文件不包括以下东西:
类的程序入口代码(面对对象编程下,用pyuic转换后只是你软件的图形化的类模块而已)
类中函数入口
把objects改成QMainWindow
还有关键是:
self.show()
我们来看看转换后的样子
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Im1.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(1024, 657)
self.send = QtWidgets.QPushButton(Dialog)
self.send.setGeometry(QtCore.QRect(943, 610, 82, 51))
self.send.setObjectName("send")
self.retranslateUi(Dialog)#本地化
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "测试页"))
self.send.setText(_translate("Dialog", "发送"))
首先看看启动方法:
1.添加if name == 'main’启动类的初始化
if __name__ == '__main__':
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
Imapp = QApplication(sys.argv)
ex = Ui_IM()
ex.show()
sys.exit(Imapp.exec_())
2.在类中开始添加
def __init__(self):
super().__init__()
self.setupUi(self)
3.添加self.show()
或者在
if __name__ == '__main__':
中添加
ex.show()
4.修改
class Ui_Dialog(object):
为
class Ui_Dialog(QMainWindow):
5.导入包
from PyQt5.QtWidgets import QApplication,QMainWindow
完整代码:
from PyQt5 import QtCore, QtGui, QtWidgets,Qt
from PyQt5.QtWidgets import QApplication, QPushButton, QMenu,QLineEdit,QMainWindow,QLabel
from PyQt5.QtCore import QCoreApplication,QTimer,QThread,pyqtSignal
from PyQt5.QtGui import QIcon, QPainter, QPixmap,QPalette,QBrush
import sys
class Ui_Dialog(QMainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(1024, 657)
Dialog.setStyleSheet("")
self.send = QtWidgets.QPushButton(Dialog)
self.send.setGeometry(QtCore.QRect(943, 610, 82, 51))
self.send.setObjectName("send")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
#self.show()
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.send.setText(_translate("Dialog", "发送"))
if __name__ == '__main__':
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
test = QApplication(sys.argv)
ex = Ui_IM()
ex.show()
sys.exit(test.exec_())
代码可以直接复制运行