问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

python pyqt怎么画圆

发布网友 发布时间:2022-04-21 23:52

我来回答

1个回答

热心网友 时间:2022-04-06 15:55

这个例子我做了好几天:

1)官网C++的源码,改写成PyQt5版本的代码,好多细节不会转化

2)网上的PyQt的例子根本运行不了

填了无数个坑,结合二者,终于能完成了一个关于绘图的东西。这个过程也掌握了很多新的知识点

【知识点】

1、关于多个点的使用

poitns = [QPoint(10, 80), QPoint(20, 10), QPoint(80, 30), QPoint(90, 70)]

请看:

1      # 定义多个点 2      points = [QPoint(10, 80), QPoint(20, 10), QPoint(80, 30), QPoint(90, 70)] 3  4      # ===直接使用 points 会报错!========= 5      # ... 6      elif self.shape == self.Points: 7            painter.drawPoints(points) 8  9      elif self.shape == self.Polyline:10            painter.drawPolyline(points)11 12      elif self.shape == self.Polygon:13            painter.drawPolygon(points, 4)14 15      # ...16 17      # ===把 points 用 QPolygon()包裹起来才正确!=========18      # ...19      elif self.shape == self.Points:20            painter.drawPoints(QPolygon(points))21 22      elif self.shape == self.Polyline:23            painter.drawPolyline(QPolygon(points))24 25      elif self.shape == self.Polygon:26            painter.drawPolygon(QPolygon(points), 4)27 28      # ...

2、在QDialog窗体中显示QWidget部件

【效果图】

【资源】

【代码】

import sysfrom PyQt5.QtCore import *from PyQt5.QtGui import *from PyQt5.QtWidgets import *class StockDialog(QWidget):    def __init__(self, parent=None):
super(StockDialog, self).__init__(parent)
self.setWindowTitle("利用QPainter绘制各种图形")

mainSplitter = QSplitter(Qt.Horizontal)
mainSplitter.setOpaqueResize(True)

frame = QFrame(mainSplitter)
mainLayout = QGridLayout(frame)        #mainLayout.setMargin(10)
mainLayout.setSpacing(6)

label1=QLabel("形状:")
label2=QLabel("画笔线宽:")
label3=QLabel("画笔颜色:")
label4=QLabel("画笔风格:")
label5=QLabel("画笔顶端:")
label6=QLabel("画笔连接点:")
label7=QLabel("画刷风格:")
label8=QLabel("画刷颜色:")

self.shapeComboBox = QComboBox()
self.shapeComboBox.addItem("Line", "Line")
self.shapeComboBox.addItem("Rectangle", "Rectangle")
self.shapeComboBox.addItem('Rounded Rectangle','Rounded Rectangle')
self.shapeComboBox.addItem('Ellipse','Ellipse')
self.shapeComboBox.addItem('Pie','Pie')
self.shapeComboBox.addItem('Chord','Chord')
self.shapeComboBox.addItem('Path','Path')
self.shapeComboBox.addItem('Polygon','Polygon')
self.shapeComboBox.addItem('Polyline','Polyline')
self.shapeComboBox.addItem('Arc','Arc')
self.shapeComboBox.addItem('Points','Points')
self.shapeComboBox.addItem('Text','Text')
self.shapeComboBox.addItem('Pixmap','Pixmap')

self.widthSpinBox = QSpinBox()
self.widthSpinBox.setRange(0,20)

self.penColorFrame = QFrame()
self.penColorFrame.setAutoFillBackground(True)
self.penColorFrame.setPalette(QPalette(Qt.blue))
self.penColorPushButton = QPushButton("更改")

self.penStyleComboBox = QComboBox()
self.penStyleComboBox.addItem("Solid",Qt.SolidLine)
self.penStyleComboBox.addItem('Dash',  Qt.DashLine)
self.penStyleComboBox.addItem('Dot',  Qt.DotLine)
self.penStyleComboBox.addItem('Dash Dot',  Qt.DashDotLine)
self.penStyleComboBox.addItem('Dash Dot Dot',  Qt.DashDotDotLine)
self.penStyleComboBox.addItem('None',  Qt.NoPen)

self.penCapComboBox = QComboBox()
self.penCapComboBox.addItem("Flat",Qt.FlatCap)
self.penCapComboBox.addItem('Square', Qt.SquareCap)
self.penCapComboBox.addItem('Round', Qt.RoundCap)

