众所周知,C#语言在开发窗口类程序的时候既方便又快捷,我们在使用C#编写窗口类程序的时候,只要新建窗口,拖动控件即可完成窗口界面的设计。其实,使用python照样可以采用拖控件的方式来快速完成form程序的开发,节省了大量的开发时间,可以说是python爱好者的福音,本文将介绍pyQt_designer+pycharm开发python窗口程序的详细过程,一方面作为本人的学习笔记,另一方面也为大家提供参考。如有表述不当,请留言指正!
(1)pyQt_designer(下载地址:https://www.qt.io/download)
(2)PyCharm Edition(下载地址:http://www.jetbrains.com/pycharm/)
(1)在PyCharm 中配置pyqt:
方法一:打开Anaconda Prompt,输入pip install pyqt5,既可自动完成下载安装(适用于网络较为稳定的环境);
方法二:下载pyqt5源码包,然后进入包目录,执行命令python setup.py install命令安装。该方法需要安装setuptools。
(2)使用Qt_designer设计窗口:
打开安装好的pyQt_designer,新建一个窗口文件(这里可以选择Widget或者Main Window,窗口可以拉伸大小);
在软件的左侧选择你需要的空间,拖动到窗口中合适的位置,选中控件即可在右侧设置它的样式,设计的过程还可以Ctrl + R查看设计效果;
(3)生成python代码:
将Qt_designer设计好的窗口文件(.ui文件),保存到特定的文件夹;
打开Anaconda Prompt,使用cd命令进入到保存目录下,使用命令pyuic5 -o xxx.py xxx.ui,即可将设计好的窗口文件转化成python代码;
将生成的.py文件用文本编辑器打开,复制代码到pycharm,即可直接运行。接下来就可以为控件添加响应事件了。
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'test_2.2.0.ui' # # Created by: PyQt5 UI code generator 5.10.1 # # WARNING! All changes made in this file will be lost! from PyQt5.QtWidgets import * from tkinter import * from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form0(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(702, 361) self.pushButton1 = QtWidgets.QPushButton(Form) self.pushButton1.setGeometry(QtCore.QRect(370, 70, 81, 31)) font = QtGui.QFont() font.setFamily("华文中宋") font.setBold(False) font.setWeight(50) self.pushButton1.setFont(font) self.pushButton1.setObjectName("pushButton1") self.label3 = QtWidgets.QLabel(Form) self.label3.setGeometry(QtCore.QRect(380, 210, 61, 16)) font = QtGui.QFont() font.setFamily("等线") self.label3.setFont(font) self.label3.setObjectName("label3") self.label4 = QtWidgets.QLabel(Form) self.label4.setGeometry(QtCore.QRect(480, 210, 54, 12)) font = QtGui.QFont() font.setFamily("等线") self.label4.setFont(font) self.label4.setObjectName("label4") self.pushButton0 = QtWidgets.QPushButton(Form) self.pushButton0.setGeometry(QtCore.QRect(150, 70, 81, 31)) font = QtGui.QFont() font.setFamily("华文中宋") font.setBold(False) font.setWeight(50) self.pushButton0.setFont(font) self.pushButton0.setObjectName("pushButton0") self.lineEdit5_1_2 = QtWidgets.QLineEdit(Form) self.lineEdit5_1_2.setGeometry(QtCore.QRect(350, 270, 101, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit5_1_2.setFont(font) self.lineEdit5_1_2.setObjectName("lineEdit5_1_2") self.lineEdit3 = QtWidgets.QLineEdit(Form) self.lineEdit3.setGeometry(QtCore.QRect(60, 120, 231, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit3.setFont(font) self.lineEdit3.setObjectName("lineEdit3") self.lineEdit1 = QtWidgets.QLineEdit(Form) self.lineEdit1.setGeometry(QtCore.QRect(250, 70, 111, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit1.setFont(font) self.lineEdit1.setObjectName("lineEdit1") self.lineEdit5_1_1 = QtWidgets.QLineEdit(Form) self.lineEdit5_1_1.setGeometry(QtCore.QRect(250, 270, 101, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit5_1_1.setFont(font) self.lineEdit5_1_1.setObjectName("lineEdit5_1_1") self.label8 = QtWidgets.QLabel(Form) self.label8.setGeometry(QtCore.QRect(50, 310, 91, 20)) font = QtGui.QFont() font.setFamily("微软雅黑") self.label8.setFont(font) self.label8.setObjectName("label8") self.lineEdit5_0_1 = QtWidgets.QLineEdit(Form) self.lineEdit5_0_1.setGeometry(QtCore.QRect(250, 230, 101, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit5_0_1.setFont(font) self.lineEdit5_0_1.setObjectName("lineEdit5_0_1") self.lineEdit5_1_0 = QtWidgets.QLineEdit(Form) self.lineEdit5_1_0.setGeometry(QtCore.QRect(150, 270, 101, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit5_1_0.setFont(font) self.lineEdit5_1_0.setObjectName("lineEdit5_1_0") self.label5 = QtWidgets.QLabel(Form) self.label5.setGeometry(QtCore.QRect(560, 210, 81, 16)) font = QtGui.QFont() font.setFamily("等线") self.label5.setFont(font) self.label5.setObjectName("label5") self.pushButton4 = QtWidgets.QPushButton(Form) self.pushButton4.setGeometry(QtCore.QRect(300, 160, 111, 31)) font = QtGui.QFont() font.setFamily("华文中宋") font.setBold(False) font.setWeight(50) self.pushButton4.setFont(font) self.pushButton4.setObjectName("pushButton4") self.lineEdit5_1_3 = QtWidgets.QLineEdit(Form) self.lineEdit5_1_3.setGeometry(QtCore.QRect(450, 270, 101, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit5_1_3.setFont(font) self.lineEdit5_1_3.setObjectName("lineEdit5_1_3") self.label0 = QtWidgets.QLabel(Form) self.label0.setGeometry(QtCore.QRect(260, 20, 221, 16)) font = QtGui.QFont() font.setFamily("隶书") font.setPointSize(14) font.setBold(True) font.setWeight(75) self.label0.setFont(font) self.label0.setObjectName("label0") self.lineEdit5_0_0 = QtWidgets.QLineEdit(Form) self.lineEdit5_0_0.setGeometry(QtCore.QRect(150, 230, 101, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit5_0_0.setFont(font) self.lineEdit5_0_0.setObjectName("lineEdit5_0_0") self.lineEdit5_0_3 = QtWidgets.QLineEdit(Form) self.lineEdit5_0_3.setGeometry(QtCore.QRect(450, 230, 101, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit5_0_3.setFont(font) self.lineEdit5_0_3.setObjectName("lineEdit5_0_3") self.lineEdit5_1_4 = QtWidgets.QLineEdit(Form) self.lineEdit5_1_4.setGeometry(QtCore.QRect(550, 270, 101, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit5_1_4.setFont(font) self.lineEdit5_1_4.setObjectName("lineEdit5_1_4") self.pushButton2 = QtWidgets.QPushButton(Form) self.pushButton2.setGeometry(QtCore.QRect(590, 70, 81, 31)) font = QtGui.QFont() font.setFamily("华文中宋") font.setBold(False) font.setWeight(50) self.pushButton2.setFont(font) self.pushButton2.setObjectName("pushButton2") self.lineEdit0 = QtWidgets.QLineEdit(Form) self.lineEdit0.setGeometry(QtCore.QRect(30, 70, 111, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit0.setFont(font) self.lineEdit0.setObjectName("lineEdit0") self.lineEdit2 = QtWidgets.QLineEdit(Form) self.lineEdit2.setGeometry(QtCore.QRect(470, 70, 111, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit2.setFont(font) self.lineEdit2.setObjectName("lineEdit2") self.pushButton3 = QtWidgets.QPushButton(Form) self.pushButton3.setGeometry(QtCore.QRect(300, 120, 111, 31)) font = QtGui.QFont() font.setFamily("华文中宋") font.setBold(False) font.setWeight(50) self.pushButton3.setFont(font) self.pushButton3.setObjectName("pushButton3") self.lineEdit4 = QtWidgets.QLineEdit(Form) self.lineEdit4.setGeometry(QtCore.QRect(60, 160, 231, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit4.setFont(font) self.lineEdit4.setObjectName("lineEdit4") self.label7 = QtWidgets.QLabel(Form) self.label7.setGeometry(QtCore.QRect(50, 280, 91, 20)) font = QtGui.QFont() font.setFamily("微软雅黑") self.label7.setFont(font) self.label7.setObjectName("label7") self.label2 = QtWidgets.QLabel(Form) self.label2.setGeometry(QtCore.QRect(280, 210, 61, 16)) font = QtGui.QFont() font.setFamily("等线") self.label2.setFont(font) self.label2.setObjectName("label2") self.label1 = QtWidgets.QLabel(Form) self.label1.setGeometry(QtCore.QRect(180, 210, 54, 12)) font = QtGui.QFont() font.setFamily("等线") self.label1.setFont(font) self.label1.setObjectName("label1") self.lineEdit5_0_4 = QtWidgets.QLineEdit(Form) self.lineEdit5_0_4.setGeometry(QtCore.QRect(550, 230, 101, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit5_0_4.setFont(font) self.lineEdit5_0_4.setObjectName("lineEdit5_0_4") self.lineEdit5_0_2 = QtWidgets.QLineEdit(Form) self.lineEdit5_0_2.setGeometry(QtCore.QRect(350, 230, 101, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit5_0_2.setFont(font) self.lineEdit5_0_2.setObjectName("lineEdit5_0_2") self.lineEdit5_2_3 = QtWidgets.QLineEdit(Form) self.lineEdit5_2_3.setGeometry(QtCore.QRect(450, 300, 101, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit5_2_3.setFont(font) self.lineEdit5_2_3.setObjectName("lineEdit5_2_3") self.lineEdit5_2_1 = QtWidgets.QLineEdit(Form) self.lineEdit5_2_1.setGeometry(QtCore.QRect(250, 300, 101, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit5_2_1.setFont(font) self.lineEdit5_2_1.setObjectName("lineEdit5_2_1") self.lineEdit5_2_2 = QtWidgets.QLineEdit(Form) self.lineEdit5_2_2.setGeometry(QtCore.QRect(350, 300, 101, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit5_2_2.setFont(font) self.lineEdit5_2_2.setObjectName("lineEdit5_2_2") self.lineEdit5_2_4 = QtWidgets.QLineEdit(Form) self.lineEdit5_2_4.setGeometry(QtCore.QRect(550, 300, 101, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit5_2_4.setFont(font) self.lineEdit5_2_4.setObjectName("lineEdit5_2_4") self.lineEdit5_2_0 = QtWidgets.QLineEdit(Form) self.lineEdit5_2_0.setGeometry(QtCore.QRect(150, 300, 101, 31)) font = QtGui.QFont() font.setFamily("等线") self.lineEdit5_2_0.setFont(font) self.lineEdit5_2_0.setObjectName("lineEdit5_2_0") self.label6 = QtWidgets.QLabel(Form) self.label6.setGeometry(QtCore.QRect(50, 240, 91, 20)) font = QtGui.QFont() font.setFamily("微软雅黑") self.label6.setFont(font) self.label6.setObjectName("label6") self.pushButton5 = QtWidgets.QPushButton(Form) self.pushButton5.setGeometry(QtCore.QRect(440, 130, 201, 51)) font = QtGui.QFont() font.setFamily("华文中宋") font.setPointSize(12) font.setBold(False) font.setWeight(50) self.pushButton5.setFont(font) self.pushButton5.setObjectName("pushButton5") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.pushButton1.setText(_translate("Form", "计算百分比")) self.label3.setText(_translate("Form", "轴向位移2")) self.label4.setText(_translate("Form", "圆周位移")) self.pushButton0.setText(_translate("Form", "获取拐点")) self.label8.setText(_translate("Form", "70%匹配行数据:")) self.label5.setText(_translate("Form", "平均轴向位移")) self.pushButton4.setText(_translate("Form", "计算弹性模量")) self.label0.setText(_translate("Form", "
自动获取拐点计算模块
")) self.pushButton2.setText(_translate("Form", "匹配文件行")) self.pushButton3.setText(_translate("Form", "计算泊松比")) self.label7.setText(_translate("Form", "30%匹配行数据:")) self.label2.setText(_translate("Form", "轴向位移1")) self.label1.setText(_translate("Form", "轴向载荷")) self.label6.setText(_translate("Form", "拐点所在行数据:")) self.pushButton5.setText(_translate("Form", "生成结果文件")) class computeModule_0_window(QtWidgets.QMainWindow,Ui_Form0): def __init__(self): super(computeModule_0_window,self).__init__() self.setupUi(self) self.setWindowTitle('自动获取拐点计算模块') window_pale = QtGui.QPalette() window_pale.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap("background_img/img2.jpg"))) self.setPalette(window_pale) if __name__ == '__main__': app = QApplication(sys.argv) w = computeModule_0_window() w.show() sys.exit(app.exec_())
运行效果如下(本人添加了背景图片之后的效果):
相比C#,pyQt_designer+pycharm的开发方式虽然多了一步代码转化的过程,但是总的来说,已经使得使用python开发窗口变得非常方便了,希望对你有用。