gpt4 book ai didi

python - pyqt4从其他py文件绘制圆弧

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:10 25 4
gpt4 key购买 nike

尝试绘制弧线,问题是我想从另一个 py 文件中计算绘制函数,但到目前为止没有运气(如果主 py 文件中的绘制函数就可以)。我导入了另一个 py 文件,但没有任何反应。这是代码:main.py

from PyQt4 import QtGui, Qt, QtCore
import sys
from src.cPrg import cPrg
from PyQt4.Qt import QPen

class mainWindow(QtGui.QWidget):

def __init__(self):

super(mainWindow, self).__init__()

self.otherFile = cPrg()

self.initUI()



def initUI(self):

#self.exitBtn = QtGui.QPushButton('Exit', self)
#self.exitBtn.setGeometry(100,100,60,40)
#self.exitBtn.clicked.connect(self.close_app)

self.label = QtGui.QLabel(self)
self.label.setText(self.otherFile.textas)
self.label.setGeometry(100,140, 60, 40)

self.otherFile.setGeometry(20,20, 20,20)
self.otherFile.startA = 270
self.otherFile.endA = -270


#self.showFullScreen()
self.setGeometry(100, 100, 800, 480)
self.setWindowTitle('Window Title')

self.show()

def close_app(self):
sys.exit()



def main():
app = QtGui.QApplication(sys.argv)
gui = mainWindow()
sys.exit(app.exec_())

if __name__ == '__main__':
main()

和 anotherfile.py

from PyQt4 import QtGui, QtCore, Qt
from PyQt4.Qt import QPen

class cPrg(QtGui.QWidget):

def __init__(self):

super(cPrg, self).__init__()

self.startA = 0
self.endA = 0
self.textas = 'bandom'

def paintEvent(self, e):
painter = QtGui.QPainter(self)
painter.setRenderHint(painter.Antialiasing)
rect = e.rect

r = QtCore.QRect(200,200,20,20) #<-- create rectangle
size = r.size() #<-- get rectangle size
r.setSize(size*10) #<-- set size
startAngle = self.startA*16 #<-- set start angle to draw arc
endAngle = self.endA*16 #<-- set end arc angle

painter.setPen(QPen(QtGui.QColor('#000000'))) #<-- arc color
#painter.setBrush(QtCore.Qt.HorPattern)
painter.drawArc(r, startAngle, endAngle) #<-- draw arc


painter.end()
super(cPrg,self).paintEvent(e)

我做错了什么以及如何更改线宽?谢谢

编辑:我在主 py 文件中所做的所有绘画,这里是代码:

from PyQt4 import QtGui, Qt, QtCore
import sys
from src.cprg import cPrg
from src.cprogress import GaugeWidget
from PyQt4.Qt import QPen

class mainWindow(QtGui.QWidget):

def __init__(self):

self.otherFile = cPrg()
self.gauge = GaugeWidget()

self.i = 0
self.lineWidth = 3
self._value = 0
self.completed = 0

super(mainWindow, self).__init__()

self.initUI()

def initUI(self):

self.setValue(.5)

#self.showFullScreen()
self.setGeometry(100, 100, 800, 480)
self.setWindowTitle('Window Title')

self.show()

def close_app(self):
sys.exit()

def setValue(self, val):
val = float(min(max(val, 0), 1))
self._value = -270 * val
self.update()

def setLineWidth(self, lineWidth):
self.lineWidth = lineWidth

def paintEvent(self, e):

painter = QtGui.QPainter(self)
painter.setRenderHint(painter.Antialiasing)
rect = e.rect

outerRadius = min(self.width(),self.height())
#arc line
r = QtCore.QRect(20,20,outerRadius-10,outerRadius-10) #<-- create rectangle
size = r.size() #<-- get rectangle size
r.setSize(size*.4) #<-- set size
startAngle = 270*16 #<-- set start angle to draw arc
endAngle = -270*16 #<-- set end arc angle

painter.setPen(QPen(QtGui.QColor('#000000'), self.lineWidth)) #<-- arc color
#painter.setBrush(QtCore.Qt.HorPattern)
painter.drawArc(r, startAngle, endAngle) #<-- draw arc

#arc prg
painter.save()
painter.setPen(QPen(QtGui.QColor('#ffffff'), 20))
painter.drawArc(r, startAngle, self._value*16)
painter.restore()

painter.end()
super(mainWindow,self).paintEvent(e)

def main():
app = QtGui.QApplication(sys.argv)
gui = mainWindow()
sys.exit(app.exec_())

if __name__ == '__main__':
main()

这是我简单的圆形进度条,现在的问题是如何将 setValue、setlineWidth 和 PaintEvent 函数放入单独的 py 文件中,然后通过使用这些函数导入此文件和类来调用?我试过这个:

