【问题标题】:How to locate tkinter root window to destroy?如何定位要销毁的 tkinter 根窗口?
【发布时间】:2020-01-06 23:53:22
【问题描述】:

对于我当前使用 tkinter 的项目,我需要在我的 GUI 中有多个页面,并且能够通过我在此处链接到 Switch between two frames in tkinter 的答案来实现这一点。

但是,我找不到一种方法来实现对根窗口的调用而不破坏这个预先存在的代码。我正在尝试创建一个小部件按钮来销毁根窗口,但我找不到要销毁的特定根。

from tkinter import *

class MainApp(Tk):
    """Class that displays respective frame"""
    def __init__(self):
        Tk.__init__(self)
        self.winfo_toplevel().title("YouTube Ripper and Editor")
        self.resizable(0, 0)
        container = Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, Downloader, Audio_Player, Config, About):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="NSEW")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        """Raise called frame"""
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(Frame):
    """Class that contains the start page"""
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.configure(bg = f_color)
        self.controller = controller
        b_font = Font(family = "Franklin Gothic Book", size = 12)

        titleLabel = Label(self, text="Youtube Ripper and Editor", font = ("Franklin Gothic Book", 16,  "bold"), bg = f_color)
        titleLabel.pack(side="top", fill="x", pady= 30)

        button1 = Button(self, text="Downloader", command=lambda: controller.show_frame("Downloader"),
                            font = b_font, bg = b_color, activebackground = bp_color)
        button2 = Button(self, text="Audio Player", command=lambda: controller.show_frame("Audio_Player"),
                            font = b_font, bg = b_color, activebackground = bp_color)
        button3 = Button(self, text="MP3 Configurator", command=lambda: controller.show_frame("Config"),
                            font = b_font, bg = b_color, activebackground = bp_color)
        button4 = Button(self, text="About", command=lambda: controller.show_frame("About"),
                            font = b_font, bg = b_color, activebackground = bp_color)
        button5 = Button(self, text="Exit", command=self.exit, font = b_font, bg = b_color, activebackground = bp_color)
        # button1.pack(fill = 'x')
        # button2.pack(fill = 'x')
        # button3.pack(fill = 'x')
        # button4.pack(fill = 'x')
        button5.pack(fill = 'x')


    def exit(self):
        """Exit program"""
        MainApp.destroy()


def main():
    app = MainApp()
    app.mainloop()


main()

【问题讨论】:

  • 根窗口是appMainApp 实例——不幸的是它是main() 函数中的一个局部变量。但是,在MainApp 类的方法中,它是self。你想在哪里创建这个Button
  • 我正在尝试在 StartPage 类初始化 (button5) 中创建“退出”按钮。
  • 那么我认为@Junuxx 的回答会起作用。

标签: python python-3.x tkinter


【解决方案1】:

使用您在StartPage__init__ 中保存的对根窗口的引用,并在其上调用destroy

def exit(self):
    """Exit program"""
    self.controller.destroy()

【讨论】:

  • 从技术上讲,不能保证页面的控制器是根窗口。正是在这种特定情况下,但StartPage 的定义中没有任何内容要求它如此。
  • @BryanOakley:当然。我自己会使用不同的结构,但我不建议在一行可以解决当前问题时进行完整的重构。
  • 至于我的项目,效果很好。感谢您的帮助!
猜你喜欢
  • 2020-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多