【发布时间】:2016-02-23 19:57:53
【问题描述】:
我使用 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()
【问题讨论】:
-
没有最好的方法,您需要使用最适合您的程序的方法。不过,您有时会需要
pack_forget()。 -
子 Frame 还是子 Toplevel?术语很重要。
-
您是否有理由不允许普通的窗户装饰?你为什么用
overrideredirect? -
我同意@Bryan Oakley 的观点,如果您不使用
overrideredirect,mac 上的关闭按钮或命令 W 可以正常工作。我注意到overrideredirect的各种奇怪行为,尤其是在 mac 上。 -
谢谢。我删除了覆盖重定向,现在我可以关闭窗口了。