简体   繁体   English

tkinter-从主/父类获取值的顶级类?

[英]tkinter - A toplevel class getting a value from the main/parent class?

Below is a program that solves quadratic equations and graphs them. 下面是一个解决二次方程式并用图形表示的程序。 I was wondering what i needed to put where the comment is located in the code in order to get a value a parent class (quadratics) and use it in a Toplevel class. 我想知道我需要在代码中放置注释的位置,以便获得父类(二次)的值并在顶级类中使用它。 Any help would be appreciated. 任何帮助,将不胜感激。

import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
from tkinter import *
class home():
        def __init__(self,master):
            self.master = master
            self.master.geometry('400x450')
            self.master.title('Mathsolver')
            Button(self.master, text='Quadratics', command = self.open_quadratics).pack()
        def open_quadratics(self):
            quadratics(Toplevel(self.master))
class quadratics():
        def __init__(self, master):
                self.master = master
                self.vara = DoubleVar()
                self.varb = DoubleVar()
                self.varc = DoubleVar()
                self.master.geometry("500x300")
                self.master.title("Quadratic Equations")
                Label(self.master, text="Enter coefficients of x below in the form ax² + bx + c = 0").grid(row=1,column=1)
                Label(self.master, text="a:").grid(row=2,column=1)
                Entry(self.master, textvariable = self.vara).grid(row=2,column=2)
                Label(self.master, text="b:").grid(row=3,column=1)
                Entry(self.master, textvariable = self.varb).grid(row=3,column=2)
                Label(self.master, text="c:").grid(row=4,column=1)
                Entry(self.master, textvariable = self.varc).grid(row=4,column=2)
                Button(self.master, text="Solve", command = self.calculate_quadratic).grid(row=5,column=2)
                Button(self.master, text='Graph', command = self.grapher, bg='green').grid(row=5,column=2)
        def grapher(self):
                quadratic_grapher(Toplevel(self.master))
        def calculate_quadratic(self):
                global x
                a = self.vara.get()
                b = self.varb.get()
                c = self.varc.get()
                mp = (-b) / 2 * a
                y = a * mp**2 + b * mp + c
                d = b**2-4*a*c
                if d < 0:
                        Label(self.master, text="No Solution for %.0fx² + %.0fx + %.0f = 0" % (a, b, c,)).grid(row=6,column=1)
                elif d == 0:
                        x = (-b+d**0.5/2*a)
                        Label(self.master, text="One Solution for %.0fx² + %.0fx + %.0f = 0" % (a, b, c,)).grid(row=7,column=1)
                        Label(self.master, text="x= %f" % x).grid(row=8,column=1)
                else:
                        x1 = ((-b)+(((b*b)-(4*a*c))**0.5))/(2*a)
                        x2 = ((-b)-(((b*b)-(4*a*c))**0.5))/(2*a)
                        Label(self.master, text="Two Solutions for %.0fx² + %.0fx + %.0f = 0:" % (a, b, c)).grid(row=9,column=1)
                        Label(self.master, text="x = %f" % x1).grid(row=10,column=1)
                        Label(self.master, text="x = %f" % x2).grid(row=11,column=1)
                Label(self.master, text="Line of Symmetry: x = %f" % mp).grid(row=12,column=1)
                Label(self.master, text="Vertex = (%f, %f)" % (mp, y)).grid(row=13,column=1)
class quadratic_grapher(quadratics):
        def __init__(self, master):
                self.master = master
                self.a_coefficient = #what needs to be here to get the vara, varb and varc values in the quadratics class?
                self.b_coefficient = 
                self.c_coefficient = 
                f = Figure(figsize=(5,4), dpi=100)
                a = f.add_subplot(111)
                self.x1 = []
                self.y1 = []
                self.x2 = []
                self.y2 = []
                self.x_positive_increment = 1
                self.x_negative_decrement = -1
                self.x_positive = 0
                self.x_negative = 0
                for plotPoints in range(0, 10):
                    self.y_positive = self.a_coefficient * self.x_positive**2 + self.b_coefficient * self.x_positive + self.c_coefficient
                    self.y_negative = self.a_coefficient * self.x_negative**2 + self.b_coefficient * self.x_negative + self.c_coefficient
                    self.x1.append(self.x_positive)
                    self.x2.append(self.x_negative)
                    self.y1.append(self.y_positive)
                    self.y2.append(self.y_negative)
                    self.x_positive = self.x_positive + self.x_positive_increment
                    self.x_negative = self.x_negative + self.x_negative_decrement
                a.plot(self.x1, self.y1)
                a.plot(self.x2, self.y2)
                self.canvas = FigureCanvasTkAgg(f, master=master)
                self.canvas.show()
                self.canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
                self.toolbar = NavigationToolbar2TkAgg(self.canvas, master )
                self.toolbar.update()
                self.canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=1)
def main():
        root = Tk()
        home(root)
        root.mainloop()

if __name__ == '__main__':
        main()

Unlike some other languages - python does not automatically call the constructor of the super classes, so you will need your quadratic_grapher class to explicitly call the constructor of the quadratics class : 与其他一些语言不同-python不会自动调用父类的构造函数,因此您将需要您的quadratic_grapher类显式调用二次类的构造函数:

Put this on the first line of your init method of your quadratic_grapher class 将其放在您的quadratic_grapher类的init方法的第一行

super(quadratic_grapher, self).__init__(master)

and now - since quadratic_grapher is also an instance of quadratic you can access the variables just by using self - so your commented lines become : 现在-由于quadratic_grapher也是二次方的实例,因此您可以仅通过使用self来访问变量-因此您的注释行变为:

self.a_coefficient = self.vara
self.b_coefficient = self.varb
self.c_coefficient = self.varc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM