安装 pip3 install PyQt5
代码说明:
1、classShowWindow(QWidget):
QWidget是PyQt中非常重要的通用窗口类,是所有用户界面对象的基类,所有和用户界面相关的控件类都是继承自QWidger类。
2、def__init__(self):
super(ShowWindow,self).__init__()
self.initUI()
ShowWindow类的构造函数 init,由于ShowWindow继承自QWidgert类,因此在构造函数中调用父类QWidget的构造函数super.
init()。
同时在构造函数中调用自定义的初始化函数initUI(),在该函数中初始化GUI中所需各个控件。
3、self.selectButton.clicked.connect(self.selectFile)
通过connect()方法将点击事件和处理逻辑关联起来
这个涉及到PyQt信号与槽的概念,信号和槽用于对象之间的通信。当指定事件发生,一个事件信号会被发射。槽可以被任何Python
脚本调用。当和槽连接的信号被发射时,槽会被调用。
inputLayout = QHBoxLayout()
inputLayout.addWidget(self.inputLabel)
inputLayout.addWidget(self.editLine)
创建一个inputLayout的水平布局(QHBoxLayout),在inputLayout中添加已定义的控件inputLabel和editLine。
QHBoxLayout让添加的控件水平排布
4、self.setLayout(mainLayout)
通过将setLayout()方法,将mainLayout设置为窗口layout。
5、if __name__ == '__main__':
app = QApplication(sys.argv)
ex = ShowWindow()
sys.exit(app.exec_())
程序运行的入口函数类似C语言中的main()方法。
app = QApplication(sys.argv),每一个pyqt程序必须创建一个application对象,sys.argv
是命令行参数,可以通过命令启动的时候传递参数。
ex = ShowWindow(),创建ShowWindow对象。
sys.exit(app.exec_()),app.exec_()事件处理开始,进入程序主循环。主循环等待时间,并把事件发送给指定处理的任务中。当调用
app.exit()或者程序因为各种原因被破坏后,使用sys.exit()关闭程序,并释放内存资源。
# -*-coding:UTF-8 -*- import matplotlib # Make sure that we are using QT5
matplotlib.use('Qt5Agg') from PyQt5.QtWidgets import QApplication, QWidget,
QLabel, QHBoxLayout, QPushButton, QLineEdit, QVBoxLayout,QFileDialog import sys
import pylab as pl import matplotlib.pyplot as plt from
matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
pl.mpl.rcParams['font.sans-serif'] = ['SimHei']#指定默认字体 class
ShowWindow(QWidget): def __init__(self): super(ShowWindow, self).__init__()
self.initUI() def initUI(self): #创建控件对象 self.inputLabel = QLabel("请输入文件路径:")
self.editLine = QLineEdit() self.selectButton = QPushButton("...")
self.selectButton.clicked.connect(self.selectFile) inputLayout = QHBoxLayout()
#水平 inputLayout.addWidget(self.inputLabel) inputLayout.addWidget(self.editLine)
inputLayout.addWidget(self.selectButton) # a figure instance to plot on
self.figure = plt.figure() # this is the Canvas Widget that displays the
`figure` # it takes the `figure` instance as a parameter to __init__
self.canvas = FigureCanvas(self.figure) # this is the Navigation widget # it
takes the Canvas widget and a parent self.button = QPushButton('Plot')
self.button.clicked.connect(self.plotText) plotLayout = QVBoxLayout() # 垂直
plotLayout.addWidget(self.canvas) plotLayout.addWidget(self.button) mainLayout
= QVBoxLayout() mainLayout.addLayout(inputLayout)
mainLayout.addLayout(plotLayout) self.setLayout(mainLayout) self.show()
#选择图形数据文件存放地址 def selectFile(self): fileName1, filetype =
QFileDialog.getOpenFileName(self, "选取文件", "./","All Files (*);;Text Files
(*.txt)") # 设置文件扩展名过滤,注意用双分号间隔 print(fileName1, filetype)
self.editLine.setText(fileName1) #画图 def plotText(self): X=[] Y=[] input_file =
self.editLine.text() #input_file = r'E:\pyqt\1.txt' with open(input_file,'r')
as f: for line in f.readlines(): data = line[:-1].split(',') #不含最后一个换行符
X.append(data[0]) Y.append(data[1]) Ax = self.figure.add_subplot(111) # Create
a `axes' instance in the figure Ax.clear() Ax.set_ylabel('角度')
Ax.set_xlabel('时间') Ax.plot(X, Y) self.canvas.draw() #更新画布 if __name__ ==
'__main__': app = QApplication(sys.argv) ex = ShowWindow() sys.exit(app.exec_())
热门工具 换一换