【问题标题】:Python/Tkinter- Problems with calling variables from another class and plot graphPython/Tkinter- 从另一个类和绘图图调用变量的问题
【发布时间】:2016-03-04 23:25:19
【问题描述】:

我正在编写一个代码,我需要在其中编写一些输入,对它们进行一些数学运算,然后打印结果。一切都带有 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”类中编写的所有数学运算,然后按下“绘图”按钮在画布中显示“x”和“pfin”的绘图。

除了画布的东西(我什么都没写,因为不知道该怎么做)它无论如何都不起作用!

【问题讨论】:

  • “不起作用”是什么意思?你有错误吗?如果是这样,有什么错误?如果没有错误,您看到的行为与您想要的有何不同?此外,您的代码的缩进需要修复。
  • 这里的代码太多了。您可以使您的问题越简单和简洁,您就越有可能得到答案。这里的人往往很乐意为您提供帮助,但不太愿意为您进行所有调试。
  • 我修复了缩进,但是当我点击运行按钮时它告诉我:第 61 行,在 runkoenig self.run = Runkoenig() TypeError: __init__() missing 1 required positional argument: 'master'
  • 对不起,代码 leinght,但如果我想正确解释问题,我真的不能削减太多,也许有些标签不是必需的,但它不会改变它的 leinght 这么多。跨度>

标签: python variables numpy matplotlib tkinter


【解决方案1】:

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

我做的主要事情是将每个子类实例保存在Mainwindows中;你的代码就是这样做的,self.app,但每个新实例都覆盖了前一个实例。所以现在Soilinput 实例被保存到Mainwindows 实例的.soil 属性中。等等。

另外,我将Mainwindows实例传递给Runkoenig,这样Runkoenig就可以直接访问Mainwindows,也可以通过Mainwindows中保存的属性访问子实例。

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

换句话说,Runkoenig(self) 将对Mainwindows 实例的引用传递给Runkoenig .__init__ 方法。 __init__ 方法接收该引用作为其第二个参数mainwin,因此在该方法内部mainwin 是对我们Mainwindows 实例的引用。我希望这不会太混乱。 :)

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()

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


下次请考虑发MCVE;对于试图回答您问题的人来说,它让生活如此变得更加简单。在创建 MCVE 的过程中,您可以自己解决问题。

【讨论】:

  • 当您将Mainwindow 实例传递给Runkoenig 时,您会编写类似def __init__ (self, mainwin) 的内容,然后您使用此mainwin.etc 告诉他可以查看Mainwindow 类,但我无法理解整个过程,我的意思是你不需要定义像mainwin= Mainwindow这样的东西来告诉他mainwin意味着那个类吗?我知道您的代码有效,但想了解未来!谢谢!
  • 所以当我将self.class2= Class2 写入另一个类时,例如Class1,然后我在__init__ 中创建具有两个参数的类2,这可能是:(self, whateveriwant);我在告诉第 2 班:self 是指你自己!第二个参数的意思是:我把你自己的一个实例放到另一个类中,看看那个类,因为我在第 2 类的某些东西之前写了whateveriwant。所以当我将 Class2 的实例写入第 1 类时,我意识到了绑定,然后我为 init 中的第二个参数命名。我说的对吗?
  • @nearchos:非常接近。 :) 我建议您制作几个 simple 非 GUI 类,并尝试对它们进行一些实验,使用 print 来查看传递的内容。
  • 我做到了。最后一个问题,如果我想做那件事但有多个类,假设我得到Class1 ---> self.class3= Class3 然后Class2 ---> self.class3= Class3 我只是在 Class 中写一个第三个参数3 的初始化类似__init__ (self, lookfor1, lookfor2) 还是更复杂?
  • @nearchos:我不太确定你想在那里做什么。我认为您希望Class1 和Class2 都可以访问Class3 的同一个实例,并且您希望Class3 实例可以访问Class1 和Class2 实例。如果是这样,那么Class1 或Class2 之一应该创建Class3 实例,将自身的引用和另一个类实例的引用传递给Class3。试着自己创建一个简单的例子,如果你不能让它像你想要的那样工作,请提出一个新问题。
猜你喜欢
  • 2021-02-08
  • 1970-01-01
  • 2013-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
相关资源
最近更新 更多