Python GUI打开一个“新页面”

时间:2014-06-30 14:20:38

标签: python user-interface tkinter window

当我按下按钮时,我想打开一个新的一面。不是一个新窗口,窗口应该是相同的,只是界面应该改变。

如何在不打开新窗口的情况下解决这个问题?

from tkinter import *

page1=Tk()
label1=Label(page1, text="This is page 1")
label1.pack()

def topage2():
    page2=Tk()
    label2=Label(page2, text="This is page 2")
    label2.pack()

button=Button(page1, text="To page 2", command=topage2)
button.pack()

mainloop()

1 个答案:

答案 0 :(得分:0)

您可以在同一个地方创建两个框架,并使用升降和降低方法将它们相互提升(例如取自Bryan Oakley here并略有改动):

import Tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.frame = tk.Frame(self)
        self.frame2 = tk.Frame(self)
        self.frame.place(relwidth=1, relheight=0.8, relx=0, rely=0)
        self.frame2.place(relwidth=1, relheight=0.8, relx=0, rely=0)
        self.label = tk.Label(self.frame, text="Hello, world")
        button1 = tk.Button(self, text="Click to hide label",
                           command=self.hide_label)
        button2 = tk.Button(self, text="Click to show label",
                            command=self.show_label)
        self.label.pack()
        button1.place(relwidth=0.5, relheight=0.15, relx=0.0, rely=0.825)
        button2.place(relwidth=0.5, relheight=0.15, relx=0.5, rely=0.825)

    def show_label(self, event=None):
        self.frame.lift(self.frame2)

    def hide_label(self, event=None):
        self.frame.lower(self.frame2)

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

你可以放置第一页'在一个框架和第二页'在另一个