如何在不终止程序的情况下关闭 tkinter 窗口? [英] how to close a tkinter window without terminating the program?

查看:44
本文介绍了如何在不终止程序的情况下关闭 tkinter 窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我正在为课堂工作,我正在使用 tkinter 来构建我的基本 GUI.当我运行代码时,我有两个下拉菜单可供选择.我还想要一个按钮来关闭窗口并将程序推进到下一个 GUI 窗口.但是,我无法在不导致程序终止的情况下获得关闭窗口的按钮.这是我的代码

i have a project that i am working on for class and i am using tkinter to build my basic GUI. when i run the code i have two drop down menus to choose options from. i also want a button to close the window and advance the program to the next GUI window. however i can not get a button to close the window without also causing the program to terminate. here is my code

from tkinter import *

Options_year = ["2014", "2013", "2012", "2011", "2010"]
Options_month = ["January","February", "March", "April","May", "June", "July","August","September","October","November",
                 "December"]
master = Tk()
variable_year = StringVar(master)
variable_year.set(Options_year[0])
variable_month = StringVar(master)
variable_month.set(Options_month[0])
window = apply(OptionMenu, (master, variable_year) + tuple(Options_year))
window_month = apply(OptionMenu, (master,variable_month) + tuple(Options_month))
window.pack()
window_month.pack()
button = Button(master, text = "Continue", command = master.quit())
#the line above is the button that i want to use to close the window
button.pack()
mainloop()
print (variable_month.get())
print (variable_year.get())

将其转换为框架并使用下面提供的答案并使其工作.感谢每一位帮助过我的人

converted this to a frame and used the supplied answer below and got it to work. thanks to every one who helped me

推荐答案

使用顶层或框架,将小部件放入其中,并销毁它.如果不想显示,可以使用 master.withdraw() 或 iconify().

Use a Toplevel or frame, put the widgets in it, and destroy() it. You can use master.withdraw() or iconify() if you do not want it to show.

这篇关于如何在不终止程序的情况下关闭 tkinter 窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