我如何从 1 class 获取信息到另一个

how do i get information from 1 class to another

新 python - kivy - gui

我试图从 1 class 获取信息到另一个,classes 基本上是我的 GUI 的不同屏幕。我研究了 return 函数,但它根本没有帮助,因为我是一个菜鸟。

.kv 文件上的主 GUI 运行 这是我的代码的细分。

PROJECT_PATH = ""

class TrainNew1(Screen):
    #takes user input,i click a button to submit, runs this function.
    def test(self):
        PROJECT_PATH = self.ids.ProjectName.text 
        #will print PROJECT_PATH fine within test /class function
class TrainNew2(Screen):

    print(PROJECT_PATH) # will not print

我不确定如何在新 class 中打印它。

您需要的是 global 个变量。你知道作用域吗?简而言之,全局变量是一种可以从代码文件中的任何位置 accessed/Modified 的变量。这是一个例子:

PROJECT_PATH = ""

class TrainNew1(Screen):
    global PROJECT_PATH # this is required to modify the original PROJECT_PATH
    #takes user input,i click a button to submit, runs this function.
    def test(self):
        PROJECT_PATH = self.ids.ProjectName.text 
        #will print PROJECT_PATH fine within test /class function
class TrainNew2(Screen):
    global PROJECT_PATH # this is used to access PROJECT_PATH 
    print(PROJECT_PATH) # Now, It can be used/modified even inside in this class