Python PyQt5带宽“的牵引弧;钢笔“;
我正在尝试使用PyQt5绘制一个圆量规(MacOS 11.0.1,Python 3.9)。 我使用drawArc语句创建了仪表背景,因此我将笔宽设置为一个大值(70)。由此产生的圆弧看起来像马蹄形,可能是因为“笔”是一个70像素的正方形,而不是一条垂直于行进方向的线 有没有办法在PyQt5中创建一个像图片右侧那样的圆弧 我愿意接受建议:应用程序已经用Python+Tkinter编写,但由于Tkinter+Raspberry上缺少抗锯齿功能,我需要重新编写 (方案B将继续使用PyQt,创建一个饼片(drawPie),并用背景色的圆圈覆盖中心区域-但这并不理想,因为它对我的设计造成了一些限制。)Python PyQt5带宽“的牵引弧;钢笔“;,python,graphics,pyqt5,qpainter,Python,Graphics,Pyqt5,Qpainter,我正在尝试使用PyQt5绘制一个圆量规(MacOS 11.0.1,Python 3.9)。 我使用drawArc语句创建了仪表背景,因此我将笔宽设置为一个大值(70)。由此产生的圆弧看起来像马蹄形,可能是因为“笔”是一个70像素的正方形,而不是一条垂直于行进方向的线 有没有办法在PyQt5中创建一个像图片右侧那样的圆弧 我愿意接受建议:应用程序已经用Python+Tkinter编写,但由于Tkinter+Raspberry上缺少抗锯齿功能,我需要重新编写 (方案B将继续使用PyQt,创建一个饼片
#导入库
从PyQt5.QtWidgets导入*
从PyQt5.QtGui导入*
从PyQt5.QtCore导入*
导入系统
arcreading=0
加法器=.1
#创建仪表类
类仪表(QMainWindow):
#建造师
定义初始化(自):
super()。\uuuu init\uuuuu()
timer=QTimer(self)#创建一个timer对象
timer.timeout.connect(self.update)#向计时器添加操作,更新整个代码
计时器启动(0)#更新周期(毫秒)
self.setGeometry(200200600600)#窗口位置和大小
self.setStyleSheet(“背景:黑色;”)#背景色
# -----------------------
#绘制事件的方法
# -----------------------
def paintEvent(自身,事件):
全局电弧读取
全局加法器
#打印('x')
kanvasx=50#装订箱原点:x
kanvasy=50#装订箱原点:y
kanvasheight=150#装订箱高度
kanvaswidth=150#装订箱宽度
arcsize=270#起点和终点之间的弧角。
弧宽=70#弧宽
painter=QPainter(self)#创建一个painter对象
painter.setRenderInt(QPainter.antiAlias)#调整painter
painter.setPen(QPen(Qt.green,arcwidth))#设置颜色和宽度
#------以下几行模拟传感器读数-----------
如果arcreading>arcsize或arcreading<0:#变量使圆弧移动
加法器=-加法器#arcreading对应于
#由弧指示的值。
arcreading=arcreading+加法器
#---------结束模拟------------------------------
#打印(arcreading)
#drawArc语法:
#牵引弧(x_轴、y_轴、宽度、长度、星形、跨距角)
painter.drawArc(kanvasx,kanvasy,#装订框:x0,y0,像素
kanvasheight+arcwidth,#装订箱:高度
kanvaswidth+arcwidth,#装订盒:宽度
int((弧大小+(180-弧大小)/2)*16),#弧起点,度(?)
int(-arcreading*16))#弧跨度
painter.end()#end painter
#驱动程序代码
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app=QApplication(sys.argv)
#创建仪表对象
win=仪表()
#展示
win.show()
退出(app.exec_389;()
您需要使用适当的设置笔的长度,在您的情况下,您应该使用FlatCap
,它正好在行尾结束,而默认值是SquareCap
(它覆盖了行尾并延伸了一半线宽):
明亮的它起作用了,格拉齐·坦特!!我看了看setPen,但笔迹是…:-/
# importing libraries
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
arcreading = 0
adder = .1
# creating a Gauge class
class Gauge(QMainWindow):
# constructor
def __init__(self):
super().__init__()
timer = QTimer(self) # create a timer object
timer.timeout.connect(self.update) # add action to the timer, update the whole code
timer.start(0) # update cycle in milliseconds
self.setGeometry(200, 200, 600, 600) # window location and size
self.setStyleSheet("background : black;") # background color
# -----------------------
# method for paint event
# -----------------------
def paintEvent(self, event):
global arcreading
global adder
# print('x')
kanvasx = 50 # binding box origin: x
kanvasy = 50 # binding box origin: y
kanvasheight = 150 # binding box height
kanvaswidth = 150 # binding box width
arcsize = 270 # arc angle between start and end.
arcwidth = 70 # arc width
painter = QPainter(self) # create a painter object
painter.setRenderHint(QPainter.Antialiasing) # tune up painter
painter.setPen(QPen(Qt.green, arcwidth)) # set color and width
# ---------- the following lines simulate sensor reading. -----------
if arcreading > arcsize or arcreading < 0: # variable to make arc move
adder = -adder # arcreading corresponds to the
# value to be indicated by the arc.
arcreading = arcreading + adder
# --------------------- end simulation ------------------------------
#print(arcreading)
# drawArc syntax:
# drawArc(x_axis, y_axis, width, length, startAngle, spanAngle)
painter.drawArc(kanvasx, kanvasy, # binding box: x0, y0, pixels
kanvasheight + arcwidth, # binding box: height
kanvaswidth + arcwidth, # binding box: width
int((arcsize + (180 - arcsize) / 2)*16), # arc start point, degrees (?)
int(-arcreading*16)) # arc span
painter.end() # end painter
# Driver code
if __name__ == '__main__':
app = QApplication(sys.argv)
# creating a Gauge object
win = Gauge()
# show
win.show()
exit(app.exec_())
painter.setPen(QPen(Qt.green, arcwidth, cap=Qt.FlatCap))