PyQt5 demo界面程序1

__author__ = 'Su'
# 0. 导入需要的包和模块
from PyQt5.Qt import * # 主要包含了我们常用的一些类, 汇总到了一块
import sys

# 1. 创建一个应用程序对象
# 每一pyqt5应用程序必须创建一个应用程序对象。sys.argv参数是一个列表,从命令行输入参数。
app = QApplication(sys.argv)

# 2. 控件的操作
# 创建控件,设置控件(大小,位置,样式...),事件,信号的处
# QWidget部件是pyqt5所有用户界面对象的基类。他为QWidget提供默认构造函数。默认构造函数没有父类。
w = QWidget()
# resize()方法调整窗口的大小。这离是500px宽500px高
w.resize(500, 500)
# move()方法移动窗口在屏幕上的位置到x = 300,y = 300坐标。
w.move(300, 300)
# 设置窗口的标题
w.setWindowTitle('武汉加油')
# 显示在屏幕上


# 控件也可以作为一个容器(承载其他的控件)
label = QLabel(w)
label.setText("你好")
label.move(100, 50)
w.show()
# 3. 应用程序的执行, 进入到消息循环
# 让整个程序开始执行,并且进入到消息循环(无限循环)
# 检测整个程序所接收到的用户的交互信息
# 系统exit()方法确保应用程序干净的退出
# 的exec_()方法有下划线。因为执行是一个Python关键词。因此,exec_()代替
sys.exit(app.exec_())

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyQt5界面中嵌入Matplotlib实际上非常简单。以下是代码示例,它将Matplotlib嵌入到一个PyQt5窗口应用程序中: ```python import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QHBoxLayout, QWidget, QPushButton from PyQt5.QtGui import QIcon from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.figure import Figure class PlotCanvas(FigureCanvas): def __init__(self, parent=None, width=5, height=4, dpi=100): fig = Figure(figsize=(width, height), dpi=dpi) self.axes = fig.add_subplot(111) FigureCanvas.__init__(self, fig) self.setParent(parent) FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding) FigureCanvas.updateGeometry(self) class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("PyQt5 & Matplotlib Demo") self.setGeometry(100, 100, 800, 600) # Create the button to update the plot update_button = QPushButton("Update Plot", self) update_button.resize(100, 25) update_button.move(10, 10) update_button.clicked.connect(self.update_plot) # Create the canvas to display the plot canvas = PlotCanvas(self, width=5, height=4) canvas.move(0, 100) # Create a layout for the window layout = QVBoxLayout() layout.addWidget(update_button) layout.addWidget(canvas) # Create a central widget and set the layout central_widget = QWidget() central_widget.setLayout(layout) self.setCentralWidget(central_widget) self.update_plot() def update_plot(self): # Clear the figure self.canvas.axes.clear() # Create some data to plot x = [1, 2, 3, 4, 5] y = [3, 5, 1, 7, 9] # Plot the data self.canvas.axes.plot(x, y) # Update the canvas self.canvas.draw() if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) ``` 这段代码首先定义了一个名为PlotCanvas的类,它是一个受Matplotlib支持的Qt小部件,并用于在Matplotlib图形上绘制。接下来,MainWindow类继承自QMainWindow并定义了一个PyQt5应用程序的主窗口。在构造函数中,我们创建了一个用于更新绘图的按钮,以及一个用于在其中绘制Matplotlib图形的PlotCanvas对象。然后,我们将两个控件添加到窗口布局中并显示窗口。最后,在update_plot方法中,我们定义了要绘制的数据以及绘图命令,然后将其呈现到Matplotlib图形上并更新窗口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值