繁体   English   中英

将变量传递给不同文件上的不同类

[英]Passing variables to different classes on different files

我正在尝试创建需要将变量从函数传递到类的软件,如下所示:

def goToAddingAClass(self):
    className = self.CreateNewClassEntry.get()
    root2 = Toplevel(self.master)
    NextWindow = AddingAClassPage.AddingAClass(root2)

...

from HomePage import *

class InputtingStudentInformation():
    def __init__(self, master):
        titleLabel(master, 'Adding a Class ' + className, labelBackground())

为了简单起见,我删除了很多代码。 第一个文件称为HomePage,第二个文件称为InputtingStudentInformationPage。 但是,我不确定如何将变量“ className”放入第二类。 我尝试如图所示从首页导入所有内容,但出现错误提示

global name 'className' is not defined

如何在不同文件的类之间传递变量?

将信息从一个类传递到另一个类的最佳方法是将其传递。通常,它采用构造函数中另一个参数的形式:

class InputtingStudentInformation():
    def __init__(self, master, className):
        titleLabel(master, 'Adding a Class ' + className, labelBackground())
...
className = self.CreateNewClassEntry.get()
something = InputtingStudentInformation(master, className)

暂无
暂无

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

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