Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python/Tkinter-从另一个类调用变量并绘制图形时出现问题_Python_Variables_Numpy_Matplotlib_Tkinter - Fatal编程技术网

Python/Tkinter-从另一个类调用变量并绘制图形时出现问题

Python/Tkinter-从另一个类调用变量并绘制图形时出现问题,python,variables,numpy,matplotlib,tkinter,Python,Variables,Numpy,Matplotlib,Tkinter,我正在写一个代码,我需要写一些输入,用它们做一些数学运算,然后打印结果。一切都有GUI 我这样写代码: class Mainwindows: def __init__ (self, master): self.master=master self.mainframe=tk.LabelFrame(self.master, text="INPUT") self.outputframe=tk.LabelFrame(self.master, tex

我正在写一个代码,我需要写一些输入,用它们做一些数学运算,然后打印结果。一切都有GUI

我这样写代码:

class Mainwindows:
    def __init__ (self, master):
        self.master=master
        self.mainframe=tk.LabelFrame(self.master, text="INPUT")
        self.outputframe=tk.LabelFrame(self.master, text="OUTPUT")
        self.sinputbutton=tk.Button(self.mainframe, text="Soil INPUT", command=self.opensoilinput)
        self.sinputbutton.pack()
        self.beamimage=tk.PhotoImage(file="Inputfond.pgm")
        self.binputbutton=tk.Button(self.mainframe, text="Beam INPUT",image=self.beamimage, compound="top", command=self.openbeaminput)
        self.binputbutton.pack()
        self.actinputbutton= tk.Button(self.mainframe, text= "Actions INPUT", command= self.openactioninput)
        self.actinputbutton.pack()
        self.glabel=tk.Label(self.mainframe, text= "Beam leinght [m]")
        self.glabel.pack()
        self.glinpute=tk.Entry(self.mainframe, textvariable=tk.DoubleVar)
        self.glinpute.pack()
        self.g2label=tk.Label(self.mainframe, text= "Strip leinght [m]")
        self.gl2inpute=tk.Entry(self.mainframe, textvariable= tk.DoubleVar)
        self.gl2inpute.pack()
        self.runbutton=tk.Button(self.outputframe, text="RUN", command=self.runkoenig)
        self.runbutton.pack(side="bottom")
        self.plotbutton=tk.Button(self.outputframe, text="Plot", command= self.plotaction)
        self.plotbutton.pack()
        self.canvplot= tk.Canvas (self.outputframe, width=800, height=600, bg="#60B7FF")
        self.canvplot.pack()
        self.mainframe.pack()
        self.outputframe.pack()

        self.soilwindow= tk.Toplevel(self.master)
        self.soilwindow.title("Soil INPUT")
        self.app= Soilinput(self.soilwindow)
        self.soilwindow.withdraw()
        self.beamwindow= tk.Toplevel(self.master)
        self.beamwindow.title("Beam INPUT")
        self.app= Beaminput(self.beamwindow)
        self.beamwindow.withdraw()
        self.actionwindow= tk.Toplevel(self.master)
        self.actionwindow.title("Actions INPUT")
        self.app= Actioninput(self.actionwindow)
        self.actionwindow.withdraw()

    def opensoilinput(self):
        self.soilwindow.deiconify()
    def openbeaminput(self):
        self.beamwindow.deiconify()
    def openactioninput(self):
        self.actionwindow.deiconify()
    def runkoenig(self):
        self.run= Runkoenig()

    def plotaction(self):
        plt.plot(self.Runkoenig.x, self.Runkoenig.pfin)

class Soilinput:      #1st input window with some widgets
    def __init__ (self, master):
        self.master=master
        self.soilframe=tk.Frame(self.master)
        self.soilstiffness= tk.Label(self.soilframe, text="Soil stiffness [kPa]")
        self.soilstiffness.pack()    
        self.soilstiffnessinpute= tk.Entry(self.soilframe, textvariable=tk.DoubleVar)
        self.soilstiffnessinpute.pack()        
        self.deflayer= tk.Label(self.soilframe, text="Deformable layer [m]")
        self.deflayer.pack()        
        self.deflayerinpute= tk.Entry(self.soilframe, textvariable=tk.DoubleVar)
        self.deflayerinpute.pack()                
        self.poissonratio=tk.Label(self.soilframe, text="Poisson ratio v")
        self.poissonratio.pack()       
        self.poissonratioinpute=tk.Entry(self.soilframe, textvariable=tk.DoubleVar)
        self.poissonratioinpute.pack()       
        self.soilbutton=tk.Button(self.soilframe, text="Ok", command=self.soilinputquit)
        self.soilbutton.pack(side="right", padx=5, pady=10)
        self.soilframe.pack()
    def soilinputquit(self):
        self.master.withdraw()

class Beaminput:   #2nd input window with other stuff widget
    def __init__ (self, master):
        self.master=master
        self.beamframe=tk.Frame(self.master)
        self.beamstiffness= tk.Label(self.beamframe, text="Beam stiffness [kPa]")
        self.beamstiffness.pack()
        self.beamstiffnessinpute= tk.Entry(self.beamframe, textvariable=tk.DoubleVar)
        self.beamstiffnessinpute.pack()
        self.beaminertia=tk.Label(self.beamframe, text="Beam inertia [kPa]")
        self.beaminertia.pack()
        self.beaminertiainpute= tk.Entry(self.beamframe,textvariable=tk.DoubleVar)
        self.beaminertiainpute.pack()
        self.beambase=tk.Label(self.beamframe, text="Beam base [m]")
        self.beambase.pack()
        self.beambaseinpute=tk.Entry(self.beamframe,textvariable=tk.DoubleVar)
        self.beambaseinpute.pack()
        self.beamexitbutton=tk.Button(self.beamframe, text="Ok", command=self.beamexit)
        self.beamexitbutton.pack(side="right", padx=5, pady=10)
        self.beamframe.pack()
    def beamexit(self):
        self.master.withdraw()

class Actioninput: #3rd and last one of the input windows
    def __init__ (self, master):
        self.master=master
        self.actionframe=tk.Frame(self.master)
        self.actionforce=tk.Label(self.actionframe, text="Nodal Force [kN]")
        self.actionforce.pack()
        self.actionforceinpute= tk.Entry(self.actionframe, textvariable=tk.DoubleVar)
        self.actionforceinpute.pack()
        self.actionforcedistance=tk.Label(self.actionframe, text="Node distance [m]")
        self.actionforcedistance.pack()
        self.actionforcedistanceinpute= tk.Entry(self.actionframe, textvariable=tk.DoubleVar)
        self.actionforcedistanceinpute.pack()
        self.linearload= tk.Label(self.actionframe, text="Linear Load [kN/m]")
        self.linearload.pack()
        self.linearloadinpute= tk.Entry(self.actionframe, textvariable=tk.DoubleVar)
        self.linearloadinpute.pack()
        self.actionexitbutton= tk.Button(self.actionframe, text="Ok", command= self.actionexit)
        self.actionexitbutton.pack(side="right", padx=5, pady=10)
        self.actionframe.pack()
    def actionexit(self):
        self.master.withdraw()

class Runkoenig:      #in here i put all the mathematical operations
    def __init__ (self, master):

        self.f=self.actionwindow.actionforceinpute.get()
        self.df= self.actionwindow.actionforcedistanceinpute.get()
        self.E= self.beamwindow.beamstiffnessinpute.get()
        self.J= self.beamwindow.beaminertiainpute.get()
        self.B= self.beamwindow.beambaseinpute.get()
        self.Et= self.soilwindow.soilstiffnessinpute.get()
        self.Hdef= self.soilwindow.deflayeclassrinpute.get()
        self.v= self.soilwindow.poissonratioinpute.get()
        self.L= self.master.glinpute.get()
        self.l= self.master.gl2inpute.get()
        self.x=(0.5*self.l*(2*self.NaSTR-self.Nuno));

        #math stuff stuff stuff and then the final passages:

        self.p=np.linalg.solve(self.A,self.N)
        self.pfin=np.resize(self.p,np.size(self.x))

def main():    
     root=tk.Tk()
     root.title("$$$$$")
     app= Mainwindows(root)
     root.mainloop()


if __name__ == '__main__':
main()
我想做的是在输入窗口中存储的条目小部件中插入输入,然后返回根,按下按钮RUN开始在“Runkoenig”类中编写的所有数学运算,然后按下按钮plot在画布中显示“x”和“pfin”的绘图


除了画布上的东西(我什么也没写,因为我不知道怎么做),反正也不行

您的类无法神奇地访问彼此的属性。这是您的代码的一个稍加修改的版本。显然,我不能正确地测试它,但它应该可以帮助您使事情正常进行

我所做的主要工作是在
Mainwindows
中保存每个子类实例;使用
self.app
,您的代码可以做到这一点,但是每个新实例都过度编写了上一个实例。现在,
Soilinput
实例被保存到
Mainwindows
实例的
.soil
属性中。等等

另外,我将
Mainwindows
实例传递给
Runkoenig
,以便
Runkoenig
可以直接访问
Mainwindows
,它还可以从
Mainwindows
中保存的属性访问子实例

Mainwindows
的任何方法中,名称
self
指的是
Mainwindows
实例本身。当按下
RUN
按钮时,调用
Mainwindows
实例的
.runkoenig
方法。该方法创建一个新的
Runkoenig
实例,将
Mainwindows
self
传递给它

换句话说,
Runkoenig(self)
将对
Mainwindows
实例的引用传递给
Runkoenig
方法。
\uuuu init\uuuu
方法接收该引用作为其第二个参数,
mainwin
,因此在该方法中
mainwin
是对我们的
Mainwindows
实例的引用。我希望这不会太令人困惑

我使用了简化版的
self.binputbutton
,因为我没有“Inputfond.pgm”图像文件



下一次,请考虑张贴A;对于那些试图回答你问题的人来说,这让生活变得简单多了。您可以在创建MCVE的过程中自己解决这个问题。

您的类无法神奇地访问彼此的属性。这是您的代码的一个稍加修改的版本。显然,我不能正确地测试它,但它应该可以帮助您使事情正常进行

我所做的主要工作是在
Mainwindows
中保存每个子类实例;使用
self.app
,您的代码可以做到这一点,但是每个新实例都过度编写了上一个实例。现在,
Soilinput
实例被保存到
Mainwindows
实例的
.soil
属性中。等等

另外,我将
Mainwindows
实例传递给
Runkoenig
,以便
Runkoenig
可以直接访问
Mainwindows
,它还可以从
Mainwindows
中保存的属性访问子实例

Mainwindows
的任何方法中,名称
self
指的是
Mainwindows
实例本身。当按下
RUN
按钮时,调用
Mainwindows
实例的
.runkoenig
方法。该方法创建一个新的
Runkoenig
实例,将
Mainwindows
self
传递给它

换句话说,
Runkoenig(self)
将对
Mainwindows
实例的引用传递给
Runkoenig
方法。
\uuuu init\uuuu
方法接收该引用作为其第二个参数,
mainwin
,因此在该方法中
mainwin
是对我们的
Mainwindows
实例的引用。我希望这不会太令人困惑

我使用了简化版的
self.binputbutton
,因为我没有“Inputfond.pgm”图像文件



下一次,请考虑张贴A;对于那些试图回答你问题的人来说,这让生活变得简单多了。在创建MCVE的过程中,您可以自己解决问题。

什么是“不工作”呢?你有错误吗?如果是,有哪些错误?如果没有错误,您看到的行为与您想要的行为有何不同?此外,代码的缩进需要固定。这里的代码太多了。你的问题越简单、越简洁,你就越有可能得到答案。这里的人往往很乐意帮助您,但不太愿意为您进行所有调试。我修复了缩进,但是当我点击Run按钮时,它告诉我:runkoenig self.Run=runkoenig()类型错误:uuu init_uuu()中的第61行缺少1个必需的位置参数:“master”对不起,代码是leinght,但如果我想正确解释问题,我真的不能削减太多,也许一些标签是不必要的,但它不会改变它的leinght。什么是“不工作”的意思?你有错误吗?如果是,有哪些错误?如果没有错误,您看到的行为与您想要的行为有何不同?此外,代码的缩进需要固定。这里的代码太多了。你的问题越简单、越简洁,你就越有可能得到答案。这里的人往往很乐意帮助您,但不太愿意为您进行所有调试。我修复了缩进,但是当我点击Run按钮时,它告诉我:runkoenig self.Run=Runkoeni中的第61行
import Tkinter as tk 

class Mainwindows:
    def __init__ (self, master):
        self.master=master
        self.mainframe=tk.LabelFrame(self.master, text="INPUT")
        self.outputframe=tk.LabelFrame(self.master, text="OUTPUT")
        self.sinputbutton=tk.Button(self.mainframe, text="Soil INPUT", command=self.opensoilinput)
        self.sinputbutton.pack()
        #self.beamimage=tk.PhotoImage(file="Inputfond.pgm")
        #self.binputbutton=tk.Button(self.mainframe, text="Beam INPUT",image=self.beamimage, compound="top", command=self.openbeaminput)

        self.binputbutton=tk.Button(self.mainframe, text="Beam INPUT", compound="top", command=self.openbeaminput)

        self.binputbutton.pack()
        self.actinputbutton= tk.Button(self.mainframe, text= "Actions INPUT", command= self.openactioninput)
        self.actinputbutton.pack()
        self.glabel=tk.Label(self.mainframe, text= "Beam leinght [m]")
        self.glabel.pack()
        self.glinpute=tk.Entry(self.mainframe, textvariable=tk.DoubleVar)
        self.glinpute.pack()
        self.g2label=tk.Label(self.mainframe, text= "Strip leinght [m]")
        self.g2label.pack()        
        self.gl2inpute=tk.Entry(self.mainframe, textvariable= tk.DoubleVar)
        self.gl2inpute.pack()
        self.runbutton=tk.Button(self.outputframe, text="RUN", command=self.runkoenig)
        self.runbutton.pack(side="bottom")
        self.plotbutton=tk.Button(self.outputframe, text="Plot", command= self.plotaction)
        self.plotbutton.pack()
        self.canvplot= tk.Canvas (self.outputframe, width=800, height=600, bg="#60B7FF")
        self.canvplot.pack()
        self.mainframe.pack()
        self.outputframe.pack()

        self.soilwindow= tk.Toplevel(self.master)
        self.soilwindow.title("Soil INPUT")
        self.soil = Soilinput(self.soilwindow)
        self.soilwindow.withdraw()
        self.beamwindow= tk.Toplevel(self.master)
        self.beamwindow.title("Beam INPUT")
        self.beam = Beaminput(self.beamwindow)
        self.beamwindow.withdraw()
        self.actionwindow= tk.Toplevel(self.master)
        self.actionwindow.title("Actions INPUT")
        self.action = Actioninput(self.actionwindow)
        self.actionwindow.withdraw()

    def opensoilinput(self):
        self.soilwindow.deiconify()
    def openbeaminput(self):
        self.beamwindow.deiconify()
    def openactioninput(self):
        self.actionwindow.deiconify()
    def runkoenig(self):
        self.run = Runkoenig(self)

    def plotaction(self):
        plt.plot(self.Runkoenig.x, self.Runkoenig.pfin)

class Soilinput:      #1st input window with some widgets
    def __init__ (self, master):
        self.master=master
        self.soilframe=tk.Frame(self.master)
        self.soilstiffness= tk.Label(self.soilframe, text="Soil stiffness [kPa]")
        self.soilstiffness.pack()    
        self.soilstiffnessinpute= tk.Entry(self.soilframe, textvariable=tk.DoubleVar)
        self.soilstiffnessinpute.pack()        
        self.deflayer= tk.Label(self.soilframe, text="Deformable layer [m]")
        self.deflayer.pack()        
        self.deflayerinpute= tk.Entry(self.soilframe, textvariable=tk.DoubleVar)
        self.deflayerinpute.pack()                
        self.poissonratio=tk.Label(self.soilframe, text="Poisson ratio v")
        self.poissonratio.pack()       
        self.poissonratioinpute=tk.Entry(self.soilframe, textvariable=tk.DoubleVar)
        self.poissonratioinpute.pack()       
        self.soilbutton=tk.Button(self.soilframe, text="Ok", command=self.soilinputquit)
        self.soilbutton.pack(side="right", padx=5, pady=10)
        self.soilframe.pack()
    def soilinputquit(self):
        self.master.withdraw()

class Beaminput:   #2nd input window with other stuff widget
    def __init__ (self, master):
        self.master=master
        self.beamframe=tk.Frame(self.master)
        self.beamstiffness= tk.Label(self.beamframe, text="Beam stiffness [kPa]")
        self.beamstiffness.pack()
        self.beamstiffnessinpute= tk.Entry(self.beamframe, textvariable=tk.DoubleVar)
        self.beamstiffnessinpute.pack()
        self.beaminertia=tk.Label(self.beamframe, text="Beam inertia [kPa]")
        self.beaminertia.pack()
        self.beaminertiainpute= tk.Entry(self.beamframe,textvariable=tk.DoubleVar)
        self.beaminertiainpute.pack()
        self.beambase=tk.Label(self.beamframe, text="Beam base [m]")
        self.beambase.pack()
        self.beambaseinpute=tk.Entry(self.beamframe,textvariable=tk.DoubleVar)
        self.beambaseinpute.pack()
        self.beamexitbutton=tk.Button(self.beamframe, text="Ok", command=self.beamexit)
        self.beamexitbutton.pack(side="right", padx=5, pady=10)
        self.beamframe.pack()
    def beamexit(self):
        self.master.withdraw()

class Actioninput: #3rd and last one of the input windows
    def __init__ (self, master):
        self.master=master
        self.actionframe=tk.Frame(self.master)
        self.actionforce=tk.Label(self.actionframe, text="Nodal Force [kN]")
        self.actionforce.pack()
        self.actionforceinpute= tk.Entry(self.actionframe, textvariable=tk.DoubleVar)
        self.actionforceinpute.pack()
        self.actionforcedistance=tk.Label(self.actionframe, text="Node distance [m]")
        self.actionforcedistance.pack()
        self.actionforcedistanceinpute= tk.Entry(self.actionframe, textvariable=tk.DoubleVar)
        self.actionforcedistanceinpute.pack()
        self.linearload= tk.Label(self.actionframe, text="Linear Load [kN/m]")
        self.linearload.pack()
        self.linearloadinpute= tk.Entry(self.actionframe, textvariable=tk.DoubleVar)
        self.linearloadinpute.pack()
        self.actionexitbutton= tk.Button(self.actionframe, text="Ok", command= self.actionexit)
        self.actionexitbutton.pack(side="right", padx=5, pady=10)
        self.actionframe.pack()
    def actionexit(self):
        self.master.withdraw()

class Runkoenig:      #in here i put all the mathematical operations
    def __init__ (self, mainwin):
        self.f=mainwin.action.actionforceinpute.get()
        self.df= mainwin.action.actionforcedistanceinpute.get()
        self.E= mainwin.beam.beamstiffnessinpute.get()
        self.J= mainwin.beam.beaminertiainpute.get()
        self.B= mainwin.beam.beambaseinpute.get()
        self.Et= mainwin.soil.soilstiffnessinpute.get()
        self.Hdef= mainwin.soil.deflayerinpute.get()
        self.v= mainwin.soil.poissonratioinpute.get()
        self.L= mainwin.glinpute.get()
        self.l= mainwin.gl2inpute.get()
        print("ok!")

        #self.x=(0.5*self.l*(2*self.NaSTR-self.Nuno));

        #math stuff stuff stuff and then the final passages:

        #self.p=np.linalg.solve(self.A,self.N)
        #self.pfin=np.resize(self.p,np.size(self.x))

def main():    
     root=tk.Tk()
     root.title("$$$$$")
     app= Mainwindows(root)
     root.mainloop()


if __name__ == '__main__':
    main()