我收到错误:AttributeError: 'DES'对象没有属性'summary_output'我调用这个Set类set_summary方法使用upload_csv文件中的这个函数,也就是upload
upload_csv_class
def upload():
global summary
xvalues = []
yvalues = []
xyvalues = []
header = []
summary.clear()
xvalues.clear()
yvalues.clear()
xyvalues.clear()
header.clear()
filename = filedialog.askopenfilename()
if len(filename) != 0:
print('Selected:', filename)
with open(filename) as file:
csvreader = csv.reader(file)
header.append(next(csvreader))
for row in csvreader:
if len(row) == 3:
xvalues.append(int(row[0]))
yvalues.append(int(row[1]))
xyvalues.append(int(row[2]))
elif len(row) == 2:
xvalues.append(row[0])
yvalues.append(row[1])
if len(header[0]) == 3:
summary.append(header[0])
summary.append(yvalues)
summary.append(yvalues)
summary.append(xyvalues)
if len(header[0]) == 2:
summary.append(header[0])
summary.append(xvalues)
summary.append(yvalues)
summary.append([])
s = Set(summary)
s.set_summary()
这没有问题,并且正确地通过set类将变量数据传递给set文件中的set_summary函数。
这就有点棘手了。
因此,当从我的set类解析摘要值到我的测试文件/DES类时,我得到这个错误。
这是我的Set类
class Set:
def __init__ (self, summary):
self.summary = summary
def set_summary(self):
print(self.summary)
s = DES(self.summary)
s.set_summary_text()
我想把总结值添加到我的DES类对象中,这样我就可以有多个tkinter帧/窗口。
我得到的错误是:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:UserstbyrmAppDataLocalProgramsPythonPython39libtkinter__init__.py", line 1892, in __call__
return self.func(*args)
File "C:Users***DocumentsWorkspace******viewupload_csv.py", line 120, in upload
s.set_summary()
File "C:Users***DocumentsWorkspace******viewSet.py", line 23, in set_summary
s.set_summary_text()
File "C:Users***DocumentsWorkspace******viewtest.py", line 174, in set_summary_text
self.summary_output.configure(state='normal')
AttributeError: 'DES' object has no attribute 'summary_output'
这是我的DES类:
class DES(Frame):
def __init__(self, summary):
self.summary = summary
def createFrame(self, master):
global i
self.frame = tk.Frame(master, width=750, height=968,bg='white')
self.upload_button = tk.Button(
self.frame,
text="Add Data",
fg="DodgerBlue4",
font=("Graph Type", 15),
height=1, width=12,
borderwidth=2,
relief="groove",
command=upload)
self.des_button = tk.Button(
self.frame,
text="New DES",
fg="DodgerBlue4",
font=("Graph Type", 15),
height=1, width=12,
borderwidth=2,
relief="groove",
command=self.new_des)
self.comb_csv_button = tk.Button(
self.frame,
text="Combine CSV",
fg="DodgerBlue4",
font=("Graph Type", 15),
height=1, width=12,
borderwidth=2,
relief="groove",
command=self.new_des)
self.logout_buttotn = tk.Button(
self.frame,
text="Logout",
font=("Arial", 15),
height=1, width=12,
borderwidth=2,
relief="groove",
fg="red",
command = self.close)
self.chat_submit_button = tk.Button(
self.frame,
text="Submit",
font=("Arial", 9),
height=1, width=12,
command=self.set_chat_text,
borderwidth=2,
relief="groove")
self.chat_input = tk.Entry(
self.frame,
width=55,
font=("Arial", 14), highlightthickness=0,
bg="white", borderwidth=1, relief="solid")
self.summary_output = tk.Text(
self.frame,
height=8,
width=78,
bg="gray95",
borderwidth=2,
relief="groove",
font=("Arial", 12))
self.summary_output.configure(state='disabled')
self.chat_output = tk.Text(
self.frame,
height=8,
width=78,
bg="gray95",
borderwidth=2,
relief="groove",
font=("Arial", 12))
self.chat_output.insert(INSERT, "Chat: n")
self.chat_output.configure(state='disabled')
n = tk.StringVar()
self.combo_box_graph = ttk.Combobox(
self.frame,
width=14,
justify='center',
textvariable=n,
font=("Arial", 22),
state="readonly")
self.combo_box_graph['values'] = (
'Select',
'Line Graph',
'Bar Graph',
'Histogram',
'Scatter Graph')
self.combo_box_graph.current(0)
self.combo_box_graph.bind("<<ComboboxSelected>>", self.view_graph)
font = Font(family = "Helvetica", size = 12)
self.frame.option_add("*TCombobox*Listbox*Font", font)
# IMPLEMENTING GRAPH ------------------------------------------------------
fig = Figure(figsize=(5, 4), dpi=130) # Create graph figure
self.plt = fig.add_subplot(111) # Add plots
self.canvas = FigureCanvasTkAgg(fig, master) # tk implementation
self.canvas.draw() # Create the graph canvas
# # IMPLEMENTING TOOLBAR ----------------------------------------------------
toolbarFrame = Frame(master)
toolbarFrame.pack(side=TOP, fill=BOTH) # Place toolbar at top of screen
toolbar = NavigationToolbar2Tk(self.canvas, toolbarFrame)
self.upload_button.place(x=20, y=560)
self.combo_box_graph.place(x=170, y=560)
self.comb_csv_button.place(x=0, y=600)
self.summary_output.place(x=20, y=610)
self.chat_output.place(x=20, y=770)
self.chat_input.place(x=20, y=920)
self.chat_submit_button.place(x=633, y=920)
self.logout_buttotn.place(x=585, y=560)
self.canvas.get_tk_widget().place(x=50, y=30)
if i == 0:
self.des_button.place(x=395, y=560)
i += 1
self.frame.pack()
def new_des(self):
self.newWindow = tk.Toplevel(root)
s = DES("")
s.createFrame(self.newWindow)
def close(self):
root.destroy()
def set_chat_text(self):
self.chat_output.configure(state='normal')
self.chat_output.insert('end', self.chat_input.get() + 'n')
self.chat_output.configure(state='disabled')
self.chat_input.delete(0, END)
def set_summary_text(self):
self.summary_output.configure(state='normal')
self.summary_output.delete('1.0', END) # Remote all text
if len(summary[0]) == 3:
text = summary[0][0]+ ": " + str(summary[1]).replace('[','').replace(']','') + "nn" + summary[0][1] + ": " + str(summary[2]).replace('[','').replace(']','') + "nn" + summary[0][2] + ": " + str(summary[3]).replace('[','').replace(']','')
if len(summary[0]) == 2:
text = summary[0][0]+ ": " + str(summary[1]).replace('[','').replace(']','') + "nn" + summary[0][1] + ": " + str(summary[2]).replace('[','').replace(']','')
self.summary_output.insert('end',text)
self.summary_output.configure(state='disabled') #Make text widget read only
def view_graph(self, event):
print("test")
self.plt.cla()
if len(self.summary) != 0:
self.header = self.summary[0]
x = self.summary[1]
y = self.summary[2]
self.x_y_range = self.summary[3]
self.xlabel = self.header[0]
self.ylabel = self.header[1]
self.plt.set_xlabel(self.xlabel)
self.plt.set_ylabel(self.ylabel)
if len(self.header[0]) >= 3:
self.xylabel = self.header[2]
if self.combo_box_graph.get() == "Line Graph":
self.plt.plot(x, y)
self.plt.set_title("Line Graph")
self.canvas.draw()
if self.combo_box_graph.get() == "Bar Graph":
self.ind = numpy.arange(len(x))
self.width = .8
self.plt.ax.bar(self.ind, y, self.width)
self.plt.ax.set_title("Bar Graph")
self.canvas.draw()
if self.combo_box_graph.get() == "Histogram":
self.plt.ax.hist(y, density=True, bins=82, label=self.ylabel)
self.mn, self.mx = self.plt.ax.set_xlim()
self.plt.ax.set_xlim(self.mn, self.mx)
self.kde_xs = np.linspace(self.mn, self.mx, 300)
self.kde = st.gaussian_kde(y)
self.plt.ax.plot(self.kde_xs, self.kde.pdf(self.kde_xs), label=self.xlabel)
self.plt.ax.legend(loc="upper left")
self.plt.ax.set_title("Histogram")
self.canvas.draw()
if self.combo_box_graph.get() == "Scatter Graph":
self.plt.ax.scatter(self.x_y_range, x, color='r')
self.plt.ax.scatter(self.x_y_range, y, color='b')
self.plt.ax.set_title("Scatter Graph")
self.canvas.draw()
if self.combo_box_graph.get() == "Select":
self.canvas.draw()
def main():
global root
root = tk.Tk()
s = DES("")
s.createFrame(root)
root.mainloop()
if __name__ == '__main__':
main()
所以我想从我的Set类解析摘要值到我的DES类,将摘要值添加到类self对象中,以便我可以在不同的窗口上查看不同的matplotlib图。现在我从我的upload_csv类中调用upload方法,它用于选择文件,读取csv文件,将值发送到Set类,然后将值发送到DES类,并作为set_summary_text函数的参数,这是我想设置摘要的地方,然后在我更改组合框bo时使用这些值。当组合框更改为图形类型时,matplotlib将使用这些值来绘制图形。值需要是自我值,这样我就可以有多个图形和窗口。请帮忙,有问题就问/
由于DES不能是单例,我认为您有3个选项:
选项1:使用已经创建的DES对象,并且在设置summary
时已经有summary_output
。class Set:
def __init__ (self, summary):
self.summary = summary
def set_summary(self,des):
print(self.summary)
des.set_summary_text()
选项2:在创建Set对象时使用已经创建的DES对象。在使用set_summary
之前,必须为这个DES对象创建一个帧class Set:
def __init__ (self, des, summary):
self des = des
self.summary = summary
def set_summary(self):
print(self.summary)
self.des.set_summary_text()
选项3:与SET对象一起创建一个DES对象
class Set:
def __init__ (self, summary, master):
self.summary = summary
self.des = DES(self.summary)
self.des.createFrame(master)
def set_summary(self):
print(self.summary)
self.des.set_summary_text()