保存多个类的数据

时间:2018-01-10 06:34:48

标签: python-3.x class user-interface tkinter

我已经环顾了很多,我已经研究了很多可比较的问题和类型的guis但不幸的是我仍然无法解决我的问题。目前我正在制作一个有多页的Tkinter gui。人们必须插入某些属性,最后我想在最后一页打印这些属性。因此,我必须访问不同类的条目值。但是,不知怎的,我无法做到这一点。有人可以帮我吗?

import tkinter as tk
class Main(tk.Tk):

    def get_page(self, page_class):
        return self.frames[page_class]

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)




        container = tk.Frame(self)


        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):

            frame = F(container, self)


            self.frames[F] = frame
            self.geometry("700x400")
            self.wm_title('Size')
            self["bg"]="white"

            frame.grid(row=0, column=0, sticky="nsew")



        self.show_frame(StartPage)
    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()        


    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()        


import tkinter as tk

class StartPage(tk.Frame):



    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller=controller 
        self["bg"]="white"

        label1=tk.Label(self, text="What are the sizes?",bg="white",fg="green",font="verdana 10")
        label1.pack()
        L1 = tk.Label(self, text="Length :",fg="green",font="verdana 8",bg="white")
        L1.pack()
        L1.place(x=220,y = 50)

        self.E1 = tk.Entry(self, bd =5)
        self.E1.pack()
        self.E1.place(x=370,y=50)



        button = tk.Button(self, text="Next",width=25,height=2, command=lambda: controller.show_frame(PageOne))
        button.pack()
        button.place(x=470,y = 180)
        button.config(bg="green",fg='white')


class PageOne(tk.Frame):



    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller=controller
        self["bg"]="white"

        page1 = self.controller.get_page(StartPage) 
        var = page1.E1.get()
        label1=tk.Label(self, text="You have insert",bg="white",fg="green",font="verdana 10")
        label1.pack()
        label2=tk.Label(self, text=var,bg="white",fg="green",font="verdana 10")
        label2.pack()





app = Main()
app.mainloop()

1 个答案:

答案 0 :(得分:0)

问题是因为PageOne.__init__在您看到StartPage之前在开始时执行,因此当您使用Entry

var = page1.E1.get()仍为空

您可以将方法update_page()添加到每个页面,并在更改页面时执行它(当您单击按钮时)。这样update_page可以在var = page1.E1.get()

中放入文字后使用Entry来获取价值

我添加了按钮"Back",因此您可以返回StartPage并更改Entry中的值,当您转到PageOne时,您会看到新值。

import tkinter as tk

class Main(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.geometry("700x400")

        container = tk.Frame(self)

        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):

            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()        
        frame.update_page() # <-- update data on page when you click button

    def get_page(self, page_class):
        return self.frames[page_class]


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller 

        label1 = tk.Label(self, text="What are the sizes?")
        label1.pack()

        L1 = tk.Label(self, text="Length :")
        L1.pack()

        self.E1 = tk.Entry(self)
        self.E1.pack()

        button = tk.Button(self, text="Next", command=lambda: controller.show_frame(PageOne))
        button.pack()

    def update_page(self): # empty method but I need it
        pass   

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        label1 = tk.Label(self, text="You have insert")
        label1.pack()

        # create empty label at start
        self.label2 = tk.Label(self, text="")
        self.label2.pack()

        button = tk.Button(self, text="Back", command=lambda: controller.show_frame(StartPage))
        button.pack()

    def update_page(self):
        # update label when page is changed
        page1 = self.controller.get_page(StartPage) 
        var = page1.E1.get()
        self.label2['text'] = var


app = Main()
app.mainloop()