【发布时间】:2018-06-15 09:09:56
【问题描述】:
我想通过读取文本文件值来旋转和翻译关于原点的一行,它可以工作,但它给了我最后一个和最后一个输出。我无法将其视为动画。 self.update() 可能存在一些问题。文件名是无人机飞行读数的日志文件。
import math
from PyQt5.QtWidgets import QWidget, QApplication, QGraphicsScene
from PyQt5.QtGui import QPainter, QColor, QBrush, QPen
from PyQt5.QtCore import Qt
import sys
import xlrd
import time
class Heads_ups(QWidget):
lineno=0
pitch,roll=[],[]
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300,300,480,360)
self.setWindowTitle('Colours')
self.show()
def read_file(self,lineno):
withopen('text file having rows and columns','r',) as f :
lines = f.readlines()[4:]
for s in lines:
a = s.split()
#print (len(a))
if(len(a)!=14):
print("point reached")
lineno += 1
continue
self.pitch.append(float(a[4]))
self.roll.append(float(a[6]))
lineno += 1
# print(lineno)
return lineno
def counterrotate(self,origin,point1,roll):
ox,oy = origin
px,py = point1
angle=math.radians(roll)
#counterclockwise
qx= ox+ math.cos(angle)*(px-ox)-math.sin(angle)*(py-oy)
qy= oy+ math.sin(angle)*(px-ox)+math.cos(angle)*(py-oy)
return qx,qy
def rotate(self,origin,point,roll):
ox,oy = origin
px,py = point
angle=math.radians(roll)
#clockwise
qx= ox+ math.cos(angle)*(px-ox)+math.sin(angle)*(py-oy)
qy= oy+ math.sin(angle)*(px-ox)+math.cos(angle)*(py-oy)
return qx,qy
def paintEvent(self,e):
# print(filename)
qp= QPainter(self)
qp.begin(self)
self.drawLine(qp)
self.update()
self.move_line(qp)
qp.end()
def drawLine(self,qp):
pen= QPen(Qt.black,2,Qt.SolidLine)
qp.setPen(pen)
qp.drawLine(0,180,480,180)
self.update()
QApplication.processEvents()
time.sleep(0.2)
def move_line(self,qp):
pen=QPen(Qt.green,2,Qt.SolidLine)
qp.setPen(pen)
qp.drawLine(0,180+50,480,180+50)
# self.scene=QGraphicsScene(self)
lineno = self.read_file(self.lineno)
for m in range(0,lineno,1):
x0=-400
x1=880
y0=180
y1=180
xc=240
yc=180
point=(x0,y0)
point1= (x1,y1)
origin= (xc,yc)
x0,y0=self.rotate(origin,point,self.roll[m]*10)
y0=y0-(self.pitch[m]*60)
# canvas.move(point,x0,y0)
x1,y1=self.counterrotate(origin,point1,self.roll[m]*10)
y1=y1-(self.pitch[m]*60)
dlt=qp.drawLine(x0,y0,x1,y1)
self.update()
QApplication.processEvents()
time.sleep(0.5)
# self.scene.removeItem(dlt)
if __name__ == '__main__':
app=QApplication(sys.argv)
hp=Heads_ups()
filename=open('textfile having rows and columns','r')
sys.exit(app.exec_())
提前致谢。
【问题讨论】:
-
请分享文件
-
@eyllanesc 它只是具有行和列的 .txt 文件。我的代码正在为 txt 文件运行,但问题是它一次显示所有行。
-
请查看 tkinter 代码,我希望在 pyqt5 中使用相同的代码。请帮帮我。
-
tkinter 不是 PyQt,如果你分享我可以测试你的代码的文件,我不想浪费时间创建一个新的 .txt,认为如果你节省我的时间,我可以节省你。
标签: python-3.x pyqt5 qpainter