self.penJoinComboBox = QComboBox()
self.penJoinComboBox.addItem("Miter",Qt.MiterJoin)
self.penJoinComboBox.addItem('Bebel', Qt.BevelJoin)
self.penJoinComboBox.addItem('Round', Qt.RoundJoin)

self.brushStyleComboBox = QComboBox()
self.brushStyleComboBox.addItem("Linear Gradient",Qt.LinearGradientPattern)
self.brushStyleComboBox.addItem('Radial Gradient', Qt.RadialGradientPattern)
self.brushStyleComboBox.addItem('Conical Gradient', Qt.ConicalGradientPattern)
self.brushStyleComboBox.addItem('Texture', Qt.TexturePattern)
self.brushStyleComboBox.addItem('Solid', Qt.SolidPattern)
self.brushStyleComboBox.addItem('Horizontal', Qt.HorPattern)
self.brushStyleComboBox.addItem('Vertical', Qt.VerPattern)
self.brushStyleComboBox.addItem('Cross', Qt.CrossPattern)
self.brushStyleComboBox.addItem('Backward Diagonal', Qt.BDiagPattern)
self.brushStyleComboBox.addItem('Forward Diagonal', Qt.FDiagPattern)
self.brushStyleComboBox.addItem('Diagonal Cross', Qt.DiagCrossPattern)
self.brushStyleComboBox.addItem('Dense 1', Qt.Dense1Pattern)
self.brushStyleComboBox.addItem('Dense 2', Qt.Dense2Pattern)
self.brushStyleComboBox.addItem('Dense 3', Qt.Dense3Pattern)
self.brushStyleComboBox.addItem('Dense 4', Qt.Dense4Pattern)
self.brushStyleComboBox.addItem('Dense 5', Qt.Dense5Pattern)
self.brushStyleComboBox.addItem('Dense 6', Qt.Dense6Pattern)
self.brushStyleComboBox.addItem('Dense 7', Qt.Dense7Pattern)
self.brushStyleComboBox.addItem('None', Qt.NoBrush)

self.brushColorFrame = QFrame()
self.brushColorFrame.setAutoFillBackground(True)
self.brushColorFrame.setPalette(QPalette(Qt.green))
self.brushColorPushButton = QPushButton("更改")

labelCol=0
contentCol=1
#建立布局
mainLayout.addWidget(label1,1,labelCol)
mainLayout.addWidget(self.shapeComboBox,1,contentCol)
mainLayout.addWidget(label2,2,labelCol)
mainLayout.addWidget(self.widthSpinBox,2,contentCol)
mainLayout.addWidget(label3,4,labelCol)
mainLayout.addWidget(self.penColorFrame,4,contentCol)
mainLayout.addWidget(self.penColorPushButton,4,3)
mainLayout.addWidget(label4,6,labelCol)
mainLayout.addWidget(self.penStyleComboBox,6,contentCol)
mainLayout.addWidget(label5,8,labelCol)
mainLayout.addWidget(self.penCapComboBox,8,contentCol)
mainLayout.addWidget(label6,10,labelCol)
mainLayout.addWidget(self.penJoinComboBox,10,contentCol)
mainLayout.addWidget(label7,12,labelCol)
mainLayout.addWidget(self.brushStyleComboBox,12,contentCol)
mainLayout.addWidget(label8,14,labelCol)
mainLayout.addWidget(self.brushColorFrame,14,contentCol)
mainLayout.addWidget(self.brushColorPushButton,14,3)
mainSplitter1 = QSplitter(Qt.Horizontal)
mainSplitter1.setOpaqueResize(True)

stack1 = QStackedWidget()
stack1.setFrameStyle(QFrame.Panel|QFrame.Raised)
self.area = PaintArea()
stack1.addWidget(self.area)
frame1 = QFrame(mainSplitter1)
mainLayout1 = QVBoxLayout(frame1)        #mainLayout1.setMargin(10)
mainLayout1.setSpacing(6)
mainLayout1.addWidget(stack1)

layout = QGridLayout(self)
layout.addWidget(mainSplitter1,0,0)
layout.addWidget(mainSplitter,0,1)
self.setLayout(layout)
#信号和槽函数        self.shapeComboBox.activated.connect(self.slotShape)
self.widthSpinBox.valueChanged.connect(self.slotPenWidth)
self.penColorPushButton.clicked.connect(self.slotPenColor)
self.penStyleComboBox.activated.connect(self.slotPenStyle)
self.penCapComboBox.activated.connect(self.slotPenCap)
self.penJoinComboBox.activated.connect(self.slotPenJoin)
self.brushStyleComboBox.activated.connect(self.slotBrush)
self.brushColorPushButton.clicked.connect(self.slotBrushColor)

