使用 Tkinter 中的按钮导航到应用程序的不同页面? [英] Using buttons in Tkinter to navigate to different pages of the application?

查看:48
本文介绍了使用 Tkinter 中的按钮导航到应用程序的不同页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很简单的问题.在 Tkinter (python) 中,我想知道谁可以使用按钮转到我的应用程序的不同页面,例如注册页面和登录页面.我知道 GUI 不像网站那样有页面",我看到了几种不同的方式,但是链接到不同页面的最佳方式是什么?

I have quite a simple question here. In Tkinter (python), i was wondering who to use a button to go to different pages of my application, e.g a register page, and a login page. I am aware that GUI does not have 'pages' like websites do, ive seen a few different ways, but what is the best way to make links to different pages?

非常感谢大家!

推荐答案

使每个页面成为一个框架.然后,您需要做的所有按钮就是隐藏可见的内容,然后使所需的框架可见.

Make each page a frame. Then, all your buttons need to do is hide whatever is visible, then make the desired frame visible.

执行此操作的一种简单方法是将帧堆叠在一起(这是 place 有意义的一次),然后,lift()您希望可见的框架.当所有页面大小相同时,此技术效果最佳;实际上,它需要您明确设置包含框架的大小.

A simple method to do this is to stack the frames on top of each other (this is one time when place makes sense) and then ,lift() the frame you want to be visible. This technique works best when all pages are the same size; in fact, it requires that you explicitly set the size of containing frame.

下面是一个人为的例子.这不是解决问题的唯一方法,只是证明这不是一个特别难解决的问题:

The following is a contrived example. This isn't the only way to solve the problem, just proof that it's not a particularly hard problem to solve:

import Tkinter as tk

class Page(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
    def show(self):
        self.lift()

class Page1(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 1")
       label.pack(side="top", fill="both", expand=True)

class Page2(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 2")
       label.pack(side="top", fill="both", expand=True)

class Page3(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 3")
       label.pack(side="top", fill="both", expand=True)

class MainView(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        p1 = Page1(self)
        p2 = Page2(self)
        p3 = Page3(self)

        buttonframe = tk.Frame(self)
        container = tk.Frame(self)
        buttonframe.pack(side="top", fill="x", expand=False)
        container.pack(side="top", fill="both", expand=True)

        p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p3.place(in_=container, x=0, y=0, relwidth=1, relheight=1)

        b1 = tk.Button(buttonframe, text="Page 1", command=p1.show)
        b2 = tk.Button(buttonframe, text="Page 2", command=p2.show)
        b3 = tk.Button(buttonframe, text="Page 3", command=p3.show)

        b1.pack(side="left")
        b2.pack(side="left")
        b3.pack(side="left")

        p1.show()

if __name__ == "__main__":
    root = tk.Tk()
    main = MainView(root)
    main.pack(side="top", fill="both", expand=True)
    root.wm_geometry("400x400")
    root.mainloop()

这篇关于使用 Tkinter 中的按钮导航到应用程序的不同页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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