Python Forum
How to instantly update the plot by getting values from a Scale widget?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to instantly update the plot by getting values from a Scale widget?
#8
(May-16-2022, 05:14 PM)menator01 Wrote: Maybe this will get you started. I've not ever worked with matplotlib or numpy but, anyway, here's my attempt
import tkinter as tk
import numpy as np
from matplotlib.figure import Figure
from matplotlib import cm
from matplotlib.colors import ListedColormap
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

np.random.seed(2)

def plot(var, window):
	num = list(np.random.randint(low=1, high=10, size=120))
	nx, ny = 3, 4
	reshaped = np.array(num).reshape(10, nx*ny)
	layer = reshaped[var-1:var, :]
	layer = layer.reshape(nx, ny)
	x, y = np.mgrid[slice(0, nx+1, 1), slice(0, ny+1, 1)]

	figure = Figure(figsize=(4, 4))
	ax = figure.add_subplot(111)
	col_type = cm.get_cmap('rainbow', 256)
	newcolors = col_type(np.linspace(0, 1, 1000))
	white = np.array([1, 1, 1, 1])
	newcolors[:1, :] = white
	newcmap = ListedColormap(newcolors)

	c = ax.pcolormesh(x, y, layer, cmap=newcmap, edgecolor='lightgray', linewidth=0.003)
	ax.figure.colorbar(c)

	canvas = FigureCanvasTkAgg(figure, window)
	canvas.draw()
	canvas.get_tk_widget().pack()


class Window:
    def __init__(self, parent):
        self.slidervar = tk.IntVar()
        self.slider = tk.Scale(parent, from_=1, to=10, orient='horizontal')
        self.slider['variable'] = self.slidervar
        self.slider.pack()

        self.button = tk.Button(parent)
        self.button['text'] = 'Open Window'
        self.button.pack()


class TopWindow:
    def __init__(self):
        self.window = tk.Toplevel(None)
        self.window.geometry('+500+300')
        self.label = tk.Label(self.window)
        self.label.pack()

class Controller:
    def __init__(self, window):
        self.window = window

        self.window.button['command'] = self.openwindow
        self.window.slider['command'] = self.update

    def openwindow(self):
        self.topwindow = TopWindow()
        plot(self.window.slidervar.get(), self.topwindow.window)

    def update(self, event):
        plot(self.window.slidervar.get(), self.topwindow.window)

if __name__ == '__main__':
    root = tk.Tk()
    root.geometry('200x200+250+250')
    Controller(Window(root))
    root.mainloop()

Thanks for your solution. The reason I am confused a little bit, is that this problem is a small example of my bigger code. Some of the parameters in the plot() are called from other classes, and my class's structure is a little bit different than the one you wrote. Should the plot function be always out of the classes or it can be considered within a class?
Reply


Messages In This Thread
RE: How to instantly update the plot by getting values from a Scale widget? - by OLE - May-16-2022, 05:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Update exist plot while change upper limit or lower limit SamLiu 1 1,031 Feb-05-2023, 10:09 AM
Last Post: SamLiu
Exclamation Update Point Coordinates from Mouse Event in a Plot Jguillerm 2 1,371 Jan-10-2023, 07:53 AM
Last Post: Jguillerm
  [Tkinter] Update matplotlib plot correctly Particledust 0 4,704 Apr-20-2020, 04:59 PM
Last Post: Particledust
  [Tkinter] Scale at the Top Friend 5 2,933 Jul-20-2019, 05:02 PM
Last Post: Yoriz
  Update plot by <Return> bind with entry widget Zorro 1 4,224 Mar-09-2019, 12:27 PM
Last Post: Zorro
  [Tkinter] update the content of Text widget atlass218 10 16,411 Dec-15-2018, 11:51 AM
Last Post: atlass218
  [Tkinter] Update value in Entry widget dannyH 7 27,929 Apr-02-2017, 10:12 AM
Last Post: dannyH
  [Tkinter] Resetting scale value back to 0 after the slider loses focus lwhistler 3 7,114 Mar-07-2017, 10:01 PM
Last Post: Larz60+
  Bokeh - dynamic update widget.Select values sixteenornumber 0 10,347 Dec-28-2016, 02:15 PM
Last Post: sixteenornumber

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020