self.slotShape(self.shapeComboBox.currentIndex())
self.slotPenWidth(self.widthSpinBox.value())
self.slotBrush(self.brushStyleComboBox.currentIndex())

def slotShape(self,value):
shape =  self.area.Shape[value]
self.area.setShape(shape)
def slotPenWidth(self,value):
color = self.penColorFrame.palette().color(QPalette.Window)
style = Qt.PenStyle(self.penStyleComboBox.itemData(self.penStyleComboBox.currentIndex(),Qt.UserRole))
cap = Qt.PenCapStyle(self.penCapComboBox.itemData(self.penCapComboBox.currentIndex(),Qt.UserRole))
join = Qt.PenJoinStyle(self.penJoinComboBox.itemData(self.penJoinComboBox.currentIndex(),Qt.UserRole))
self.area.setPen(QPen(color,value,style,cap,join))
def slotPenStyle(self,value):
self.slotPenWidth(value)
def slotPenCap(self,value):
self.slotPenWidth(value)
def slotPenJoin(self,value):
self.slotPenWidth(value)
def slotPenColor(self):
color = QColorDialog.getColor(Qt.blue)
self.penColorFrame.setPalette(QPalette(color))
self.area.setPen(QPen(color))
def slotBrushColor(self):
color = QColorDialog.getColor(Qt.blue)
self.brushColorFrame.setPalette(QPalette(color))
self.slotBrush(self.brushStyleComboBox.currentIndex())
def slotBrush(self,value):
color = self.brushColorFrame.palette().color(QPalette.Window)
style = Qt.BrushStyle(self.brushStyleComboBox.itemData(value,Qt.UserRole))
if(style == Qt.Lin

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
临沂比较有名的男装品牌 呼伦贝尔市悦动网络科技有限公司怎么样? 呼伦贝尔中汇实业有限公司怎么样? 呼伦贝尔油玉不绝电子商务有限公司怎么样? 如何避免wps卡顿? 属鼠的男人找对象是属什么,属鼠的人和什么属相合 96年鼠的姻缘在哪年 属相相合年份运势提升 2024属鼠找对象属什么最佳 黑客攻击网站能报案吗 黑客攻击报案有用吗 python 的qlabel的mousepressevent怎么用 python如何用pyqt5打开视频(还有PPT)并输出到QLa... PYQT用qlabel循环显示图片会卡住 微信为什么会疑似身份冒用怎么样恢复华为? 睿因路由器拨号超时 睿因路由器不知密码怎么蹭网 我的是睿因路由器,能连接的上,但是上不了网 睿因路由wn578r2怎么升级固件? 普通面粉变高筋面粉 高筋面条是转基因面条吗 高筋面粉配制 睿因的路由器wan灯不亮怎么回事 睿因路由器sys灯长亮,上不了网怎么办? 面粉有转基因吗? 睿因路由器怎么连接机顶盒 面粉有转基因吗 有人说高筋面粉是转基因的,究竟是不是真的? 高筋面粉是转基因小麦做的吗 睿因S32 路由器怎么样?好吗 高筋面粉是转基因面粉吗? 用PYQT设计好了界面,用另一PY文件调用,要怎么做? Qt 中如何改变Label大小 pyqt5 listwidget怎么指定item向右对齐 wxPython和PyQt谁才是最赞的Python GUI库 QT怎么获得label实时坐标 python3.6.3怎么安装pip和pygame,要详细 QT4 如何在 QLabel 上显示二值图和灰度图 ??? PyInstaller 无法打包完成后,运行exe无反应? QT编程的QDialog对话框右上角的问号按钮如何取消呢 QT中控件如何随着窗口大小的改变而使它充满整个窗... 如何在PyQt5中的QLabel中显示摄像头 PyQt5 将图片读取到内存后怎么显示 苹果手机丢了如何查询一天的移动路线? 苹果手机信息中心在哪找不到 无需手机号怎么注册? 苹果6plus怎么在主屏幕上显示时间天气 不用手机号码可以注册吗? 苹果手机天气预报如何设置? 苹果手机怎样不在通知栏里显示天气 苹果4s手机为什么不显示小时预报