Search code examples
pythonmatplotlibtkinter

Why do I need to click twice to exit my python program?


I just went through all similar questions, but found nothing related to python. I have a program that works fine. Have an exit button that closes the program. Problem I am having is that after adding a function to plot the results (using matplotlib and pandas), after closing the plot, I need to click the exit button twice to close the program! Following example is based on the solution of user:7721581 to another problem of mine.

from tkinter import *
from matplotlib import pyplot as plt
import pandas as pd
import io

root = Tk()

quit_button = Button(root, text='Exit', command=root.quit)
quit_button.grid()

dummy_csv_file = '''Y,X1,X2,X3
1,5,10,15
2,6,11,16
3,7,12,17
4,8,13,18
5,9,14,19'''

df = pd.read_csv(io.StringIO(dummy_csv_file))
df.plot(x="Y", y=["X1", "X2", "X3"])

plt.show()

root.mainloop()

Solution

  • Here is the solution: Replace plt.show() by plt.show(block=False)