Python / Tkinter:如何在首次运行应用程序时显示空白底框?

时间:2017-07-11 00:31:03

标签: python tkinter

首先运行应用程序时,屏幕上会显示最后一帧的小部件。我想要做的是,当用户点击他们的按钮时显示相关的帧。所以,我想用顶部按钮显示一个空白框架。为了做到这一点,我该怎么办? (我删除了按钮功能,因为它们与问题无关。)提前感谢。

import tkinter as tk


class TopFrame(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid(row=0, column=0, sticky="nsew")

        self.BottomFrame = tk.Frame(master=master)
        self.BottomFrame.grid(row=1, column=0, sticky="nsew")

        self.f1 = tk.Frame(master=self.BottomFrame)
        self.f2 = tk.Frame(master=self.BottomFrame)
        self.f3 = tk.Frame(master=self.BottomFrame)

        for f in (self.f1, self.f2, self.f3):
            f.grid(row=0, column=0, sticky="nsew")

        self.b1 = tk.Button(master=self, text="Add Words")
        self.b2 = tk.Button(master=self, text="Add From File")
        self.b3 = tk.Button(master=self, text="Change Words")

        self.add_button = tk.Button(master=self.f1, text="Add")
        self.open_button = tk.Button(master=self.f2, text="Open File")
        self.change_button = tk.Button(master=self.f3, text="Change")

        self.l1 = tk.Label(master=self.f1, text="English")
        self.l2 = tk.Label(master=self.f1, text="Turkish")
        self.l3 = tk.Label(master=self.f3, text="Old word")
        self.l4 = tk.Label(master=self.f3, text="New word")

        self.e1 = tk.Entry(master=self.f1)
        self.e2 = tk.Entry(master=self.f1)
        self.e3 = tk.Entry(master=self.f3)
        self.e4 = tk.Entry(master=self.f3)

        self.configure_buttons()
        self.configure_labels()
        self.configure_entries()

    def configure_buttons(self):
        self.b1.grid(row=0, column=0)
        self.b1.configure(command=lambda: self.f1.tkraise())
        self.b2.grid(row=0, column=1)
        self.b2.configure(command=lambda: self.f2.tkraise())
        self.b3.grid(row=0, column=2)
        self.b3.configure(command=lambda: self.f3.tkraise())

        self.add_button.grid(row=2, columnspan=2)
        #self.add_button.configure(command=self.add_word)
        self.open_button.pack(side="top")
        #self.open_button.configure(command=self.add_from_file)
        self.change_button.grid(row=2, columnspan=2)

    def configure_labels(self):
        self.l1.grid(row=0, column=0)
        self.l2.grid(row=0, column=1)
        self.l3.grid(row=0, column=0)
        self.l4.grid(row=0, column=1)

    def configure_entries(self):
        self.e1.grid(row=1, column=0)
        self.e2.grid(row=1, column=1)
        self.e3.grid(row=1, column=0)
        self.e4.grid(row=1, column=1)


if __name__ == "__main__":
    root = tk.Tk()
    example = TopFrame(master=root)
    example.mainloop()

1 个答案:

答案 0 :(得分:1)

不要在同一个位置放置3个小部件,最好只拥有你需要的小部件。

首先,摆脱这段代码:

for f in (self.f1, self.f2, self.f3):
    f.grid(row=0, column=0, sticky="nsew")

现在框架将以空白状态开始。

然后,我们将移除当前帧(如果有的话)并在其位置添加另一个帧,而不是在帧上调用.tkraise()。所以

self.b1.configure(command=lambda: self.f1.tkraise())
self.b2.configure(command=lambda: self.f2.tkraise())
self.b3.configure(command=lambda: self.f3.tkraise())

变为:

self.b1.configure(command=lambda: self._activate(self.f1))
self.b2.configure(command=lambda: self._activate(self.f2))
self.b3.configure(command=lambda: self._activate(self.f3))

def _activate(self, frame):
    # remove the current frame
    for child in self.BottomFrame.winfo_children():
        child.grid_forget()

    # add the new frame in its place
    frame.grid(row=0, column=0, sticky='nsew')