gpt4 book ai didi

python - 在 Tkinter 中更新 Canvas

转载 作者:行者123 更新时间:2023-12-01 02:23:58 26 4
gpt4 key购买 nike

有人知道如何修改以下代码,以便每次更改测角函数时都会更新图形吗?绘图是在函数 plot() 中完成的。 stackoverflow 上有一些相关线程,但我无法将它们应用到我的示例代码中...

非常感谢,

麦基

import numpy as np
import Tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

class App:

def __init__(self, window):

self.window = window

# set up pi
pi = 3.141592654

# create the frame and pack to the object
frame = tk.Frame(window)
frame.pack()

def callback(func, *pargs):
print(func.get())
func.set(func.get())
self.plot()
self.window.update()

self.func = tk.StringVar()
self.func.set("sin") # default value
self.func.trace('w', lambda *pargs: callback(self.func, *pargs))

self.button = tk.OptionMenu(window, self.func, "sin", "cos", "tan")
self.button.pack(side = tk.TOP)

self.min_value = tk.Entry(window, justify = tk.RIGHT)
self.min_value.pack()

self.min_value.delete(0, tk.END)
self.min_value.insert(0, -pi)

self.max_value = tk.Entry(window, justify = tk.RIGHT)
self.max_value.pack()

self.max_value.delete(0, tk.END)
self.max_value.insert(0, pi)

self.button = tk.Button(frame, text = "QUIT", foreground = "red", command = frame.quit)
self.button.pack(side = tk.BOTTOM)

self.draw = tk.Button(frame, text = "DRAW", command = self.plot())
self.draw.pack(side = tk.LEFT)

def plot(self):

# generate numbers for the plot
x = np.array(np.arange(np.float64(self.min_value.get()), np.float64(self.max_value.get()), 0.001))

if self.func.get() == 'sin':
print('plotting sin()')
y = np.sin(x)
elif self.func.get() == 'cos':
print('plotting cos()')
y = np.cos(x)
else:
print('plotting tan()')
y = np.tan(x)

# create the plot
fig = Figure(figsize = (6, 6))
a = fig.add_subplot(1,1,1)
a.plot(x, y, color = 'blue')
a.set_title ("Goniometric Functions", fontsize = 12)
a.set_ylabel(self.func.get() + '(x)', fontsize = 8)
a.set_xlabel('x', fontsize = 8)

# canvas
canvas = FigureCanvasTkAgg(fig, master = self.window)
self.widget = canvas.get_tk_widget().pack()
print('here I should update canvas')
canvas.draw()

root = tk.Tk()
app = App(root)
root.mainloop()
root.destroy()

最佳答案

您应该在 Canvas 上创建空图,然后仅使用 a.set_xdata()a.set_ydata() 替换图中的数据

import numpy as np
import tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

class App:

def __init__(self, window):

self.window = window

# set up pi
pi = 3.141592654

# create the frame and pack to the object
frame = tk.Frame(window)
frame.pack()

def callback(func, *pargs):
print(func.get())
#func.set(func.get()) # <-- it has no sense
self.update_plot()
self.window.update()

self.func = tk.StringVar()
self.func.set("sin") # default value
self.func.trace('w', lambda *pargs: callback(self.func, *pargs))

self.button = tk.OptionMenu(window, self.func, "sin", "cos", "tan")
self.button.pack(side = tk.TOP)

self.min_value = tk.Entry(window, justify = tk.RIGHT)
self.min_value.pack()

self.min_value.delete(0, tk.END)
self.min_value.insert(0, -pi)

self.max_value = tk.Entry(window, justify=tk.RIGHT)
self.max_value.pack()

self.max_value.delete(0, tk.END)
self.max_value.insert(0, pi)

self.button = tk.Button(frame, text="QUIT", foreground="red", command=root.destroy) #<--
self.button.pack(side = tk.BOTTOM)

self.draw = tk.Button(frame, text="DRAW", command=self.update_plot)
self.draw.pack(side = tk.LEFT)

# create empty plot
self.create_plot()
# update plot with current function at start
self.update_plot()

def create_plot(self):

# create the plot
self.fig = Figure(figsize = (6, 6))
self.a = self.fig.add_subplot(1,1,1)

self.p, _ = self.a.plot([], [], color = 'blue')

self.a.set_title ("Goniometric Functions", fontsize = 12)

# canvas
self.canvas = FigureCanvasTkAgg(self.fig, master = self.window)
self.widget = self.canvas.get_tk_widget().pack()
#self.canvas.draw()

def update_plot(self):
print('update')

# generate numbers for the plot
x = np.array(np.arange(np.float64(self.min_value.get()), np.float64(self.max_value.get()), 0.001))

if self.func.get() == 'sin':
print('plotting sin()')
y = np.sin(x)
elif self.func.get() == 'cos':
print('plotting cos()')
y = np.cos(x)
else:
print('plotting tan()')
y = np.tan(x)

# replace labels
self.a.set_ylabel(self.func.get() + '(x)', fontsize = 8)
self.a.set_xlabel('x', fontsize = 8)

# replace data
self.p.set_xdata(x)
self.p.set_ydata(y)

# rescale
self.a.relim()
self.a.autoscale_view()

# update screen
self.canvas.draw()

root = tk.Tk()
app = App(root)
root.mainloop()
#root.destroy() You don't need it

关于python - 在 Tkinter 中更新 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47602364/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com