如何在python tkinter中将数据从一个页面传递到另一个页面?

时间:2016-10-11 20:49:37

标签: python python-3.x tkinter

我的节目是这个..     将tkinter导入为tk     来自tkinter import *

TITLE_FONT = ("Helvetica", 18, "bold")
class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        self.title(" Allocation")
        width, height = self.winfo_screenwidth(), self.winfo_screenheight()
        self.geometry('%dx%d+0+0' % (width,height))
        self.state('zoomed')
        self.wm_iconbitmap('icon.ico')
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

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

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        logo = tk.PhotoImage(file="backesh.ppm")
        BGlabel = tk.Label(self,image=logo)
        BGlabel.image = logo
        BGlabel.place(x=0,y=0,width=592,height=450)
        label = tk.Label(self, text="This is the start page", font=TITLE_FONT)
        label.place(x=0,y=0,width=592,height=44)
        frame1 = Frame(self)
        Label(frame1, bd=5,bg="black",text="  Enter text :  ",font=("Helvetica", 14),fg="light green",width=21).pack(side=LEFT)
        emp=Entry(frame1, bd =5,font=("Times New Roman", 14),width=25)
        emp.pack(side=LEFT)
        frame1.place(x=400,y=160)

        button1 = tk.Button(self, text="Go to Page One",
                        command=lambda: controller.show_frame(PageOne))
        button2 = tk.Button(self, text="Go to Page two",
                        command=lambda: controller.show_frame(PageTwo))
        button3 = tk.Button(self, text="Exit",
                        command=self.quit)
        button1.place(x=100,y=406,width=200,height=44)
        button2.place(x=300,y=406,width=200,height=44)
        button3.place(x=500,y=406,width=80,height=44)


class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        logo = tk.PhotoImage(file="backesh.ppm")
        BGlabel = tk.Label(self,image=logo)
        BGlabel.image = logo
        BGlabel.place(x=0,y=0,width=592,height=450)
        label = tk.Label(self, text="This is page one", font=TITLE_FONT)
        label.place(x=0,y=0,width=592,height=44)

        button1 = tk.Button(self, text="Go to Start Page",
                        command=lambda: controller.show_frame(StartPage))
    #button2 = tk.Button(self, text="Go to Page two",
     #                   command=lambda: controller.show_frame(PageTwo))
        button3 = tk.Button(self, text="Exit",
                        command=self.quit)
        button1.place(x=100,y=406,width=200,height=44)
        button3.place(x=300,y=406,width=200,height=44)

class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        logo = tk.PhotoImage(file="backesh.ppm")
        BGlabel = tk.Label(self,image=logo)
        BGlabel.image = logo
        BGlabel.place(x=0,y=0,width=592,height=450)
        label = tk.Label(self, text="This is page two", font=TITLE_FONT)
        label.place(x=0,y=0,width=592,height=44)

        button1 = tk.Button(self, text="Go to Start Page",
                        command=lambda: controller.show_frame(StartPage))
    #button2 = tk.Button(self, text="Go to Page two",
     #                   command=lambda: controller.show_frame(PageTwo))
        button3 = tk.Button(self, text="Exit",
                        command=self.quit)
        button1.place(x=100,y=406,width=200,height=44)
        button3.place(x=300,y=406,width=200,height=44)

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

我想从StartPage获取条目文本数据,并在PageOne中将其显示为标签。怎么做?我是新来的。请输入代码。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

首先,更正类 PageOne StartPage 的代码,并将 self.controller = controller 添加到__init__函数:

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        #your code
        self.controler = controller

在StartPage中的输入字段之前和PageOne中的标签之前添加自身:

#entry in StartPage
self.emp=tk.Entry(frame1, bd =5,font=("Times New Roman", 14),width=25)
self.emp.pack(side=tk.LEFT)

#label in PageOne
self.label = tk.Label(self, text="This is page one", font=TITLE_FONT)
self.label.place(x=0,y=0,width=592,height=44)

然后将函数go_to_page_one添加到StartPage类:

def go_to_page_one(self):
    self.controller.SomeVar = self.emp.get() #save text from entry to some var
    self.controller.frames[PageOne].correct_label() #call correct_label function
    self.controller.show_frame(PageOne) #show page one

在StartPage类中的button1上将命令更改为lambda:self.go_to_page_one():

button1 = tk.Button(self, text="Go to Page One",
                    command=lambda: self.go_to_page_one())

最后在类PageOne:

中添加一个函数正确的标签
def correct_label(self):
    self.label.config(text=self.controller.SomeVar) #correct the label