使用 RangeSliders 控制轴范围并自动更新图形

Using RangeSliders to control axis ranges and automatically update the graph

我想使用 RangeSliders, 控制轴的范围,当我设置 left/right 或 up/down 滑块时,图形会自动更新。

我最初编写了一个代码来使用按钮完成此任务。

import tkinter as tk
from tkinter import *
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from RangeSlider.RangeSlider import RangeSliderH, RangeSliderV

root = Tk()

root.geometry("700x500")

def plot():
    figure = Figure(figsize=(5, 4))
    ax = figure.add_subplot(111)

    ax.set_xlim(rs1.getValues())
    ax.set_ylim(rs2.getValues())

    canvas = FigureCanvasTkAgg(figure, root)
    canvas.get_tk_widget().place(x=120, y=0)
    figure.patch.set_facecolor('#f0f0f0')

hVar1 = IntVar() # left handle variable
hVar2 = IntVar()  # right handle variable
rs1 = RangeSliderH(root, [hVar1, hVar2], Width=230, Height=55, padX=17, min_val=0, max_val=100, font_size=12,\
                   show_value=True, digit_precision='.0f', bgColor='#f0f0f0', line_s_color='black',\
                   line_color='black', bar_color_inner='black', bar_color_outer='#f0f0f0')
rs1.place(x=250, y=400)

vVar1 = IntVar()  # top handle variable
vVar2 = IntVar()  # down handle variable
rs2 = RangeSliderV(root, [vVar1, vVar2], Width=81, Height=180, padY=11, min_val=0, max_val=100, font_size=12,\
                   show_value=True, digit_precision='.0f', bgColor='#f0f0f0', line_s_color='black',\
                   line_color='black', bar_color_inner='black', bar_color_outer='#f0f0f0')
rs2.place(x=0, y=150)

button = Button(root, text="Plot", command=plot, width=10)
button.place(x=300, y=470)

root.mainloop()

我通过使用引入的方法 3 从下面的 link 获得自动更新的想法来编写这段代码:Constantly Update Label Widgets From Entry Widgets TKinter 但是我不知道如何在没有按钮的情况下让它自动工作。

import tkinter as tk
from tkinter import *
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from RangeSlider.RangeSlider import RangeSliderH, RangeSliderV

root = Tk()

root.geometry("700x500")

def plot():
    figure = Figure(figsize=(5, 4))
    ax = figure.add_subplot(111)

    ax.set_xlim(rs1.getValues())
    ax.set_ylim(rs2.getValues())

    canvas = FigureCanvasTkAgg(figure, root)
    canvas.get_tk_widget().place(x=120, y=0)
    figure.patch.set_facecolor('#f0f0f0')

def auto():
    ax.set_xlim(rs1.getValues())
    ax.set_ylim(rs2.getValues())
    # call again after 100 ms
    root.after(100, auto)


hVar1 = IntVar() # left handle variable
hVar2 = IntVar()  # right handle variable
rs1 = RangeSliderH(root, [hVar1, hVar2], Width=230, Height=55, padX=17, min_val=0, max_val=100, font_size=12,\
                   show_value=True, digit_precision='.0f', bgColor='#f0f0f0', line_s_color='black',\
                   line_color='black', bar_color_inner='black', bar_color_outer='#f0f0f0')
rs1.place(x=250, y=400)


vVar1 = IntVar()  # top handle variable
vVar2 = IntVar()  # down handle variable
rs2 = RangeSliderV(root, [vVar1, vVar2], Width=81, Height=180, padY=11, min_val=0, max_val=100, font_size=12,\
                   show_value=True, digit_precision='.0f', bgColor='#f0f0f0', line_s_color='black',\
                   line_color='black', bar_color_inner='black', bar_color_outer='#f0f0f0')
rs2.place(x=0, y=150)

auto()

button = Button(root, text="Plot", command=plot, width=10)
button.place(x=300, y=470)

root.mainloop()

该代码大部分重用了您的示例代码。主要变化:

  • 不同的功能 1) 启动图形图(即 returns 创建图形和轴对象)和 2)更新轴对象
  • 使用 RangeSlider 库中概述的 .trace_add() 调用更新函数

我也删除了from tkinter import *because reasons

import tkinter as tk
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from RangeSlider.RangeSlider import RangeSliderH, RangeSliderV

root = tk.Tk()

root.geometry("700x500")

def plot_init():
    figure = Figure(figsize=(5, 4))
    ax = figure.add_subplot(111)    
    canvas = FigureCanvasTkAgg(figure, root)
    canvas.get_tk_widget().place(x=120, y=0)
    figure.patch.set_facecolor('#f0f0f0')
    return figure, ax

def plot_update(name, index, operation):
    ax1.set_title(f"last change: {name}")
    ax1.set_xlim(rs1.getValues())
    ax1.set_ylim(rs2.getValues())
    fig1.canvas.draw_idle()
    
hVar1 = tk.IntVar(name="horizontal slider") # left handle variable
hVar2 = tk.IntVar()  # right handle variable
rs1 = RangeSliderH(root, [hVar1, hVar2], Width=230, Height=55, padX=17, min_val=0, max_val=100, font_size=12,\
                   show_value=True, digit_precision='.0f', bgColor='#f0f0f0', line_s_color='black',\
                   line_color='black', bar_color_inner='black', bar_color_outer='#f0f0f0')
rs1.place(x=250, y=400)


vVar1 = tk.IntVar(name="vertical slider")  # top handle variable
vVar2 = tk.IntVar()  # down handle variable
rs2 = RangeSliderV(root, [vVar1, vVar2], Width=81, Height=180, padY=11, min_val=0, max_val=100, font_size=12,\
                   show_value=True, digit_precision='.0f', bgColor='#f0f0f0', line_s_color='black',\
                   line_color='black', bar_color_inner='black', bar_color_outer='#f0f0f0')
rs2.place(x=0, y=150)


hVar1.trace_add('write', plot_update)
vVar1.trace_add('write', plot_update)

fig1, ax1 = plot_init()
plot_update("None", 0, 0)

root.mainloop()

示例输出:

matplotlib 提供的 RangeSlider library is rudimentary to non-existing, so why you only have to bind one of the two slider variables to the update function is anybody's guess. I would rather use the RangeSlider class 文档以供将来支持。