【问题标题】:Tkinter navigation with frames带框架的 Tkinter 导航
【发布时间】:2017-05-01 16:49:31
【问题描述】:

你好,

我真的被这个 python 代码困住了。或许你能帮帮我?

我想用按钮导航页面。我有三个不同的框架。然后我按下一个按钮,一个框架在其他框架之上,但不隐藏其他框架。

如果我对您的问题不清楚,请尝试我的代码,您就会明白我的意思。

此代码来自但很少修改。

import tkinter as tk
from tkinter import ttk
from tkinter import StringVar

LARGE_FONT= ("Verdana", 12)


class Application(tk.Tk):

    def __init__(self, *args, **kwargs):

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

        tk.Tk.wm_title(self, "Title")

        container = tk.Frame(self, width=768, height=1000)
        container.pack(side="top", fill='both' , expand = 1)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, PageOne, PageTwo):

            frame = F(container, self)

            self.frames[F] = frame
            #frame.pack()
            frame.grid(row=0, column=0)

        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = ttk.Label(self, text="Start Page", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button = ttk.Button(self, text="Visit Page 1",
                            command=lambda: controller.show_frame(PageOne))
        button.pack()

        button2 = ttk.Button(self, text="Visit Page 2",
                            command=lambda: controller.show_frame(PageTwo))
        button2.pack()


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text="Page One!!!", font=LARGE_FONT)
        label.pack(pady=0,padx=100)

        button1 = ttk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage))
        button1.pack()

        button2 = ttk.Button(self, text="Page Two",
                            command=lambda: controller.show_frame(PageTwo))
        button2.pack()


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.Label(self, text="Page Two!!!", font=LARGE_FONT)
        label.pack(pady=0,padx=0)

        button1 = ttk.Button(self, text="Back to Home",
                            command=lambda: controller.show_frame(StartPage))
        button1.pack()

        button2 = ttk.Button(self, text="Page One",
                            command=lambda: controller.show_frame(PageOne))
        button2.pack()

        phone = StringVar()
        home = ttk.Radiobutton(self, text='Home', variable=phone, value='home')
        office = ttk.Radiobutton(self, text='Office', variable=phone, value='office')
        cell = ttk.Radiobutton(self, text='Mobile', variable=phone, value='cell')
        home.pack()
        office.pack()
        cell.pack()

app = Application()
app.mainloop()

非常感谢:)

【问题讨论】:

    标签: python python-3.x user-interface tkinter


    【解决方案1】:

    您需要告诉所有框架用完所有可用空间,以便它们覆盖它们下方的所有框架。你可以通过使用粘性参数来做到这一点:

    frame.grid(row=0, column=0, sticky='nsew')
    

    这告诉框架坚持北,南,东和西侧;换句话说,填充两个方向的所有空间。

    【讨论】:

      猜你喜欢
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      • 2014-11-10
      • 2020-08-06
      • 1970-01-01
      相关资源
      最近更新 更多