【问题标题】:how do i get information from 1 class to another我如何从一个班级获取信息到另一个班级
【发布时间】:2021-11-18 12:16:24
【问题描述】:

python新手-kivy-gui

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

在 .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

我不知道如何让它在新课程中打印出来。

【问题讨论】:

    标签: python class user-interface kivy return


    【解决方案1】:

    您需要的是global 变量。你知道范围吗?简而言之,全局变量是一种可以从代码文件中的任何位置访问/修改的变量。这是一个例子:

    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 
    

    【讨论】:

    • 您好,Khushal Jangid 先生。非常感谢您花时间回答我的问题。你帮了我很大的忙!我是一位经验丰富的(业余)编码人员,但我仍在学习 Python 的语法,我没有意识到你可以做到这一点。更多爱 python 的理由!谢谢先生。
    • 这是我的荣幸,先生 :)
    • 嘿,@Chandradeobalram 请接受这个作为解决方案!
    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多