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
#1
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])
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   Tkinter_talking_Mpl.py (Size: 1.75 KB / Downloads: 54)
Reply
#2
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/us...gskip.html
Larz60+ likes this post
Reply
#3
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
Reply
#4
You could embed an interactive python interpreter in the program and use that to enter commands to change the plot.
Reply


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

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