我正在尝试在我的Python程序中创建多个页面,但不知道如何。在这里,我有程序的第一页。我正在尝试将此程序的按钮链接到另一个页面。
目标是使用TkInter和Canvas创建一个基本程序,它将其他子程序集成到基本程序中。大多数答案都包含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()
答案 0 :(得分:0)
显示使用两个页面的基本示例:
创建应用后,它会创建两个页面并使用PageOne
切换到self.show_frame(PageOne)
。创建后,每个页面都会传递对主应用程序的引用,该应用程序通过self.controller
进行维护。任何子框架都可以使用self.controller
来访问主应用程序的方法和数据。一个或多个页面所需的数据或功能通常包含在主应用程序中,这意味着所有页面都可以访问。在示例中,两个页面都使用主应用方法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()