python - 使用 TKinter 编辑窗口

标签 python user-interface tkinter

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

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

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

但我不确定下面的代码中的“nGui”是什么(根据要求我的整个代码)。因此,当运行它时,它会出现一个非常小的窗口。我认为它可能是“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()

预先感谢您 - 如果这是在错误的地方,请仁慈并告诉我我是这个网站的新手。

最佳答案

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

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

关于python - 使用 TKinter 编辑窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42744171/

相关文章:

Python/Pygame - 创建 "End credits"就像电影结尾处的那样

python - 列出 python 模块的子模块

JQuery 自动完成 : changing look and feel

javascript - 如何控制td中包含更多字符(包括字母和数值)的文本?

java - 我可以在 Java 桌面应用程序中执行此操作吗?

python - Tkinter 中的按钮命令

javascript - 名称错误 : name "something" is not defined - Web App using Angular JS and Python

python - 使用 Python 将二维列表打印为多项式

python - 如何将字符串用于图标位图?

python - Tkinter 文本小部件比任何其他带有网格的小部件更宽