【问题标题】:Create a 'close/exit' option for a child frame that is created using tkinter in python为在 python 中使用 tkinter 创建的子框架创建一个“关闭/退出”选项
【发布时间】: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 上。
  • 谢谢。我删除了覆盖重定向,现在我可以关闭窗口了。

标签: python tkinter tcl


【解决方案1】:

此解决方案处理创建多个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 的实例中。

【讨论】:

  • 如果您描述了您所做的更改,您的答案会更好。否则,读者必须逐行将您的代码与原始代码进行比较。
  • 一次打开​​多个top会失败,只能关闭最后一个,其他都卡住
猜你喜欢
  • 1970-01-01
  • 2020-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-20
  • 2023-02-04
相关资源
最近更新 更多