当我尝试从一个类到另一个类获取一个DoubleVar值,并将其乘以一个整数时,我得到以下错误消息:
TypeError: unsupported operand type(s) for *: 'DoubleVar' and 'int'
我试图从vara
班级到varb
班级获得varc
,quadratics
和quadratic_grapher
的价值
任何人都可以帮我解决这个问题吗?
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))
class quadratic_grapher(quadratics):
def __init__(self, master):
super(quadratic_grapher, self).__init__(master)
self.master = master
self.a_coefficient = self.vara
self.b_coefficient = self.varb
self.c_coefficient = self.varc
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 :(得分:1)
a,b和c系数是variable class的实例。使用.get()
方法从实例中获取值。
以下是一个例子:
from tkinter import *
root = Tk()
var = DoubleVar()
var.set(1.0)
print(var.get())
print(var)
root.mainloop()