如何参考;“尚未创建”;Python中的类实例
python和一般面向对象编程的新手。我写了一个脚本来做一些事情,这里是它的一个非常简化的版本及其主要结构如何参考;“尚未创建”;Python中的类实例,python,matplotlib,tkinter,Python,Matplotlib,Tkinter,python和一般面向对象编程的新手。我写了一个脚本来做一些事情,这里是它的一个非常简化的版本及其主要结构 from tkinter import * import matplotlib.pyplot as plt import numpy as np class MainWindow: def __init__(self): self.mw = Tk() button = Button(self.mw, text='Open', command=sel
from tkinter import *
import matplotlib.pyplot as plt
import numpy as np
class MainWindow:
def __init__(self):
self.mw = Tk()
button = Button(self.mw, text='Open', command=self.openfile)
button.pack()
self.anotherwd = []
self.mw.mainloop()
def openfile(self):
x = float(input('Number:'))
self.anotherwd.append(AnotherWindow(x))
class AnotherWindow:
def __init__(self,number):
self.aw = Toplevel()
self.var = IntVar()
self.numb = number
label = Label(self.aw, text="Multiply by:")
entry = Entry(self.aw, textvariable=self.var)
button = Button(self.aw, text='Plot', command=self.plot)
label.pack()
entry.pack()
button.pack()
def plot(self):
numb_change = self.numb * self.var.get()
plotwd.plotvar = numb_change
plotwd.plot_line()
YetAnotherWindow()
class PlotWindow:
def __init__(self):
self.plotvar = 0
self.fig, self.ax = plt.subplots()
def plot_line(self):
example = np.arange(0., 5., 0.2)
self.ax.plot(example, self.plotvar*example)
self.fig.show()
class YetAnotherWindow:
def __init__(self):
self.yaw = Toplevel()
self.var_yaw = IntVar()
label = Label(self.yaw, text="More numbers:")
entry = Entry(self.yaw, textvariable=self.var_yaw)
button = Button(self.yaw, text='Plot more', command=self.plot_another_line)
label.pack()
entry.pack()
button.pack()
def plot_another_line(self):
example = np.arange(0., 5., 0.2)
plotwd.ax.plot(example, self.var_yaw.get() * plotwd.plotvar * example)
plotwd.fig.show()
plotwd = PlotWindow()
gui = MainWindow()
在“主窗口”中,您可以导入一些数据(此处为作为控制台输入的数字),然后在“另一个窗口”中修改数据(此处将其乘以输入的数字),并在“绘图窗口”中打印修改后的数据。之后,您可以在“YetAnotherWindow”(同样是一些无用的乘法)中使用它,并在同一个“PlotWindow”中绘制结果
问题是,只有一个“PlotWindow”-对象,而我每次按“AnotherWindow”中的按钮时都要创建一个实例,其中包含单独修改的数据和另一个“YetAnotherWindow”-实例。
现在,我可以像在“MainWindow”中那样将新实例附加到列表中,但我不知道如何在单独的类(如“YetAnotherWindow”)中引用这样的实例,因为初始化时还不会创建“PlotWindow”实例中的一个属性。我想这就是为什么我宣布它“固定”为“plotwd”,首先作为一个“解决方案”
我认为“继承”可能是一个相关的关键词,但我不能让它以这种方式工作。无论如何,我可能会做一些太复杂的事情,所以在实际代码中进行大规模重写之前,我最好先问一下
谢谢
编辑1:还有一个(相关的?)问题是,如果我关闭绘图窗口,“另一个窗口”也会关闭,我真的不想这样做,这是我想改变的主要原因
编辑2:不工作的例子我想象它会如何工作
from tkinter import *
import matplotlib.pyplot as plt
import numpy as np
class MainWindow:
def __init__(self):
self.mw = Tk()
button = Button(self.mw, text='Open', command=self.openfile)
button.pack()
self.anotherwd = []
self.mw.mainloop()
def openfile(self):
x = float(input('Number:'))
self.anotherwd.append(AnotherWindow(x))
class AnotherWindow:
def __init__(self, number):
self.aw = Toplevel()
self.var = IntVar()
self.numb = number
self.plotwindows = []
self.yawindows = []
label = Label(self.aw, text="Multiply by:")
entry = Entry(self.aw, textvariable=self.var)
button = Button(self.aw, text='Plot', command=self.plot)
label.pack()
entry.pack()
button.pack()
def plot(self):
numb_change = self.numb * self.var.get()
self.plotwindows.append(PlotWindow)
self.plotwindows[-1].plotvar = numb_change
self.plotwindows[-1].plot_line
self.yawindows.append(YetAnotherWindow)
class PlotWindow:
def __init__(self):
self.plotvar = 0
self.fig, self.ax = plt.subplots()
def plot_line(self):
example = np.arange(0., 5., 0.2)
self.ax.plot(example, self.plotvar*example)
self.fig.show()
class YetAnotherWindow():
def __init__(self):
self.yaw = Toplevel()
self.var_yaw = IntVar()
label = Label(self.yaw, text="More numbers:")
entry = Entry(self.yaw, textvariable=self.var_yaw)
button = Button(self.yaw, text='Plot more', command=self.plot_another_line)
label.pack()
entry.pack()
button.pack()
def plot_another_line(self):
example = np.arange(0., 5., 0.2)
gui.anotherwd[-1].plotwindows[-1].ax.plot(example, self.var_yaw.get() * gui.anotherwd[-1].plotwindows[-1].plotvar * example)
gui.anotherwd[-1].plotwindows[-1].fig.show()
gui = MainWindow()
而且,即使这样做可行,我也会在“YetAnotherGui”中访问“最后一个绘图窗口”,而不是它“属于”的窗口。嗯,我在这里不是这样做的吗?我想要的不是plotwd,而是plotwd[0]、plotwd[1]等等,它们有自己的独立类属性,具体取决于您在“另一个窗口”中向其提供的数据以及您可以通过“YetAnotherWindow”单独修改的数据。可能误解了你的意思。你的问题不清楚。为什么
plotwd[0]
不适合您?因为我不知道如何在单独的类(如“YetAnotherWindow”)中获取该类实例的self.fig、self.ax。我试图重写它(见编辑2),正如我想象的那样。但是它a)看起来很可怕,也不是很像皮提亚式,或者哲学是如何被称为的,b)不起作用。如果self.anotherwd[0]
是AnotherWindow
的一个实例,那么self.anotherwd[0]
是顶层,self.anotherwd[0]
是PlotWindow
的一个实例,依此类推。这并不复杂,tkinter对象也没有什么独特之处。这就是python的工作原理。我从来没有说过关于tkinter对象的任何事情,如果标签误导了我,我很抱歉,但在本例中,我不关心tkinter。重要的是在其他实例中调用的类实例的引用(如果这在本例中是正确的术语)。我不敢相信整个“self.anotherwd[0].plotwindows[0]”会是最好的方法。