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

3 篇文章 0 订阅

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

在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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值