Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用tkInter选择页面_Python_Python 3.x_Tkinter - Fatal编程技术网

Python 使用tkInter选择页面

Python 使用tkInter选择页面,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我是python新手,正在尝试使用tkinter为研究应用程序制作GUI。我无法设置我的程序,以便能够导航到不同的页面。我使用这个堆栈溢出问题来设置代码 此特定解决方案的问题在于,它在父窗口顶部创建了按钮,允许用户导航到程序中的每个页面。在我的程序中,允许用户在任何时候导航到任何页面是没有意义的。如何在特定页面上创建一个按钮,使我能够导航到其他页面 study\u button()是我的尝试 from tkinter import * #initializes each page as a

我是python新手,正在尝试使用tkinter为研究应用程序制作GUI。我无法设置我的程序,以便能够导航到不同的页面。我使用这个堆栈溢出问题来设置代码

此特定解决方案的问题在于,它在父窗口顶部创建了按钮,允许用户导航到程序中的每个页面。在我的程序中,允许用户在任何时候导航到任何页面是没有意义的。如何在特定页面上创建一个按钮,使我能够导航到其他页面

study\u 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()

下面是如何应用@Bryan Oakley的许多其他tkinter中与代码相关的模式之一。它将
pages
字典属性添加到
MainView
类中,该属性可在
pages
子类中使用,以通过其类名引用其他类的任何实例

为了方便起见,在每个子类的调用序列中添加了一个命名的
controller
参数。这将是
MainView
控制它们的实例

注意:我还向您的
StudyPage
添加了一个
按钮,该按钮会转到
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()

你的问题不清楚。在每个页面上放置按钮的方法很多,没有什么独特的地方需要做。像添加其他按钮一样添加按钮。星号导入是个坏主意。@Bryan Oakley关于我链接的那个线程,我已经修改了你写的代码好几个小时了,试图找出它。我不明白的是,当你在tkinter中为一个页面创建一个类时,你是否创建过该页面的实例?我一直在尝试打印实例,以便更好地了解我正在处理的对象,但我不知道如何调用它们“当您为页面创建类时,您是否创建过该页面的实例?”:这定义了一个页面对象:
class StudyPage(page):
,这创建了一个实例:
StudyPage=StudyPage(self)
。实例的引用被分配到
studypage
。您能解释一下为什么使用字典来存储子类的名称吗?仅仅使用列表不是更容易吗?我是python新手,我只是想弄明白这一点。我使用字典提供了一种简单易读的方法来选择所需的页面子类,避免出现幻数,以及依赖于它们的创建和添加顺序。我也看到过使用子类本身作为标记来实现这一点,但这是非常罕见的,并且有其潜在的缺点。