【问题标题】:Clearing frames in tkinter在 tkinter 中清除帧
【发布时间】:2016-05-03 17:37:13
【问题描述】:

在没有任何正式培训和对 Python 3.3 非常基本的了解的情况下,我正在努力理解 tkinter 以制作基本的 gui。我已经阅读了这种方法:

Tkinter! Understanding how to switch frames

这超出了我的想象。我从复制和粘贴代码中学得不好。我的问题是这样的:

如果我想制作一个带有多个窗口的 gui,我通过简单地销毁一个框架并在使用类时用另一个框架替换它来实现,我想我只是不明白为什么它如此困难。今天玩了一会儿,不明白为什么下面的代码不起作用。我不能把我的面条包裹起来是为什么它可以正确翻转到新窗口,但无法返回主窗口。两个按钮的编码相同,用于各自的目的,但一个有效,一个无效不。

from tkinter import *

class Application:
    def __init__(self, master):
        self.master = master
        self.new_switch_on = False
        self.main_switch_on = False
        self.main_frame()

    def main_frame(self):
        if self.new_switch_on == True:
            self.new_frame.destroy()
        self.new_switch_on = False
        self.main_switch_on = True
        self.main_frame = Frame(self.master, width = 200, height = 100)
        Label(self.main_frame, text = 'Main Frame').pack()
        Button(self.main_frame, text = 'Next', command = self.new_frame).pack()
        self.main_frame.pack()

    def new_frame(self):
        if self.main_switch_on == True:
            self.main_frame.destroy()
        self.main_switch_on = False
        self.new_switch_on = True
        self.new_frame = Frame(self.master, width = 400, height = 200)
        Label(self.new_frame, text = 'New Frame').pack()
        Button(self.new_frame, text = 'Back', command = self.main_frame).pack()
        self.new_frame.pack()

root = Tk()
root.geometry('-1+1')

app = Application(root)

root.mainloop()

我知道我可以按照上述链接获得所需的结果,但我只是不明白为什么这样的事情,或者对于新学习者来说更简单的其他事情不能达到相同的结果。

【问题讨论】:

    标签: tkinter frame python-3.3


    【解决方案1】:

    问题是因为您为方法main_frame(self, ...) 和框架self.main_frame = Frame(...) 使用了相同的名称。

    当您将框架分配给变量 self.main_frame = Frame(...) 时,您将失去对方法 self.main_frame() 的访问权限,并且您的 Button(self.new_frame, text = 'Back', command=self.main_frame) 无法调用方法 self.main_frame。

    为方法 main_frame 使用不同的名称 - 例如 create_main_frame

    new_frame(self, ...) 和 self.new_frame = Frame(...) 也有同样的问题

    (ps。我在代码中添加了空行以使其更具可读性)

    from tkinter import *
    
    class Application:
    
        def __init__(self, master):
            self.master = master
    
            self.new_switch_on = False
            self.main_switch_on = False
    
            self.create_main_frame()
    
        def create_main_frame(self): # new name
            if self.new_switch_on == True:
                self.new_frame.destroy()
    
            self.new_switch_on = False
            self.main_switch_on = True
    
            self.main_frame = Frame(self.master, width = 200, height = 100)
    
            Label(self.main_frame, text = 'Main Frame').pack()
            Button(self.main_frame, text = 'Next', command = self.create_new_frame).pack()  # new name
    
            self.main_frame.pack()
    
        def create_new_frame(self): # new name
            if self.main_switch_on == True:
                self.main_frame.destroy()
    
            self.main_switch_on = False
            self.new_switch_on = True
    
            self.new_frame = Frame(self.master, width = 400, height = 200)
    
            Label(self.new_frame, text='New Frame').pack()
            Button(self.new_frame, text='Back', command=self.create_main_frame).pack()  # new name
    
            self.new_frame.pack()
    
    
    root = Tk()
    root.geometry('-1+1')
    
    app = Application(root)
    
    root.mainloop()
    

    【讨论】:

    • 是的...我觉得自己像个白痴,尤其是因为我知道不这样做。感谢您指出这一点。完全按预期工作,现在在消除布尔值并更改一些内容之后,它仍然可以完美运行。
    猜你喜欢
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 2018-02-10
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多