Python Forum
Interaction between Matplotlib window, Python prompt and TKinter window - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Interaction between Matplotlib window, Python prompt and TKinter window (/thread-41742.html)



Interaction between Matplotlib window, Python prompt and TKinter window - NorbertMoussy - Mar-13-2024

Hi,
I would like to have a Maplotlib window with a graph and interacting with normal instruction from the kernel. In parallel, I would like to have a QT or tkinter window that behave independetly (in a thread) that interact also with the matplotlib window for instance to modify the selection of the data to plot. I have globally a problem of "main thread" saying that mpl instructions have to be done from the same thread as the thread that called the mpl window. If I call the mpl window from the tkinter thread , I cannot interact directly with the matplotlib window with the kernel prompt. Is it clear ? Do you have a solution for this ?

For instance I tried this script, but tkinter window is not able to modify the mpl graph (I attached the file, don't know how to put is correctly inside the message).

    import tkinter as tk
    import threading
    import queue

    import matplotlib.pyplot as plt
    %matplotlib qt

    class MainWindow(tk.Tk):
        def __init__(self, initial_value, event_queue, fig ):
            super().__init__()
            self.event_queue = event_queue
            self.title('Parameter window')
            self.geometry('300x150')

            self.listcounter = initial_value
            self.fig = fig
            self.counter = self.listcounter[0]

            self.label = tk.Label(self, text=f'Present value : {self.counter}')
            self.label.pack(pady=10)

            self.add_button = tk.Button(self, text='Add 1', command=self.add_one)
            self.add_button.pack(pady=5)

            self.close_button = tk.Button(self, text='Close', command=self.close_window)
            self.close_button.pack(pady=5)

        def add_one(self):

            self.counter += 1
            self.label.config(text=f'Present value : {self.counter}')
            self.listcounter.append(self.counter)
            self.event_queue.put(('CounterChanged', self.counter))
            ax = self.fig.axes[0]
            ax.set_title('From parameter window')
            ax.grid()

        def close_window(self):
            self.destroy()

    def listener(event_queue):
        while True:
            event = event_queue.get()
            if event[0] == 'CounterChanged':
                print(f"Event detected : counter value : {event[1]}")

    def run_gui(initial_value, event_queue, fig):
        window = MainWindow(initial_value, event_queue, fig)
        window.mainloop()

    if __name__ == '__main__':
        fig = plt.figure()
        event_queue = queue.Queue()
        initial_value = [10]  
        thread = threading.Thread(target=run_gui, args=(initial_value, event_queue, fig,))
        thread.start()
        plt.plot([1,3,5], [5,9,3])



RE: Interaction between Matplotlib window, Python prompt and TKinter window - deanhystad - Mar-13-2024

I don't think you can do what you ask, but you can do something similar.

This links to an example where a plot is drawn in a tkinter window. Because it doesn't call show(), the figure is available for manipulation by the python program. The example creates a tkinter.Scale widget that is used to change a parameter for plotted signal..

https://matplotlib.org/stable/gallery/user_interfaces/embedding_in_tk_sgskip.html


RE: Interaction between Matplotlib window, Python prompt and TKinter window - NorbertMoussy - Mar-15-2024

Ok yes, it is a good option, it works in a thread. The only downside is that the instructions cannot be done through plt.method() but via figure.method() or axe.method(), but it works. Thanks. Big Grin


RE: Interaction between Matplotlib window, Python prompt and TKinter window - deanhystad - Mar-17-2024

You could embed an interactive python interpreter in the program and use that to enter commands to change the plot.