如何在Python中Tkinter中的类内使用slider.on_changed()

2024-09-30 04:33:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用水平滑块更改绘图的xlim。但首先,我不知道如何使用on_changed()方法更新滑块。我对类、对象和对象之间的交互方式没有很好的理解

我将此滑块示例用作模板:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons

fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
t = np.arange(0.0, 1.0, 0.001)
a0 = 5
f0 = 3
delta_f = 5.0
s = a0 * np.sin(2 * np.pi * f0 * t)
l, = plt.plot(t, s, lw=2)
ax.margins(x=0)

axcolor = 'lightgoldenrodyellow'
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
axamp = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)

sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0, valstep=delta_f)
samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)


def update(val):
    amp = samp.val
    freq = sfreq.val
    l.set_ydata(amp*np.sin(2*np.pi*freq*t))
    fig.canvas.draw_idle()


sfreq.on_changed(update)
samp.on_changed(update)

resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')


def reset(event):
    sfreq.reset()
    samp.reset()
button.on_clicked(reset)

rax = plt.axes([0.025, 0.5, 0.15, 0.15], facecolor=axcolor)
radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0)


def colorfunc(label):
    l.set_color(label)
    fig.canvas.draw_idle()
radio.on_clicked(colorfunc)

plt.show()

在我的应用程序中实现时遇到问题的部分是:

sfreq.on_changed(update)
samp.on_changed(update)

如果您使用plt.show()打开绘图,它可以正常工作,但是如果您像下面所做的那样将其打包到画布中,它将停止工作。你知道为什么吗

以下是我的应用程序的代码: 更新:我简化了代码以关注问题

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import(FigureCanvasTkAgg, NavigationToolbar2Tk)

from tkinter import *
import tkinter as tk

global running
running = True

global graph_exists
graph_exists = False

class MyApp:
    def __init__(self, parent):
        self.myParent = parent ###remember my parent, the root
        self.sfreq = 0
        self.samp = 0

        
        
    def run(self):
        self.fig, self.ax = plt.subplots()
        plt.subplots_adjust(left=0.25, bottom=0.25)
        self.t = np.arange(0.0, 1.0, 0.001)
        a0 = 5
        f0 = 3
        delta_f = 5.0
        s = a0 * np.sin(2 * np.pi * f0 * self.t)
        self.l, = plt.plot(self.t, s, lw=2)
        self.ax.margins(x=0)

        axcolor = 'lightgoldenrodyellow'
        axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
        axamp = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)

        self.sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0, valstep=delta_f)
        self.samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)

        canvas = FigureCanvasTkAgg(self.fig, root)
        canvas.draw()
        canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
        graph_exists = True

        

    def update(self, val):
        amp = self.samp.val
        freq = self.sfreq.val
        self.l.set_ydata(amp*np.sin(2*np.pi*freq*self.t))
        self.fig.canvas.draw_idle()

    if graph_exists:
        self.sfreq.on_changed(update)
        self.samp.on_changed(update)
        

#Run the event loop
root = tk.Tk()
myapp = MyApp(root)
myapp.run()
root.mainloop()

Tags: importselfmatplotlibonnpfigupdateplt
2条回答

代码的问题是方法update从未被调用。。。您需要将部分if graph_exists放入run方法中。此外,正如@acw1668共享帖子中所建议的,您还需要在绘图之前将图形添加到画布中

请尝试下面的代码

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import(FigureCanvasTkAgg, NavigationToolbar2Tk)

from tkinter import *
import tkinter as tk

global running
running = True

global graph_exists
graph_exists = False

class MyApp:

    def __init__(self, parent):
        self.myParent = parent ###remember my parent, the root
        self.sfreq = 0
        self.samp = 0

    def run(self):
        self.fig, self.ax = plt.subplots()
        canvas = FigureCanvasTkAgg(self.fig, root)
        canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
        
        plt.subplots_adjust(left=0.25, bottom=0.25)
        self.t = np.arange(0.0, 1.0, 0.001)
        a0 = 5
        f0 = 3
        delta_f = 5.0
        s = a0 * np.sin(2 * np.pi * f0 * self.t)
        self.l, = plt.plot(self.t, s, lw=2)
        self.ax.margins(x=0)

        axcolor = 'lightgoldenrodyellow'
        axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
        axamp = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)

        self.sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0, valstep=delta_f)
        self.samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)
        graph_exists = True
        
        if graph_exists:
            self.sfreq.on_changed(self.update)
            self.samp.on_changed(self.update)

    def update(self, val):
        print('inside update')
        amp = self.samp.val
        freq = self.sfreq.val
        self.l.set_ydata(amp*np.sin(2*np.pi*freq*self.t))
        
        self.fig.canvas.draw_idle()
        

#Run the event loop
root = tk.Tk()
myapp = MyApp(root)
myapp.run()
root.mainloop()

正如我在评论中的链接中所说,在打印之前,您需要将图形添加到画布,否则滑块无法工作。此外,在创建滑块后,还应调用on_changed(...)

    def run(self):
        self.fig, self.ax = plt.subplots()
        canvas = FigureCanvasTkAgg(self.fig, root)  # add the figure to canvas before plotting
        plt.subplots_adjust(left=0.25, bottom=0.25)
        self.t = np.arange(0.0, 1.0, 0.001)
        a0 = 5
        f0 = 3
        delta_f = 5.0
        s = a0 * np.sin(2 * np.pi * f0 * self.t)
        self.l, = plt.plot(self.t, s, lw=2)
        self.ax.margins(x=0)

        axcolor = 'lightgoldenrodyellow'
        axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], facecolor=axcolor)
        axamp = plt.axes([0.25, 0.15, 0.65, 0.03], facecolor=axcolor)

        self.sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0, valstep=delta_f)
        self.samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)
        ### setup on_changed callbacks
        self.sfreq.on_changed(self.update) # use 'self.update' instead of 'update'
        self.samp.on_changed(self.update)
        ###
        #canvas = FigureCanvasTkAgg(self.fig, root)
        canvas.draw()
        canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
        graph_exists = True

相关问题 更多 >

    热门问题