简体   繁体   English

使用 tkInter 选择页面

[英]Selecting pages with tkInter

I'm new to python and trying to make a GUI with tkinter for a study application.我是 python 新手,正在尝试使用 tkinter 为学习应用程序制作 GUI。 I'm having trouble setting up my program to be able to navigate to different pages.我在设置程序以导航到不同页面时遇到问题。 I used this stack overflow question to set up my code.我使用这个堆栈溢出问题来设置我的代码。

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

the problem with this particular solution is that it creates buttons at the top of the parent window that allow the user to navigate to each page in the program.这个特定解决方案的问题在于它在父窗口的顶部创建了按钮,允许用户导航到程序中的每个页面。 It wouldn't make sense in my program to allow the user to navigate to any page at any time.在我的程序中允许用户随时导航到任何页面是没有意义的。 How would I create a button on a specific page that allows me to navigate to a different page?我将如何在特定页面上创建一个按钮以允许我导航到不同的页面?

study_button() is my attempt at this study_button()是我在这方面的尝试

from tkinter import *

#initializes each page as a class
class Page(Frame):
    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)
    def show(self):
        self.lift()

#creates a specific page
class SelectionPage(Page):
    def __init__(self, *args, **kwargs):
        Page.__init__(self, *args, **kwargs)
        label = Label(self, text='selection page')
        label.pack()

        def study_button():
            studypage = StudyPage(self)
            studypage.lift()

            print("study")

        studybutton = Button(self, text = "Study", command=study_button)
        studybutton.pack()


class StudyPage(Page):
    def __init__(self, *args, **kwargs):
        Page.__init__(self, *args, **kwargs)
        label = Label(self, text = 'this is the study page')
        label.pack()

class ModifyPage(Page):
    def __init__(self, *args, **kwargs):
        Page.__init__(self, *args, **kwargs)
        label = Label(self, text = 'this is the modify page')
        label.pack()
#base page
class MainView(Frame):
    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)

        studypage = StudyPage(self)
        selectionpage = SelectionPage(self)
        modifypage = ModifyPage(self)

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

        studypage.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        selectionpage.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        modifypage.place(in_=container, x=0, y=0, relwidth=1, relheight=1)

        selectionpage.show()



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

Here's how to apply the pattern shown in one of @Bryan Oakley's many other tkinter-related answers to your code.以下是如何将@Bryan Oakley 的许多其他与 tkinter 相关的答案之一中显示的模式应用于您的代码。 It adds a pages dictionary attribute to the MainView class which can be used in the Page subclasses to reference any of the instance of the others by their class name.它向MainView类添加了一个pages字典属性,该属性可以在Page子类中使用,以通过它们的类名引用其他实例的任何实例。

To facilitate that, a named controller argument has been added to the calling sequence of each subclass.为方便起见,已将命名controller参数添加到每个子类的调用序列中。 This will be the instance of MainView controlling them.这将是MainView控制它们的实例。

Note I also added a Button to your StudyPage that goes to the ModifyPage to give you a better idea of the pattern.注意我还在您的StudyPage中添加了一个Button ,该Button转到ModifyPage以使您更好地了解该模式。

from tkinter import *


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

    def show(self):
        self.lift()


class SelectionPage(Page):
    def __init__(self, controller, *args, **kwargs):
        Page.__init__(self, *args, **kwargs)
        label = Label(self, text='Selection page')
        label.pack()

        def study_button():
            studypage = controller.pages['StudyPage']
            studypage.show()
            print("study")

        studybutton = Button(self, text="Study", command=study_button)
        studybutton.pack()


class StudyPage(Page):
    def __init__(self, controller, *args, **kwargs):
        Page.__init__(self, *args, **kwargs)
        label = Label(self, text='This is the study page')
        label.pack()

        def modify_button():
            modifypage = controller.pages['ModifyPage']
            modifypage.show()
            print("modifying")

        modifybutton = Button(self, text="Modify", command=modify_button)
        modifybutton.pack()


class ModifyPage(Page):
    def __init__(self, controller, *args, **kwargs):
        Page.__init__(self, *args, **kwargs)
        label = Label(self, text='This is the modify page')
        label.pack()


class MainView(Frame):
    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)

        # Create dictionary of Page subclasses.
        self.pages = {}
        for Subclass in (StudyPage, SelectionPage, ModifyPage):
            self.pages[Subclass.__name__] = Subclass(self)

        studypage, selectionpage, modifypage = self.pages.values()

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

        studypage.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        selectionpage.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        modifypage.place(in_=container, x=0, y=0, relwidth=1, relheight=1)

        selectionpage.show()



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

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

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