python退出mainloop_Python tkinter mainloop在关闭风的时候没有退出

我是编程和Python的新手,正在制作一个计算器应用程序来练习。

我在Python2.7中使用Tkinter。该应用程序有各种各样的按钮,以及显示数字/结果的条目和标签小部件。在

我想我的程序确实启动了mainloop,但是关闭窗口并不能停止mainloop。因为在我添加after循环之前它的工作正常,所以我假设after是问题所在。我也在使用wait_变量。在

如果您能看看我的代码并给出一些建议,我将不胜感激!

我已经包括了主要内容;创建小部件和处理用户输入/结果输出的代码在不同的文件(按钮、显示、输入)中,但是希望没有这些可以理解。在import Tkinter as tk

# These contain the other bits of code

import Buttons as bt

import Displays as ds

import Inputs as ip

class MainWindow(tk.Frame):

def __init__(self, parent):

tk.Frame.__init__(self, parent)

self.parent = parent

self.parent.title("Calculator")

# Making frames to fill with widgets

self.displays_frame = tk.Frame(parent)

self.displays_frame.grid(padx=10, pady=10)

self.buttons_frame = tk.Frame(parent)

self.buttons_frame.grid()

# Initialising the widgets and user input functions

self.display = ds.Displays(self.displays_frame)

self.button = bt.Button(self.buttons_frame)

self.input = ip.Inputs()

def take_inputs(self, parent):

self.parent = parent

# This waits for a button to be pressed

self.wait_variable(self.button.button_pressed)

# On a button press, its value is inserted into display Entry box/result is put in Label

# Both self.button.button_pressed above and self.display.r below are StringVars

self.input.process_input(self.button.button_pressed, self.display.entry_box, self.display.r)

# Using after here so it keeps waiting for button presses to be recorded

self.after(100, self.take_inputs, self.parent)

def main():

root = tk.Tk()

app = MainWindow(root)

# Here the wait_variable and after functions are called

app.take_inputs(root)

# The below string is printed after the first button has been pressed

print "main, now starting mainloop"

root.mainloop()

# "finished" is never printed

print "finished"

if __name__ == '__main__':

main()

我假设我已经创建了自己的事件处理程序,而不是使用mainloop,所以我确实尝试在take_inputs方法中添加self.parent.protocol("WM_DELETE_WINDOW", self.end(parent)),这样我就可以不必运行mainloop就可以退出所有操作了。这个自行结束函数我在MainWindow类中添加了一个方法,该方法打印“立即关闭”,然后退出或销毁程序。在

但是,无论我为protocol插入的任何函数都会立即运行;"WM_DELETE_WINDOW"没有被正确地查找(用“foo”替换“WM_DELETE_WINDOW”没有给出错误)。在

谢谢你的帮助!在

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值