Python Forum
Interaction between Matplotlib window, Python prompt and TKinter window
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Interaction between Matplotlib window, Python prompt and TKinter window
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 ):
            self.event_queue = event_queue
            self.title('Parameter window')

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

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

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

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

        def add_one(self):

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

        def close_window(self):

    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)

    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,))
        plt.plot([1,3,5], [5,9,3])
deanhystad write Mar-13-2024, 05:55 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Attached Files

.py (Size: 1.75 KB / Downloads: 54)
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..
Larz60+ likes this post
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
You could embed an interactive python interpreter in the program and use that to enter commands to change the plot.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter multiple windows in the same window hosierycouch 1 359 May-30-2024, 04:28 AM
Last Post: deanhystad
  User Interaction with Graph in GUI Marty23 1 783 Mar-19-2024, 03:17 AM
Last Post: deanhystad
  [Tkinter] Tkinter Matplotlib Animation Graph not rendering dimidgen 3 1,403 Mar-12-2024, 02:09 PM
Last Post: deanhystad
  [Tkinter] CTkScrollableDropdown error: bad window path name ".!ctkscrollabledropdown" CopperGenie 1 772 Mar-03-2024, 04:32 AM
Last Post: deanhystad
  [Tkinter] (CLOSED) CTkScrollableDropdown error: bad window path name ".!ctkscrollabledropdown" CopperGenie 4 1,083 Mar-03-2024, 03:21 AM
Last Post: CopperGenie
  Transparent window background, but not text on it muzicman0 7 3,517 Feb-02-2024, 01:28 AM
Last Post: Joically
  Tkinter multiple windows in the same window tomro91 1 1,153 Oct-30-2023, 02:59 PM
Last Post: Larz60+
  [Tkinter] Modal window DPaul 14 3,755 Oct-18-2023, 06:11 AM
Last Post: DPaul
  [PyQt] PyQt5 window closing when trying to display a graph bianca 4 2,095 Aug-12-2023, 03:25 PM
Last Post: bianca
Exclamation [Tkinter] Error when closing the main window with destroy TomasSanchexx 1 1,023 Aug-06-2023, 01:54 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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