使用函数在python中的帧之间切换 [英] Switching between frames in python with functions

查看:35
本文介绍了使用函数在python中的帧之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有两个功能,其中的框架可以轻松切换.我可以删除go"框架,但我不确定之后如何显示另一个框架.

I wish to have two functions in which I have frames that I can easily switch between. I can remove the "go" frame but I am unsure how to show another frame afterward.

from tkinter import *

class Trip:
    def __init__(self, parent):
        self.go = Frame(parent, width=500, height=450)
        self.go.grid(row=0, column=0)
        self.go.grid_propagate(0)  # to reserve space required for frame
        menuButton = Button(self.go, text="Continue", command=self.menuScreen)
         menuButton.grid(row=1, column=0)


    def menuScreen(self):
        self.go.grid_remove()
        self.menu = Frame(parent, width=500, height=450, bg='orchid')
        self.menu.grid(row=0, column=0)
        self.menu.grid_propagate(0)  # to reserve space required for frame

        self.addMore = Button(self.menuScreen, text="Return", command=self.__init__)
        self.addmore.grid(row=1, column=0)


if __name__ == "__main__":
    root = Tk()
    root.title("Traveller Details")
    play = Trip(root)
    root.geometry("500x450+0+0")
    root.mainloop()

推荐答案

需要先在__init__中创建两个frame;然后用第一帧显示 tk 窗口.

You need to create the two frames in __init__ first; then show the tk window with the first frame.

每个框架上的可点击按钮允许您在两个框架之间来回切换.

A clickable button on each frame allows you to switch back and forth between the two frames.

"""
demonstrate switching back and forth between tkinter frames
"""

import tkinter as tk

class Trip:
    """
    A class to demonstrate switching back and forth between tkinter frames
    """

    def __init__(self, parent):
        self.parent = parent
        self.parent.title("Traveller Details")
        self.parent.geometry("500x450+0+0")        

        self.go_frame = tk.Frame(self.parent, width=500, height=450, bg='light blue')
        self.goto_menu_frame_button = tk.Button(self.go_frame, text="Continue", command=self.menu_screen)

        self.menu_frame = tk.Frame(self.parent, width=500, height=450, bg='light steel blue')
        self.goto_go_frame_button = tk.Button(self.menu_frame, text="Return", command=self.go_screen)

        self.current_frame = None
        self.go_screen()

    def go_screen(self):
        """
        The first screen to be visible - has a clickable button to switch 
        to the other screen 
        """
        self.remove_current_frame()
        self.current_frame = self.go_frame
        self.go_frame.grid(row=0, column=0)
        self.go_frame.grid_propagate(0)  # to reserve space required for frame
        self.goto_menu_frame_button.grid(row=1, column=0)

    def menu_screen(self):
        """
        The second screen - has a clickable button to switch back to the first screen 
        """
        self.remove_current_frame()
        self.current_frame = self.menu_frame
        self.menu_frame.grid(row=0, column=0)
        self.menu_frame.grid_propagate(0)  # to reserve space required for frame
        self.goto_go_frame_button.grid(row=0, column=0)

    def remove_current_frame(self):
        """
        removes the current frame from the grid if it exists
        """
        if self.current_frame is not None:
            self.current_frame.grid_remove()

    def start(self):
        """
        launches the GUI
        """
        self.parent.mainloop()


if __name__ == "__main__":
    trip = Trip(tk.Tk())
    trip.start()

这篇关于使用函数在python中的帧之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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