python设置tk退出_退出tkinter gui

在Python 2.7和Windows 7环境下,开发者遇到一个问题,即使用cx_Freeze创建的GUI程序中,无法通过退出按钮正确关闭程序。尝试了使用sys.exit()、root.quit()和root.destroy()三种方法,其中只有root.destroy()能够关闭GUI,但程序仍然继续运行。错误提示为'Fatal Python error: PyEval_RestoreThread: NULL tstate'。寻求解决方案以确保点击退出按钮时程序完全关闭。
部署运行你感兴趣的模型镜像

我知道有很多人都在同一个方向前进,但我找不到解决办法。很多线程即将关闭GUI,但仍在运行python代码,这是我不希望看到的。但请看下面我的问题:

我目前使用的是python2.7和windows7。我正在开发一个程序来分析我从传感器读取的数据。在我完成我的python程序后,我用cx\u freeze冻结它,以便在没有python或没有matplotlib的pc上执行它。我的问题是我想添加一个退出按钮,关闭我的应用程序。问题是,我尝试了3种不同的可能性,见下文: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()

我已经读到,通常我应该使用根。退出()选项。但是唯一能正常工作的按钮是第三个有错误的按钮根目录。销毁(). 问题是如果我使用第三个按钮,GUI正在关闭,但程序仍在运行?我是否也退出了主循环,但我认为我退出主循环时根。退出()? 在

另外两个按钮显示一条错误信息,程序在Windows7上崩溃了,但至少整个程序都关闭了。我也试着像一些人建议的那样根。退出没有括号,但根本不起作用。在

两个按钮的错误消息是: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.

 

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值