python之pyQt5实例:几何绘图界面

使用PyQt5设计一个界面,其中点击不同的按钮可以在画布上画出点、直线、圆和样条曲线

from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton,QHBoxLayout,QVBoxLayout,QWidget,QLabel
from PyQt5.QtGui import QPainter, QPen, QColor
from PyQt5.QtCore import Qt
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("绘图应用")
        self.setGeometry(100, 100, 400, 400)
        self.button_point = QPushButton("画点", self)
        self.button_line = QPushButton("画直线", self)
        self.button_circle = QPushButton("画圆", self)
        self.button_curve = QPushButton("画曲线", self)
        self.button_point.clicked.connect(self.draw_point)
        self.button_line.clicked.connect(self.draw_line)
        self.button_circle.clicked.connect(self.draw_circle)
        self.button_curve.clicked.connect(self.draw_curve)

        # 将按钮添加到布局中
        hlayout = QHBoxLayout()
        hlayout.addWidget(self.button_point)
        hlayout.addWidget(self.button_line)
        hlayout.addWidget(self.button_circle)
        hlayout.addWidget(self.button_curve)

        # 创建画布控件(标签)并设置其占位符文本和样式表(使其不显示边框)
        self.canvas = QLabel()
        self.canvas.setText('Canvas')
        self.canvas.setStyleSheet("border: 0px solid white; background-color: white;")
        # self.canvas.setFixedSize(280, 180)  # 设置画布大小(示例值)

        # 将画布控件添加到布局的底部,并设置其占据主窗口的大部分位置
        vlayout = QVBoxLayout()
        vlayout.addLayout(hlayout)
        vlayout.addWidget(self.canvas)

        # 创建一个容器,将其布局设置为垂直布局,并将其添加到主窗口的顶部区域(不使用setCentralWidget)
        container = QWidget()
        container.setLayout(vlayout)
        self.setFixedHeight(220)  # 设置主窗口的高度,以适应容器的布局
        self.setCentralWidget(container)  # 将容器设置为中央窗口部件(中心区域)

    def draw_point(self):
        # 实现画点功能的方法
        pass

    def draw_line(self):
        # 实现画直线功能的方法
        pass

    def draw_circle(self):
        # 实现画圆功能的方法
        pass

    def draw_curve(self):
        # 实现画曲线功能的方法
        pass
def paintEvent(self, event):
    painter = QPainter(self)
    pen = QPen()
    pen.setWidth(2)
    painter.setPen(pen)

    if self.draw_point_flag:
        painter.drawPoint(self.last_pos)

    if self.draw_line_flag:
        painter.drawLine(self.start_pos, self.end_pos)

    if self.draw_circle_flag:
        radius = max(abs(self.start_pos.x() - self.end_pos.x()), abs(self.start_pos.y() - self.end_pos.y()))
        painter.drawEllipse(self.start_pos, radius, radius)

    if self.draw_curve_flag:
        # 实现画曲线的方法
        pass
def draw_point(self):
    self.draw_point_flag = True
    self.draw_line_flag = False
    self.draw_circle_flag = False
    self.draw_curve_flag = False

def draw_line(self):
    self.draw_point_flag = False
    self.draw_line_flag = True
    self.draw_circle_flag = False
    self.draw_curve_flag = False

def draw_circle(self):
    self.draw_point_flag = False
    self.draw_line_flag = False
    self.draw_circle_flag = True
    self.draw_curve_flag = False

def draw_curve(self):
    self.draw_point_flag = False
    self.draw_line_flag = False
    self.draw_circle_flag = False
    self.draw_curve_flag = True
if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec()

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyQt5是一个用于创建 GUI 程序的工具包。下面是一个使用 PyQt5 创建简单界面的示例代码: ``` import sys from PyQt5.QtWidgets import QApplication, QWidget if __name__ == '__main__': app = QApplication(sys.argv) w = QWidget() w.resize(250, 150) w.move(300, 300) w.setWindowTitle('Simple') w.show() sys.exit(app.exec_()) ``` 这段代码会创建一个简单的窗口,可以调整大小和移动,标题为“Simple”。 如果需要更复杂的界面,可以使用 PyQt5 的布局管理器和控件,如 QHBoxLayout、QVBoxLayout、QLineEdit、QPushButton 等。 ```python import sys from PyQt5.QtWidgets import (QApplication, QHBoxLayout, QLabel, QLineEdit, QPushButton, QVBoxLayout, QWidget) class MyApp(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): nameLabel = QLabel("Name:", self) nameEdit = QLineEdit(self) ageLabel = QLabel("Age:", self) ageEdit = QLineEdit(self) okButton = QPushButton("OK", self) nameLabel.move(20, 20) nameEdit.move(70, 20) ageLabel.move(20, 50) ageEdit.move(70, 50) okButton.move(20, 80) hbox = QHBoxLayout() hbox.addStretch(1) hbox.addWidget(okButton) vbox = QVBoxLayout() vbox.addStretch(1) vbox.addWidget(nameLabel) vbox.addWidget(nameEdit) vbox.addWidget(ageLabel) vbox.addWidget(ageEdit) vbox.addLayout(hbox) self.setLayout(vbox) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Simple') self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = MyApp() sys.exit(app.exec_()) ``` 这段代码会创建一个包含包含一个姓名和年龄输入框, 一个“OK”按钮的窗口。其中使用了QHBoxLayout和QVBoxLayout进行布局,将控件QLabel、QLineEdit、QPushButton添加到布局中。窗口标题为“Simple”.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值