当 window 对象被封装时,你如何关闭一个 python - tkinter window?
How do you close a python - tkinter window, when the window object is encapsulated?
好的,这是我正在进行的一个更大的项目,如果看起来很乱,我深表歉意。
问题是当我单击 GUI 上的 'Exit Program' 按钮时,window 仍然处于活动状态。
我知道当我点击 window 右上角的 'x' 时按钮正在工作;程序关闭,因此 运行 变量已设置回 0,从而停止代码循环。
我的问题是如何让 window 在单击退出按钮时自动关闭,因为 root.destroy() 方法没有这样做。
#imports
from tkinter import *
import random, pickle, shelve
#global vars
run = 0
class Window(Frame):
#the class that manages the UI window
def __init__(self, master, screen_type = 0):
"""Initilize the frame"""
super(Window, self).__init__(master)
self.grid()
if screen_type == 1:
self.log_in_screen()
def log_in_screen(self):
#Program Exit Button
self.exit = Button(self, text = " Exit Program ", command = self.end)
self.exit.grid(row = 3, column = 0, columnspan = 2, sticky = W)
def end(self):
global run, root
run = 0
root.destroy()
#Main Loop
def main():
global run, root
run = 1
while run != 0:
root = Tk()
root.title("Budget Manager - 0.6.1")
root.geometry("400x120")
screen = Window(root, screen_type = run)
root.mainloop()
store = shelve.open("store.dat", "c")
main()
store.close()
My question is how do I get the window to be closed automatically when
the exit button is clicked, because the root.destroy() method isn't
doing it.
答案是:在根 window 上调用 destroy()
。你说它不工作,但你发布的代码似乎工作,destroy()
被记录要做的正是你描述的你想要发生的事情:它会破坏 window。您的代码在循环中创建了新的顶层 windows,因此可能只有 出现 无法工作,因为旧的 window id 已被破坏,而新的 window一眨眼的功夫。
您真正想问的似乎是 "how can I make clicking on the "x“与单击 "Exit program" 按钮一样吗?”。如果是这样的话,答案就非常简单了,即使你的非常规代码在循环中创建根 windows。
要让 window 框架上的 "x" 按钮调用函数而不是破坏 window,请使用 wm_protocol
方法和 "WM_DELETE_WINDOW"
常量和你希望它调用的函数。
例如:
while run != 0:
root = Tk()
...
screen = Window(root, screen_type = run)
root.wm_protocol("WM_DELETE_WINDOW", screen.end)
...
root.mainloop()
您可以执行以下操作。我已经在我自己的项目等中使用过它并且它有效。
Mywin =tkinter.Tk()
def exit():
Mywin.quit()
# etc.
好的,这是我正在进行的一个更大的项目,如果看起来很乱,我深表歉意。
问题是当我单击 GUI 上的 'Exit Program' 按钮时,window 仍然处于活动状态。 我知道当我点击 window 右上角的 'x' 时按钮正在工作;程序关闭,因此 运行 变量已设置回 0,从而停止代码循环。
我的问题是如何让 window 在单击退出按钮时自动关闭,因为 root.destroy() 方法没有这样做。
#imports
from tkinter import *
import random, pickle, shelve
#global vars
run = 0
class Window(Frame):
#the class that manages the UI window
def __init__(self, master, screen_type = 0):
"""Initilize the frame"""
super(Window, self).__init__(master)
self.grid()
if screen_type == 1:
self.log_in_screen()
def log_in_screen(self):
#Program Exit Button
self.exit = Button(self, text = " Exit Program ", command = self.end)
self.exit.grid(row = 3, column = 0, columnspan = 2, sticky = W)
def end(self):
global run, root
run = 0
root.destroy()
#Main Loop
def main():
global run, root
run = 1
while run != 0:
root = Tk()
root.title("Budget Manager - 0.6.1")
root.geometry("400x120")
screen = Window(root, screen_type = run)
root.mainloop()
store = shelve.open("store.dat", "c")
main()
store.close()
My question is how do I get the window to be closed automatically when the exit button is clicked, because the root.destroy() method isn't doing it.
答案是:在根 window 上调用 destroy()
。你说它不工作,但你发布的代码似乎工作,destroy()
被记录要做的正是你描述的你想要发生的事情:它会破坏 window。您的代码在循环中创建了新的顶层 windows,因此可能只有 出现 无法工作,因为旧的 window id 已被破坏,而新的 window一眨眼的功夫。
您真正想问的似乎是 "how can I make clicking on the "x“与单击 "Exit program" 按钮一样吗?”。如果是这样的话,答案就非常简单了,即使你的非常规代码在循环中创建根 windows。
要让 window 框架上的 "x" 按钮调用函数而不是破坏 window,请使用 wm_protocol
方法和 "WM_DELETE_WINDOW"
常量和你希望它调用的函数。
例如:
while run != 0:
root = Tk()
...
screen = Window(root, screen_type = run)
root.wm_protocol("WM_DELETE_WINDOW", screen.end)
...
root.mainloop()
您可以执行以下操作。我已经在我自己的项目等中使用过它并且它有效。
Mywin =tkinter.Tk()
def exit():
Mywin.quit()
# etc.