Python GUI 打开一个“新页面" [英] Python GUI open a "new page"

查看:32
本文介绍了Python GUI 打开一个“新页面"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I press a button, I want to open a new side. Not a new window, the window should be the same, just the interface should change.

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

How can I solve this without opening a new 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()

推荐答案

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

You could create two frames in the same place, and lifting them over one another using the lift and lower methods (example taken from Bryan Oakley here and slightly altered):

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()

您可以将第一页"放在一个框架中,将第二页"放在另一个框架中

You could place 'page one' in one frame and 'page two' in the other

这篇关于Python GUI 打开一个“新页面"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