from PyQt4 import QtGui, Qt, QtCore
import sys
from src.cprg import cPrg #<import progressbar
from src.cprogress import GaugeWidget
from PyQt4.Qt import QPen

class mainWindow(QtGui.QWidget):

def __init__(self):

self.otherFile = cPrg() #< imported progress bar
self.gauge = GaugeWidget()

self.i = 0
self.lineWidth = 3
self._value = 0
self.completed = 0

super(mainWindow, self).__init__()

self.initUI()

def initUI(self):

self.otherFile.setGeometry(10,10,100,100) #<<<< progress bar size
self.otherFile.setValue(0.5) #< progress bar value

这不起作用。

最佳答案

使用QPen(color, line_width)更改线宽,update()使用paintEvent重绘。

试试这个:

from PyQt4 import QtGui, QtCore


class cPrg:
def __init__(self):
self.linewidth = 0

def setLineWidth(self, linewidth):
self.linewidth = linewidth

def drawArc(self, painter):
painter.setRenderHint(painter.Antialiasing)
r = QtCore.QRect(200,200,20,20) #<-- create rectangle
size = r.size() #<-- get rectangle size
r.setSize(size*10) #<-- set size
startAngle = self.startA*16 #<-- set start angle to draw arc
endAngle = self.endA*16 #<-- set end arc angle
painter.setPen(QtGui.QPen(QtGui.QColor('#000000'), self.linewidth)) #<-- arc color
painter.drawArc(r, startAngle, endAngle) #<-- draw arc


class mainWindow(QtGui.QWidget):
def __init__(self):
super(mainWindow, self).__init__()
self.otherFile = cPrg()
self.initUI()
self.i = 0

def initUI(self):
self.label = QtGui.QLabel(self)
self.label.setText(self.otherFile.textas)
self.label.setGeometry(100,140, 60, 40)
self.otherFile.startA = 270
self.otherFile.endA = -270
self.setGeometry(100, 100, 800, 480)
self.setWindowTitle('Window Title')
timer = QtCore.QTimer(self)
timer.timeout.connect(self.changeLineWidth)
timer.start(1000)

def changeLineWidth(self):
self.otherFile.setLineWidth(self.i)
self.i += 1
self.i %= 60
self.update()

def paintEvent(self, e):
painter = QtGui.QPainter(self)
self.otherFile.drawArc(painter)


if __name__ == '__main__':
import sys

app = QtGui.QApplication(sys.argv)
w = mainWindow()
w.show()
sys.exit(app.exec_())

如果你想要一个圆形进度条,你必须覆盖QProgressbar:

from math import ceil

from PyQt4 import QtGui, Qt, QtCore
import sys


class cPrg(QtGui.QProgressBar):

def __init__(self, parent=None):
super(cPrg, self).__init__(parent)
self.linewidth = 1

def factor(self, value):
a = 360 / (self.maximum() - self.minimum())
b = -a / (self.maximum() - self.minimum())
return a*value + b

def setLineWidth(self, linewidth):
self.linewidth = linewidth
self.update()

def paintEvent(self, event):

painter = QtGui.QPainter(self)
painter.setRenderHint(painter.Antialiasing)
r = self.rect()
val = ceil(self.factor(self.value()))
nr = QtCore.QRect(r.topLeft() + QtCore.QPoint(self.linewidth, self.linewidth),
QtCore.QSize(r.width()-2*self.linewidth, r.height()-2*self.linewidth))
painter.setPen(QtGui.QPen(QtGui.QColor('#000000'), self.linewidth))
painter.drawArc(nr, 0*16, val*16)


class mainWindow(QtGui.QWidget):
def __init__(self):
super(mainWindow, self).__init__()
self.otherFile = cPrg(self)
self.otherFile.setMinimum(0)
self.otherFile.setMaximum(360)
self.otherFile.setValue(90)
self.initUI()

timerLW = QtCore.QTimer(self)
timerLW.timeout.connect(self.changeLW)
timerLW.start(100)

timerVal = QtCore.QTimer(self)
timerVal.timeout.connect(self.updateValue)
timerVal.start(100)

def initUI(self):
self.label = QtGui.QLabel(self)
self.label.setText("test")
self.label.setGeometry(200, 200, 60, 40)

self.otherFile.setGeometry(0, 0, 200, 200)
self.setGeometry(0, 0, 800, 480)
self.setWindowTitle('Window Title')

def changeLW(self):
lw = (self.otherFile.linewidth + 1) % 20
self.otherFile.setLineWidth(lw)

def updateValue(self):
self.otherFile.setValue(self.otherFile.value() + 1)


def main():
app = QtGui.QApplication(sys.argv)
gui = mainWindow()
gui.show()
sys.exit(app.exec_())

if __name__ == '__main__':
main()

enter image description here enter image description here

关于python - pyqt4从其他py文件绘制圆弧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41191439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com