简体   繁体   English

使用功能在python中的框架之间切换

[英]Switching between frames in python with functions

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. 我可以删除“ go”框架,但是不确定如何显示其他框架。

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

Scope of parent seems to be confined to constructor initialization. parent范围似乎仅限于constructor初始化。 You are calling parent again in menuScreen function. 您正在menuScreen函数中再次调用parent menuScreen

Try defining a local variable within class say parent and pass parent1 as argument to the constructor. 尝试在类类parent定义一个局部变量,然后将parent1作为参数传递给构造函数。 That way parent attribute is visible throughout the class. 这样, parent属性在整个类中都是可见的。

I was able to advance to next screen using above, although it ran into some other issues which I believe may be dependent on other part of your code that's not presented here. 尽管上面遇到了一些其他问题,但我相信可能取决于此处未介绍的代码的其他部分,因此我可以使用上面的内容进入下一个屏幕。

Here is modified code. 这是修改后的代码。

from tkinter import *

class Trip:
    def __init__(self, parent1):
        self.parent = parent1
        self.go = Frame(self.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(self.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()

Output: 输出:

在此处输入图片说明

You need to create the two frames in __init__ first; 首先需要在__init__创建两个框架; then show the tk window with the first frame. 然后显示带有第一帧的tk窗口。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM