2

I have a script as follows that executes on my windows machine

import matplotlib.pyplot as plt
import numpy as np
import tkinter

class main(tkinter.Frame): #main window
    def __init__(self, root): # initialise
        tkinter.Frame.__init__(self)
        self.root = root


        tkinter.Button(self, text='New spots', command=self.newSpots).grid()

    def newSpots(self):
        x = np.random.rand(10)
        y = np.random.rand(10)
        plt.scatter(x,y)
        plt.show()



if __name__=='__main__':
    root = tkinter.Tk()

    app = main(root).grid()
    root.mainloop()

When running on windows, it opens a window with a simple button, and clicking this button opens a matplotlib viewer with 10 dots plotted in random positions. Each subsequent press of the button adds a further ten dots.

Executing this code on a mac produces the same initial window, and the first press of the button generates the plot and opens the viewer as expected. However, it then becomes impossible to interact with the original window (only the controls on the viewer work) until the viewer window is closed. How do I make the behaviour on the mac mirror that on the windows machine?

1 Answer 1

1

I found a solution to this issue -- it seems matplotlib defaults to the TkAgg backend on Windows (I'm unsure whether this is a general Windows thing, or specific to whatever particular install is on the machine).

Adding the following lines to the top of the script forces the TkAgg backend and leads to the same behaviour on both machines.

import matplotlib
matplotlib.use("TkAgg")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.