【发布时间】:2023-03-09 06:47:01
【问题描述】:
我是新来的,这是我的第一个问题。
我想构建一个程序,用户通过滚动条插入函数参数,并在 tkinter 中生成该函数的图形。
这是我的代码
from tkinter import*
import tkinter
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import numpy as np
root = Tk()
root.title("Grath")
#root.geometry('500x600')
var_a = DoubleVar()
var_b = DoubleVar()
var_c = DoubleVar()
x1 = np.linspace(100, 0, 1000)
def y(x):
a = float(var_a.get())
b = float(var_b.get())
c = float(var_c.get())
return a*x**2+b*x+c
def graph():
fig.add_subplot(111).plot(x1, y(x1))
canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack()
#Labels and Bottons
label0=Label(root,text="Ploting f(x) = ax^2+b*x+c").pack()
label1=Label(root,text="Val of a").pack()
textbox1=Scale(root,from_=0,to=10,orient=HORIZONTAL,variable = var_a).pack()
label2=Label(root,text="Val of b").pack()
textbox2=Scale(root,from_=0,to=10,orient=HORIZONTAL,variable = var_b).pack()
label3=Label(root,text="val of c").pack()
textbox3=Scale(root,from_=0,to=10,orient=HORIZONTAL,variable = var_c).pack()
button1 = Button(root,text="Run",command=graph).pack()
fig = Figure(figsize=(5, 4), dpi=100)
canvas = FigureCanvasTkAgg(fig, master=root) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack()
#canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
tkinter.mainloop()
我已经尝试过 .destroy () 函数以及定义一个用 .pack_forget () 隐藏框架的函数,但没有任何效果
【问题讨论】:
标签: python matplotlib tkinter canvas