pyqt5 开发入门
使用:pyqt5-tools designer 进行窗口设计
使用命令:pyuic5 -o textedit.py textedit.ui 把设计的UI文件转换成py文件
注意:使用对话框的话要继承于class Ui_MainWindow(QtWidgets.QMainWindow):
使用信号的方法:self.actionnew.triggered.connect(self.new_btn) self.new_btn 为当前类下的方法
下面示例:
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'textedit.ui' # # Created by: PyQt5 UI code generator 5.15.2 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. import sys import os from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QFileDialog class Ui_MainWindow(QtWidgets.QMainWindow): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(820, 687) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget) self.verticalLayout.setObjectName("verticalLayout") self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.MinimumExpanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.plainTextEdit.sizePolicy().hasHeightForWidth()) self.plainTextEdit.setSizePolicy(sizePolicy) self.plainTextEdit.setObjectName("plainTextEdit") self.verticalLayout.addWidget(self.plainTextEdit) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 820, 23)) self.menubar.setObjectName("menubar") self.menu = QtWidgets.QMenu(self.menubar) self.menu.setObjectName("menu") self.menu_2 = QtWidgets.QMenu(self.menubar) self.menu_2.setObjectName("menu_2") self.menu_3 = QtWidgets.QMenu(self.menubar) self.menu_3.setObjectName("menu_3") self.menu_4 = QtWidgets.QMenu(self.menubar) self.menu_4.setObjectName("menu_4") self.menu_5 = QtWidgets.QMenu(self.menubar) self.menu_5.setObjectName("menu_5") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.actionfile = QtWidgets.QAction(MainWindow) self.actionfile.setObjectName("actionfile") self.actionnew = QtWidgets.QAction(MainWindow) self.actionnew.setObjectName("actionnew") self.actionnew.triggered.connect(self.new_btn) #点击信号 self.actionopen = QtWidgets.QAction(MainWindow) self.actionopen.setObjectName("actionopen") self.actionopen.triggered.connect(self.open_btn) #打开文件 self.actionclose = QtWidgets.QAction(MainWindow) self.actionclose.setObjectName("actionclose") self.actionexit = QtWidgets.QAction(MainWindow) self.actionexit.setObjectName("actionexit") self.actioncut = QtWidgets.QAction(MainWindow) self.actioncut.setObjectName("actioncut") self.actioncopy = QtWidgets.QAction(MainWindow) self.actioncopy.setObjectName("actioncopy") self.actionpaste = QtWidgets.QAction(MainWindow) self.actionpaste.setObjectName("actionpaste") self.actionall = QtWidgets.QAction(MainWindow) self.actionall.setObjectName("actionall") self.actiondel = QtWidgets.QAction(MainWindow) self.actiondel.setObjectName("actiondel") self.actionfont = QtWidgets.QAction(MainWindow) self.actionfont.setObjectName("actionfont") self.actionautotab = QtWidgets.QAction(MainWindow) self.actionautotab.setCheckable(True) self.actionautotab.setObjectName("actionautotab") self.actionhelp = QtWidgets.QAction(MainWindow) self.actionhelp.setObjectName("actionhelp") self.menu.addAction(self.actionnew) self.menu.addAction(self.actionopen) self.menu.addAction(self.actionclose) self.menu.addSeparator() self.menu.addAction(self.actionexit) self.menu_2.addAction(self.actioncut) self.menu_2.addAction(self.actioncopy) self.menu_2.addAction(self.actionpaste) self.menu_2.addSeparator() self.menu_2.addAction(self.actionall) self.menu_2.addAction(self.actiondel) self.menu_4.addAction(self.actionautotab) self.menu_4.addAction(self.actionfont) self.menu_5.addAction(self.actionhelp) self.menubar.addAction(self.menu.menuAction()) self.menubar.addAction(self.menu_2.menuAction()) self.menubar.addAction(self.menu_3.menuAction()) self.menubar.addAction(self.menu_4.menuAction()) self.menubar.addAction(self.menu_5.menuAction()) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.menu.setTitle(_translate("MainWindow", "文件")) self.menu_2.setTitle(_translate("MainWindow", "编辑")) self.menu_3.setTitle(_translate("MainWindow", "查看")) self.menu_4.setTitle(_translate("MainWindow", "工具")) self.menu_5.setTitle(_translate("MainWindow", "帮助")) self.actionfile.setText(_translate("MainWindow", "file")) self.actionnew.setText(_translate("MainWindow", "新建")) self.actionopen.setText(_translate("MainWindow", "打开")) self.actionclose.setText(_translate("MainWindow", "关闭")) self.actionexit.setText(_translate("MainWindow", "退出")) self.actioncut.setText(_translate("MainWindow", "剪切")) self.actioncopy.setText(_translate("MainWindow", "复制")) self.actionpaste.setText(_translate("MainWindow", "粘贴")) self.actionall.setText(_translate("MainWindow", "全选")) self.actiondel.setText(_translate("MainWindow", "删除")) self.actionfont.setText(_translate("MainWindow", "字体")) self.actionautotab.setText(_translate("MainWindow", "自动换行")) self.actionhelp.setText(_translate("MainWindow", "帮助")) def new_btn(self): print("new_btn") file, ok = QFileDialog.getSaveFileName(self, "保存文件", os.getcwd(), "All Files (*);;Text Files (*.txt)") self.plainTextEdit.setPlainText("new") self.statusbar.showMessage("新建文件") def open_btn(self): file, filetype = QFileDialog.getOpenFileName(self, "打开文件", os.getcwd(), "All Files (*);;Text Files (*.txt)") fs=open(file,"r+",encoding="utf-8") self.plainTextEdit.setPlainText(fs.read()) fs.close() self.statusbar.showMessage("新建文件") if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) # 创建一个QApplication,也就是你要开发的软件app MainWindow = QtWidgets.QMainWindow() # 创建一个QMainWindow,用来装载你需要的各种组件、控件 ui = Ui_MainWindow() # ui是你创建的ui类的实例化对象 ui.setupUi(MainWindow) # 执行类中的setupUi方法,方法的参数是第二步中创建的QMainWindow MainWindow.show() # 执行QMainWindow的show()方法,显示这个QMainWindow sys.exit(app.exec_()) # 使用exit()或者点击关闭按钮退出QApplication