您的位置:首页 > 编程语言 > Python开发

在python GUI编程中从父类(继承的类)中调用tkinter标准quit方法导致程序无响应的问题

2019-03-19 18:03 477 查看
版权声明:本文为博主原创文章,未经博主允许禁止转载! https://blog.csdn.net/az9996/article/details/88668858

=======================================================================

在jupyter和pycharm中均会出现此问题。

问题的具体描述:按钮功能为,点击按钮关闭窗口。当调用父类(或继承的类)中的quit方法传递给command,会出现按钮按下,但窗口未关闭,程序无响应。

问题的具体原因还不清楚。

解决方法
  使用sys库中的exit方法来进行关闭窗口的操作,无异常发生。

下面是例子:

=======================================================================

from tkinter import *

def greeting():
print('Hello stdout world!...')

win = Frame()
win.pack(side=TOP, expand=YES, fill=BOTH)
Button(win, text='Hello', command=greeting).pack(side=LEFT, fill=Y)
Label(win,  text='Hello container world').pack(side=TOP)
Button(win, text='Quit', command=win.quit).pack(side=RIGHT, expand=YES,fill=X)

win.mainloop()

代码描述:

按下Hello按钮将触发此文件中定义的greeting函数,该函数将再次打印到stdout。

按下退出按钮调用由win从Frame类(Frame)继承的标准tkinter退出方法。退出与Tk具有相同的效果。退出我们之前使用的)。

修改后正常程序正常关闭。

from tkinter import *
from tkinter_test.gui6 import Hello
from sys import exit

class HelloContainer(Frame):
def __init__(self,parent=None):
Frame.__init__(self,parent)
self.pack()
self.makeWidgets()

def makeWidgets(self):
Hello(self).pack(side=RIGHT)
Button(self,text='Attach',command=exit).pack(side=LEFT)

if __name__ == '__main__':
HelloContainer().mainloop()

从sys导入exit
将command=win.quit修改为command=exit

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