简体   繁体   English

我试图将一个变量从一个 python 文件传递​​到另一个 python 文件

[英]i am trying to pass one variable from one python file to another python file

sample_box.py sample_box.py

gr=0

class SampBoxLayout(BoxLayout):

    def input_rpm(self, instance, value):
        if value is True:
            global gr
            gr=22

pyth.py pyth.py

from sample_box import gr

while(True):
    if(gr== '22'):

Try creating a constant instead of a normal variable:尝试创建一个常量而不是普通变量:

GR = 0

and then import it:然后导入它:

import sample_box

To get the constant, use the syntax:要获取常量,请使用以下语法:

sample_box.GR

Add this to your code to prevent the import statement to run sample_box.py:将此添加到您的代码中以防止导入语句运行 sample_box.py:

GR = 0
if __name__ == "__main__":
    # all your code that you don't want to run here

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

相关问题 将变量从一个 python 文件传递到另一个 - Pass a variable from one python file to another 如何将变量值从一个文件中的一个类传递到另一个文件中的另一个类 python tkinter - How to pass variable value from one class in one file to another class in another file python tkinter Python:将数据从一个文件传递到另一个文件 - Python: Pass Data From One File to Another 将文件作为输入从一个 python 传递到另一个 - Pass a file as an input from one python to another 我可以将值从一个Python文件传递到另一个文件吗? - Can I pass values from one Python file to another? 如何在 Python 中将变量从一个生成器传递到另一个生成器? - How can I pass a variable from one generator to another in Python? 如何将变量从一个 Python 脚本传递到另一个? - How can I pass a variable from one Python Script to another? 如何将变量从一个 class 传递到 Python 中的另一个变量? - How can I pass a variable from one class to another in Python? 将变量从一个类传递到另一个Python - Pass variable from one class to another Python Python:将在一个文件中的一个类的方法中定义的变量传递给另一个文件 - Python: Pass variable defined in method of one class in one file to another seperate file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM