|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import tkinter as tk
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
from mpl_toolkits import axisartist
window = tk.Tk()
window.title("二次函数")
window.geometry("600x600")
figure = Figure(figsize=(5, 4), dpi=100)
axes = axisartist.Subplot(figure, 111)
figure.add_axes(axes)
canvas = FigureCanvasTkAgg(figure, master=window)
toolbar = NavigationToolbar2Tk(canvas, window)
toolbar.update()
canvas.get_tk_widget().pack()
l = tk.Label(window,bg = "yellow",width = 20,text = "一元二次函数")
l.pack()
def Repaint(source):
a_value = a_scale.get()
b_value = b_scale.get()
c_value = c_scale.get()
# 计算绘图中线
mid = 0
if a_value != 0:
mid = -b_value / 2 / a_value
x = np.linspace(mid - 10, mid + 10, 200)
y = np.zeros(x.shape)
for i in range(x.shape[0]):
y[i] = a_value * x[i] ** 2 + b_value * x[i] + c_value
y_min, y_max = -10, 10
if a_value > 0:
y_min, y_max = min(y) - 5, 50
elif a_value < 0:
y_min, y_max = -50, max(y) + 5
ax = plt.gca()
ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")
ax.xaxis.set_ticks_position("bottom")
ax.yaxis.set_ticks_position("left")
ax.spines["bottom"].set_position(("data",0))
ax.spines["left"].set_position(("data",0))
a_scale = tk.Scale(window,label = "a",from_ = -10,to = 10,orient = tk.HORIZONTAL,
length = 200,showvalue = 0,tickinterval = 2,resolution = 1,command = Repaint)
b_scale = tk.Scale(window,label = "b",from_ = -10,to = 10,orient = tk.HORIZONTAL,
length = 200,showvalue = 0,tickinterval = 2,resolution = 1,command = Repaint)
c_scale = tk.Scale(window,label = "c",from_ = -10,to = 10,orient = tk.HORIZONTAL,
length = 200,showvalue = 0,tickinterval = 2,resolution = 1,command = Repaint)
a_scale.pack()
b_scale.pack()
c_scale.pack()
a = a_scale.get()
b = b_scale.get()
c = c_scale.get()
l = tk.Label(window,text = "二次函数图像",font = ("Arial",12),width = 15,height = 2)
l.pack()
plt.show()
window.mainloop()
本帖最后由 sunrise085 于 2020-8-6 19:27 编辑
看39行到55行 import tkinter as tk
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
from mpl_toolkits import axisartist
window = tk.Tk()
window.title("二次函数")
window.geometry("600x600")
figure = Figure(figsize=(5, 4), dpi=100)
axes = axisartist.Subplot(figure, 111)
figure.add_axes(axes)
canvas = FigureCanvasTkAgg(figure, master=window)
toolbar = NavigationToolbar2Tk(canvas, window)
toolbar.update()
canvas.get_tk_widget().pack()
l = tk.Label(window,bg = "yellow",width = 20,text = "一元二次函数")
l.pack()
def Repaint(source):
a_value = a_scale.get()
b_value = b_scale.get()
c_value = c_scale.get()
# 计算绘图中线
mid = 0
if a_value != 0:
mid = -b_value / 2 / a_value
x = np.linspace(mid - 10, mid + 10, 200)
y = np.zeros(x.shape)
for i in range(x.shape[0]):
y[i] = a_value * x[i] ** 2 + b_value * x[i] + c_value
y_min, y_max = -10, 10
if a_value > 0:
y_min, y_max = min(y) - 5, 50
elif a_value < 0:
y_min, y_max = -50, max(y) + 5
#这里添加这四句,第一句清除原有图形,第二句根据新的xy值图形,第三句和第四句重绘图形。
axes.clear()
axes.plot(x,y)
figure.canvas.draw()
figure.canvas.flush_events()
#下面这几句导致又绘制了一张图
'''ax = plt.gca()
ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")
ax.xaxis.set_ticks_position("bottom")
ax.yaxis.set_ticks_position("left")
ax.spines["bottom"].set_position(("data",0))
ax.spines["left"].set_position(("data",0)) '''
a_scale = tk.Scale(window,label = "a",from_ = -10,to = 10,orient = tk.HORIZONTAL,
length = 200,showvalue = 0,tickinterval = 2,resolution = 1,command = Repaint)
b_scale = tk.Scale(window,label = "b",from_ = -10,to = 10,orient = tk.HORIZONTAL,
length = 200,showvalue = 0,tickinterval = 2,resolution = 1,command = Repaint)
c_scale = tk.Scale(window,label = "c",from_ = -10,to = 10,orient = tk.HORIZONTAL,
length = 200,showvalue = 0,tickinterval = 2,resolution = 1,command = Repaint)
a_scale.pack()
b_scale.pack()
c_scale.pack()
a = a_scale.get()
b = b_scale.get()
c = c_scale.get()
l = tk.Label(window,text = "二次函数图像",font = ("Arial",12),width = 15,height = 2)
l.pack()
plt.show()
window.mainloop()
|
|