下面是一个解决二次方程并绘制它们的程序。我想知道我需要把注释放在代码中的位置,以便获得父类(quadratics)的值并在Toplevel类中使用它。任何帮助将不胜感激。
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()
答案 0 :(得分:0)
与其他语言不同 - python不会自动调用超类的构造函数,因此您需要使用quadratic_grapher类来显式调用quadratics类的构造函数:
将它放在quadratic_grapher类的 init 方法的第一行
super(quadratic_grapher, self).__init__(master)
现在 - 因为quadratic_grapher也是二次方的实例,你可以只使用self来访问变量 - 所以你的注释行变为:
self.a_coefficient = self.vara
self.b_coefficient = self.varb
self.c_coefficient = self.varc