使用TKinter编辑窗口

时间:2017-03-12 05:35:53

标签: python user-interface tkinter

当我有多个帧时,如何更改窗口的几何形状和标签?

没有框架,我的代码将是:

nGui = Tk()
nGui.geometry("500x500")

但我不确定' nGui'在下面的代码(我的整个代码请求)。因此,当它运行时,它会变成一个非常小的窗口。我想它可能是&t; tk.Tk'但是当我试图编辑它时,它只是创建了一个新窗口。

class DietBuddy(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)


        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.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, PageOne, Diet_Finder):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="HazaTea Productions", font=TITLE_FONT)
        label.place(relx=.5, rely=.5, anchor="center")

        time.sleep(2)
        button = tk.Button(self, text="Continue",
                           command=lambda: controller.show_frame("PageOne"))
        button.place(relx=.5, rely=.6, anchor="center")


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 1", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Find Diet",
                       command=lambda: controller.show_frame("Diet_Finder"))
        button.pack()


class Diet_Finder(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Diet Finder", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)


        button = tk.Button(self, text="Find my Diet!",
                       command=lambda: controller.show_frame("StartPage"))
        button.pack()


if __name__ == "__main__":
    app = DietBuddy()
    app.mainloop()

提前谢谢 - 如果这是在错误的地方,请怜悯并告诉我,我是这个网站的新手。

1 个答案:

答案 0 :(得分:1)

DietBuddy类子类Tk。因此,针对DietBuddy实例调用geometry方法:

if __name__ == "__main__":
    app = DietBuddy()
    app.geometry('500x500')  # <---
    app.mainloop()