为在 python 中使用 tkinter 创建的子框架创建一个 'close/exit' 选项
Create a 'close/exit' option for a child frame that is created using tkinter in python
我使用 tkinter 创建了一个脚本,当您按下按钮时它会弹出一个子框架。该框架占据了我 mac 笔记本电脑的整个屏幕尺寸。现在我需要创建一个选项来退出/或关闭这个新框架。这样做的最佳选择是什么?
-谢谢
from Tkinter import *
import tkFont
class App(Frame):
def __init__(self, *args, **kwargs):
Frame.__init__(self, *args, **kwargs)
self.apple = Button(self,
text="Apple", command=self.write_apple)
self.apple.pack(side=LEFT)
def write_apple(self):
self.customFont = tkFont.Font(family="Helvetica", size=80)
t = Toplevel(self)
t.overrideredirect(True)
t.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
l = Label(t, text="This is a green apple.",font=self.customFont)
l.pack(side="top", fill="both", expand=True)
if __name__ == "__main__":
root = Tk()
main = App(root)
main.pack(side="top", fill="both", expand=True)
root.mainloop()
此解决方案处理创建多个 Toplevel
实例的情况:
from Tkinter import *
import tkFont
class App(Frame):
def __init__(self, *args, **kwargs):
Frame.__init__(self, *args, **kwargs)
self.apple = Button(self,
text="Apple", command=self.write_apple)
self.apple.pack(side=LEFT)
self.top_dict = dict()
def destroy_top(self, event):
# event.widget is the instance of Label that was clicked
# Get the instance of Toplevel
top = self.top_dict[event.widget]
# Destroy the instance of Toplevel
top.destroy()
# Remove the instance of Toplevel from the list
del self.top_dict[event.widget]
def write_apple(self):
self.customFont = tkFont.Font(family="Helvetica", size=80)
# Create an instance of Toplevel
top = Toplevel(self)
top.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
label = Label(top, text="This is a green apple.",font=self.customFont)
label.pack(side="top", fill="both", expand=True)
# Bind the destroy_top method to the mouse-button-1 click
label.bind('<Button-1>', self.destroy_top)
# Save the instance of Toplevel using the label as the key
self.top_dict[label] = top
if __name__ == "__main__":
root = Tk()
main = App(root)
main.pack(side="top", fill="both", expand=True)
root.mainloop()
注意:正如@Bryan Oakley 指出的那样,删除对 overrideredirect
的调用会向 Toplevel
.
的实例添加 window 装饰
我使用 tkinter 创建了一个脚本,当您按下按钮时它会弹出一个子框架。该框架占据了我 mac 笔记本电脑的整个屏幕尺寸。现在我需要创建一个选项来退出/或关闭这个新框架。这样做的最佳选择是什么?
-谢谢
from Tkinter import *
import tkFont
class App(Frame):
def __init__(self, *args, **kwargs):
Frame.__init__(self, *args, **kwargs)
self.apple = Button(self,
text="Apple", command=self.write_apple)
self.apple.pack(side=LEFT)
def write_apple(self):
self.customFont = tkFont.Font(family="Helvetica", size=80)
t = Toplevel(self)
t.overrideredirect(True)
t.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
l = Label(t, text="This is a green apple.",font=self.customFont)
l.pack(side="top", fill="both", expand=True)
if __name__ == "__main__":
root = Tk()
main = App(root)
main.pack(side="top", fill="both", expand=True)
root.mainloop()
此解决方案处理创建多个 Toplevel
实例的情况:
from Tkinter import *
import tkFont
class App(Frame):
def __init__(self, *args, **kwargs):
Frame.__init__(self, *args, **kwargs)
self.apple = Button(self,
text="Apple", command=self.write_apple)
self.apple.pack(side=LEFT)
self.top_dict = dict()
def destroy_top(self, event):
# event.widget is the instance of Label that was clicked
# Get the instance of Toplevel
top = self.top_dict[event.widget]
# Destroy the instance of Toplevel
top.destroy()
# Remove the instance of Toplevel from the list
del self.top_dict[event.widget]
def write_apple(self):
self.customFont = tkFont.Font(family="Helvetica", size=80)
# Create an instance of Toplevel
top = Toplevel(self)
top.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
label = Label(top, text="This is a green apple.",font=self.customFont)
label.pack(side="top", fill="both", expand=True)
# Bind the destroy_top method to the mouse-button-1 click
label.bind('<Button-1>', self.destroy_top)
# Save the instance of Toplevel using the label as the key
self.top_dict[label] = top
if __name__ == "__main__":
root = Tk()
main = App(root)
main.pack(side="top", fill="both", expand=True)
root.mainloop()
注意:正如@Bryan Oakley 指出的那样,删除对 overrideredirect
的调用会向 Toplevel
.