我是Python编程的新手,我最近刚开始。 我也在发现OOP,我主要编写基于ASM和C语言的嵌入式软件。 我有一个小项目,我需要读取文件,获取数据并“清理”它们。这些数据绘制在笛卡尔坐标和极坐标中的图上。到目前为止,我已经实现了在这里和那里获取代码的示例,并使用pyQt5创建接口并在它们上集成两个matplotlib画布。
我创建了一个名为'clean'的按钮,当按下它时会创建一个正常工作的事件,它会清理数据,但到目前为止我还没有用刷新的数据刷新画布。
我认为我迷失了pyQt集成的OOP中的类,函数和变量。
有人可以帮我看一下程序的主要结构并指出我的问题吗? 我也知道我的python代码不是那么'干净'并且我没有使用它的优点而且我详细说明了很多......这来自C背景。
见下面的代码,我已从文件中删除了数据处理。我需要绘制以下列表名为 LDTvalue [] [] ,我在一个图上有两个图,它是 LDTvalue [0]和LDTvalue [6] :
我发现在点击按钮时我无法更改任何小部件。例如,窗口标题会更改但不会更改窗口小部件标签。所以这就是我强烈怀疑搞乱类和对象的功能。
import sys
import matplotlib
matplotlib.use("Qt5Agg")
from PyQt5 import QtCore
from PyQt5.QtWidgets import *
from numpy import arange, sin, cos, pi, radians
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
LDTheader = {'Company': 'xxx','Type_ind': 0,'Symmetry': 0, 'Mc': 0, 'Dc': 0 , 'Ng': 0, 'Dg': 0}
LDTline = list()
LDTCplane = list()
LDTCvalue = list()
LDTvalue = list()
LDTvalue2 = list()
LDTtemplist = list()
LDTfile = open('01.ldt')
for line in LDTfile :
LDTline.append(line.strip())
LDTheader['Company'] = LDTline[0];
LDTheader['Type_ind'] = LDTline[1];
LDTheader['Symmetry'] = LDTline[2];
LDTheader['Mc'] = int(LDTline[3]);
LDTheader['Dc'] = float(LDTline[4]);
LDTheader['Ng'] = int(LDTline[5]);
LDTheader['Dg'] = float(LDTline[6]);
print("Company name:", LDTheader['Company'])
print("Type indicator:", LDTheader['Type_ind'])
print("Symmetry:", LDTheader['Symmetry'])
print("Mc:", LDTheader['Mc'])
print("Dc", LDTheader['Dc'])
print("Ng:", LDTheader['Ng'])
print("Dg:", LDTheader['Dg'])
if LDTheader['Type_ind'] == '1' :
print('1 - point source with symmetry about the vertical axis')
elif LDTheader['Type_ind'] == '2' :
print('2 - linear luminaire')
elif LDTheader['Type_ind'] == '3' :
print('3 - point source with any other symmetry')
else: print('Error in source type indicator!')
if LDTheader['Symmetry'] == '0' :
print('0 - no symmetry')
elif LDTheader['Symmetry'] == '1' :
print('1 - symmetry about the vertical axis')
elif LDTheader['Symmetry'] == '2' :
print('2- symmetry to plane C0-C180')
elif LDTheader['Symmetry'] == '3' :
print('3- symmetry to plane C90-C270')
elif LDTheader['Symmetry'] == '4' :
print('4- symmetry to plane C0-C180 and to plane C90-C270')
else: print('Error in symmetry paramater!')
for i in range(42,42+LDTheader['Mc']) :
LDTCplane.append(LDTline[i])
for i in range(42+LDTheader['Mc'],42+LDTheader['Mc']+LDTheader['Ng']) :
LDTCvalue.append(float(LDTline[i]))
#Separate all the values of the different Cplane in a list of a list called LDTvalue
#print(int(LDTheader['Mc']/int(LDTheader['Symmetry']))+1)
for j in range(0,int(LDTheader['Mc']/int(LDTheader['Symmetry']))+1):
if LDTheader['Symmetry'] == '4':
for i in range(1,LDTheader['Ng']):
LDTtemplist.append(float(LDTline[42+LDTheader['Ng']*(j+1)+LDTheader['Mc']+(LDTheader['Ng']-i)]))
for i in range(0,LDTheader['Ng']-1):
LDTtemplist.append(float(LDTline[42+LDTheader['Ng']*(j+1)+LDTheader['Mc']+(i)]))
LDTvalue.append(list(LDTtemplist))
del LDTtemplist
LDTtemplist = list()
if LDTheader['Symmetry'] == '4':
for i in range(1,LDTheader['Ng']):
LDTtemplist.append(float(LDTline[42+LDTheader['Ng']+LDTheader['Mc']+(LDTheader['Ng']-i)]))
for i in range(0,LDTheader['Ng']-1):
LDTtemplist.append(float(LDTline[42+LDTheader['Ng']+LDTheader['Mc']+(i)]))
LDTvalue2 = LDTtemplist
del LDTtemplist
LDTtemplist = list()
class MyPDiagram(FigureCanvas):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111,projection='polar')
# We want the axes cleared every time plot() is called
#self.axes.hold(False)
t = arange(-180,180,LDTheader['Dg'])
t = radians(t)
self.axes.plot(t, LDTvalue[0], color='r', linewidth=1)
self.axes.plot(t, LDTvalue[6], color='b', linewidth=1)
self.axes.set_theta_zero_location("S")
self.axes.grid(True)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,
QSizePolicy.Expanding,
QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
class MyCDiagram(FigureCanvas):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
# We want the axes cleared every time plot() is called
#self.axes.hold(False)
x = arange(-180,180,LDTheader['Dg'])
self.axes.plot(x, LDTvalue[0], color='r', linewidth=1)
self.axes.plot(x, LDTvalue[6], color='b', linewidth=1)
self.axes.grid(True)
#
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,
QSizePolicy.Expanding,
QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
buttonOK = QPushButton('OK')
buttonClean = QPushButton('Clean')
pd_label = QLabel('Polar Diagram')
pd = MyPDiagram(QWidget(self), width=5, height=4, dpi=100)
cd_label = QLabel('Carthesian Diagram')
cd = MyCDiagram(QWidget(self), width=5, height=4, dpi=100)
buttonClean.clicked.connect(self.handleButtonClean)
grid = QGridLayout()
grid.setSpacing(10)
grid.addWidget(buttonOK, 1, 0)
grid.addWidget(buttonClean, 1, 1)
grid.addWidget(pd_label, 2, 0)
grid.addWidget(pd, 3, 0, 5, 1)
grid.addWidget(cd_label, 2, 1)
grid.addWidget(cd, 3, 1, 5, 1)
grid.update()
self.setLayout(grid)
self.setGeometry(300, 300, 1000, 600)
self.setWindowTitle('W&D LDTcleaner')
def handleButtonClean(self):
#1. Clean the value above 90 degrees
for j in range(0,int(LDTheader['Mc']/int(LDTheader['Symmetry']))+1):
for i in range(180,LDTheader['Ng']-1):
LDTvalue[j][i] = 0
#2. Clean when center value is not the max
for j in range(0,int(LDTheader['Mc']/int(LDTheader['Symmetry']))+1):
for i in range(0,LDTheader['Ng']-1):
if LDTvalue[j][0] < max(LDTvalue[j]):
LDTvalue[j][0] = max(LDTvalue[j])
self.initUI() #-----> This is where I am blocked. I tried to recall initUI()
print('Cleaned!')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())