我有一个创建窗口的小模块(program1)。我已将其导入到我的另一个 python 程序(program2)中。
如何让程序 2 获取 self.x 和程序 1 中的 x?
这是程序1。
import Tkinter
class Class(Tkinter.Tk):
def __init__(self, parent):
Tkinter.Tk.__init__(self, parent)
self.parent = parent
self.Main()
def Main(self):
self.button= Tkinter.Button(self,text='hello')
self.button.pack()
self.x = 34
x = 62
def run():
app = Class(None)
app.mainloop()
if __name__ == "__main__":
run()
最佳答案
您可以将变量 self.x
作为 Class
实例的成员进行访问:
c = Class(parent)
print(c.x)
您无法访问局部变量 - 当方法调用结束时它会超出范围。
关于python - 在 Python 中导入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4240266/