python 3.5将对象从一个类传递到另一个类

时间:2016-03-20 22:56:38

标签: python

我试图弄清楚如何将数据从一个类传递到另一个类。我对python的了解非常有限,我使用的代码来自本网站的示例。

我正在尝试从" UserNamePage"中传递用户名进入"欢迎页面"类。有人可以告诉我如何实现这一目标。我将添加更多页面,我需要在不同页面之间传递数据

以下是完整代码 - 如上所述,此代码的大部分来自其他示例,我正在使用这些示例来学习。

import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import datetime 
import re

def Chk_String(mystring):
    Allowed_Chars = re.compile('[a-zA-Z_-]+$')
    return Allowed_Chars.match(mystring)


def FnChkLogin(Page):

    booAllFieldsCorrect = False;

    myFName = Page.FName.get()
    myFName = myFName.replace(" ", "")

    myLName = Page.LName.get()
    myLName = myLName.replace(" ", "")


    if myFName == "":
        messagebox.showinfo('Login Ifo is Missing', "Please type in your First Name") 
    elif not Chk_String(myFName): 
        messagebox.showinfo('First Name Error:', "Please only use Leter or - or _")
    elif myLName == "":
        messagebox.showinfo('Login Info is Missing', "Please type in your Last Name")
    elif not Chk_String(myLName): 
        messagebox.showinfo('Last Name Error:', "Please only use Leter or - or _")  
    else:

        booAllFieldsCorrect = True;

        if  booAllFieldsCorrect == True:
            app.geometry("400x200") 
            app.title("Welcome Screen")         
            PageController.show_frame(app,"WelcomePage")

def FnAddButton(Page,Name,Label,Width,X,Y,FnAction):
    Name = ttk.Button (Page, text=Label,width=int(Width),command=FnAction)
    Name.place(x=int(X),y=int(Y))   



class PageController(tk.Tk):    
    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 (UserNamePage,WelcomePage):
            page_name = F.__name__
            frame = F(container,self)       
            self.frames[page_name] = frame
            frame.grid(row=0,column=0,sticky="nsew")

        self.show_frame("UserNamePage")

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



class UserNamePage(tk.Frame):


    def __init__(self,parent,controller):

        tk.Frame.__init__(self,parent)

        self.controller = controller

        lblFName = Label(self,text="First Name  ",relief=GROOVE,width=12,anchor=E).place(x=50,y=50)
        lblLName = Label(self,text="Last Name  ",relief=GROOVE,width=12,anchor=E).place(x=50,y=75)

        self.FName = StringVar()
        inputFName = Entry(self,textvariable=self.FName,width=25).place(x=142,y=50)
        self.LName = StringVar()
        inputLName = Entry(self,textvariable=self.LName,width=25).place(x=142,y=75)

        cmdContinue = ttk.Button (self, text='Continue',width=9,command=lambda:FnChkLogin(self)).place(x=320,y=70)  


class WelcomePage(tk.Frame):

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

        UserNamePageData = UserNamePage(parent,controller)
        UserFName = str(UserNamePageData.FName)
        UserLName = str(UserNamePageData.LName)

        strWelcome = "Welcome " +  UserFName + " " + UserLName

        lblWelcome = Label(self,text=strWelcome,relief=FLAT,width=50,anchor=W).place(x=25,y=25) 


if __name__ == "__main__":
    app = PageController()
    app.geometry("400x200") 
    app.title("User  Page")
    app.eval('tk::PlaceWindow %s center' % app.winfo_pathname(app.winfo_id()))
    app.mainloop()

1 个答案:

答案 0 :(得分:0)

在课程show_frame(self,page_name)的方法PageController中,添加以下行

if page_name == 'WelcomePage':
  self.frames['WelcomePage'].UserFName = self.frames['UserNamePage'].FName.get()
  self.frames['WelcomePage'].UserLName = self.frames['UserNamePage'].LName.get()

并删除两行UserFName = str(UserNamePageData.FName)UserLName = str(UserNamePageData.LName)

说明:必须在引用两个帧的地方(即类PageController)完成。