简体   繁体   English

无法在 Python/TkInter 中创建多个页面

[英]Can't create multiple pages in Python/TkInter

I'm trying to create multiple pages in my Python program but don't know how.我正在尝试在我的 Python 程序中创建多个页面,但不知道如何创建。 Here I have the first page of the program.这是程序的第一页。 I'm trying to make the buttons of this program link to another page.我试图让这个程序的按钮链接到另一个页面。
The goal is to create a base program using TkInter and Canvas, that integrates other sub-programs into the base program.目标是使用 TkInter 和 Canvas 创建一个基本程序,将其他子程序集成到基本程序中。 Most of the answers have things such as self , or _init_ , but that was not the way how I learned it.大多数答案都有诸如self_init_东西,但这不是我学习它的方式。

from tkinter import *


tkw = Tk()
cvs = Canvas(width="800", height="600", background="#7ce577")


"""
FIRST ROW BUTTONS
"""
cvs.create_oval(50, 65, 250, 265, fill="#a0ccda", outline="#a0ccdb")
cvs.create_text(148, 161, text="L", font="Arial 60 bold", state="normal")
btnScott = Button(tkw, text="Lee", font="Arial 16", command="scott", bg="#a0ccda")
btnScott.place(x=100, y=275, width="100", height="50")

cvs.create_oval(295, 65, 495, 265, fill="#a0ccda", outline="#a0ccdb")
cvs.create_text(395, 161, text="W", font="Arial 60 bold", state="normal")
btnMan = Button(tkw, text="Wei Hong", font="Arial 16", command="man", bg="#a0ccda")
btnMan.place(x=345, y=275, width="100", height="50")

cvs.create_oval(540, 65, 740, 265, fill="#a0ccda", outline="#a0ccdb")
cvs.create_text(640, 161, text="R", font="Arial 60 bold", state="normal")
btnRof = Button(tkw, text="Rofieq", font="Arial 16", command="rof", bg="#a0ccda")
btnRof.place(x=590, y=275, width="100", height="50")


"""
SECOND ROW BUTTONS
"""
cvs.create_oval(170, 340, 370, 540, fill="#a0ccda", outline="#a0ccdb")
cvs.create_text(270, 435, text="E", font="Arial 60 bold", state="normal")
btnYang = Button(tkw, text="En Yang", font="Arial 16", command="yang", bg="#a0ccda")
btnYang.place(x=220, y=550, width="100", height="50")

cvs.create_oval(420, 340, 620, 540, fill="#a0ccda", outline="#a0ccdb")
cvs.create_text(520, 435, text="S", font="Arial 60 bold", state="normal")
btnYun = Button(tkw, text="Sze Yun", font="Arial 16", command="yun", bg="#a0ccda")
btnYun.place(x=470, y=550, width="100", height="50")


cvs.pack()
cvs.mainloop()

Basic example showing the use of two pages:显示使用两个页面的基本示例:

When the app is created, it in turn creates two pages and switches to PageOne using self.show_frame(PageOne) .创建应用程序时,它会依次创建两个页面并使用self.show_frame(PageOne)切换到PageOne When created, each page is passed a reference to the main app, which is maintained via self.controller .创建时,每个页面都会传递一个对主应用程序的引用,该引用通过self.controller维护。 self.controller can be used by any child frame to access methods and data bellowing to the main app. self.controller可以被任何子框架用来访问主应用程序的方法和数据。 Data or functionality needed by one or more pages would normally be contained within the main app, meaning all pages have access.一个或多个页面所需的数据或功能通常包含在主应用程序中,这意味着所有页面都可以访问。 In the example both pages use the main app method show_frame .在示例中,两个页面都使用主应用程序方法show_frame

from tkinter import *
import tkinter.ttk as ttk 


class MyApp(Tk):
    def __init__(self):
        Tk.__init__(self)
        container = ttk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        self.frames = {}
        for F in (PageOne, PageTwo):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky='NSEW')
        self.show_frame(PageOne)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class PageOne(ttk.Frame):
    def __init__(self, parent, controller):
        self.controller = controller
        ttk.Frame.__init__(self, parent)
        self.make_widget()

    def make_widget(self):
        self.cvs = Canvas(self, width="800", height="600", background="#7ce577")
        """
        FIRST ROW BUTTONS
        """
        self.cvs.create_oval(50, 65, 250, 265, fill="#a0ccda", outline="#a0ccdb")
        self.cvs.create_text(148, 161, text="L", font="Arial 60 bold", state="normal")
        btnScott = Button(self.cvs, text="Lee", font="Arial 16", command="scott", bg="#a0ccda")
        btnScott.place(x=100, y=275, width="100", height="50")

        self.cvs.create_oval(295, 65, 495, 265, fill="#a0ccda", outline="#a0ccdb")
        self.cvs.create_text(395, 161, text="W", font="Arial 60 bold", state="normal")
        btnMan = Button(self.cvs, text="Wei Hong", font="Arial 16", command="man", bg="#a0ccda")
        btnMan.place(x=345, y=275, width="100", height="50")

        self.cvs.create_oval(540, 65, 740, 265, fill="#a0ccda", outline="#a0ccdb")
        self.cvs.create_text(640, 161, text="R", font="Arial 60 bold", state="normal")
        btnRof = Button(self.cvs, text="Rofieq", font="Arial 16", command="rof", bg="#a0ccda")
        btnRof.place(x=590, y=275, width="100", height="50")


        """
        SECOND ROW BUTTONS
        """
        self.cvs.create_oval(170, 340, 370, 540, fill="#a0ccda", outline="#a0ccdb")
        self.cvs.create_text(270, 435, text="E", font="Arial 60 bold", state="normal")
        btnYang = Button(self.cvs, text="En Yang", font="Arial 16", command="yang", bg="#a0ccda")
        btnYang.place(x=220, y=550, width="100", height="50")

        self.cvs.create_oval(420, 340, 620, 540, fill="#a0ccda", outline="#a0ccdb")
        self.cvs.create_text(520, 435, text="S", font="Arial 60 bold", state="normal")
        btnYun = Button(self.cvs, text="Sze Yun", font="Arial 16", command="yun", bg="#a0ccda")
        btnYun.place(x=470, y=550, width="100", height="50")

        # demo button to change page
        btnChange = Button(self.cvs, text="Change", font="Arial 16",
                           command=lambda: self.controller.show_frame(PageTwo),
                           bg="#a0ccda")
        btnChange .place(x=600, y=550, width="100", height="50")

        self.cvs.pack()

        def change_page(self):
            pass


class PageTwo(ttk.Frame):
    def __init__(self, parent, controller):
        self.controller = controller  
        ttk.Frame.__init__(self, parent)
        self.make_widget()

    def make_widget(self):
        ttk.Label(self, text='This is page two').grid(padx=(20,20), pady=(20,20))
        button1 = ttk.Button(self, text='Previous Page',
                             command=lambda: self.controller.show_frame(PageOne))
        button1.grid()


if __name__ == '__main__':
    app = MyApp()
    app.title('Multi-Page Test App')
    app.mainloop()

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

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