1

I know there are a lot of thread heading in the same direction, but I couldn't find a solution. A lot of threads are about to close the GUI but still running the python code and that's what I don't want. But see my question below:

I am currently using Python 2.7 and Windows 7. I am developing a program which analyses my data which I read from a sensor. After I finish my python program I freeze it with cx_freeze to execute it on pc's without python or without matplotlib etc... . The problem I have atm is I want to add a quit button which closes my application. The problem is that I tried 3 different possibilities see below:

import Tkinter
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,  NavigationToolbar2TkAgg
import globals
import data2plot
#from __builtin__ import file
globals.init()

def plot(x, aw,temperature,water):
#function to plot via matplotlib in the gui

#global file and if someone refresh before load data, default data is test.csv
file = "test"

#Version1
def close_window():
    sys.exit()
#Version2    
def close_window2():
    root.quit()
#Version3        
def close_window3():
    root.destroy()


# GUI
root = Tkinter.Tk()


draw_button = Tkinter.Button(root, text="Quit", command = close_window)
draw_button.grid(row=1, column=2)

draw_button = Tkinter.Button(root, text="Quit2", command = close_window2)
draw_button.grid(row=1, column=3)

draw_button = Tkinter.Button(root, text="Quit3", command = close_window3)
draw_button.grid(row=1, column=4)

# init figure with the 3 different values and axes
fig = matplotlib.pyplot.figure()


canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().grid(row=0,column=1)
toolbar = NavigationToolbar2TkAgg(canvas, root)
toolbar.grid(row=1,column=1)

#starts loop for the figure
root.mainloop()

I have read that normally I should use the root.quit() option. But the only button which works without an error is the third button with root.destroy(). The problem is if I use the third button, the GUI is closing, but the program is still running? Do I have also exit the mainloop, but I thought I exit the mainloop with root.quit()?

The other 2 buttons are showing an error message and the program is crushing on Windows 7, but at least the whole program is closed. I also tried like some guys suggesting root.quit without brackets, but doesn't work at all.

The error message for the two buttons is:

Fatal Python error: PyEval_RestoreThread: NULL tstate

This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.

Now my question is how can I ensure that I use my GUI, plot some things and so on and if I push the quit button, the GUI closes and the whole program closes too?

Many thanks! Max

2 Answers 2

2

Hmm. root.quit() has always worked for me with no errors, but if it isn't for you, perhaps this is the best way to go, so that you close the GUI and the program:

root.destroy()
sys.exit()
0
1

I had trouble when I tried to close the app from a different thread, so I used this code , (it will invoke the quit from the GUI thread)

def exitApp(self):
    self.root.after(100,root.quit)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.