Python 如何将变量值从一个文件中的一个类传递到另一个文件中的另一个类

Python 如何将变量值从一个文件中的一个类传递到另一个文件中的另一个类,python,python-3.x,tkinter,python-class,Python,Python 3.x,Tkinter,Python Class,我是python新手。在Python3.7和windows操作系统上工作。假设我创建了一个名为 Class1.py其中 import tkinter as tk import Class2 class main_window: def openanotherwin(): Class2.this.now() def create(): root = tk.Tk() button1 = tk.Button(root, text=&quo

我是python新手。在Python3.7和windows操作系统上工作。假设我创建了一个名为 Class1.py其中

import tkinter as tk
import Class2
class main_window:
    def openanotherwin():
        Class2.this.now()
    def create():
        root = tk.Tk()
        button1 = tk.Button(root, text="Open another window", command = openanotherwin )
        button1.pack()
        root.mainloop()
现在我的Class2.py包含:

import tkinter as tk
class this():
    def now():
        new = tk.Toplevel(root)  #Error displayed: root is not defined
        lb = tk.Label(new, text = "Hello")
        lb.pack()
        new.mainloop()
import Class1
Class1.main_window.create()
我的Main.py包含:

import tkinter as tk
class this():
    def now():
        new = tk.Toplevel(root)  #Error displayed: root is not defined
        lb = tk.Label(new, text = "Hello")
        lb.pack()
        new.mainloop()
import Class1
Class1.main_window.create()
显示的错误为:
根未在Class2.py中定义。我尝试了
root=Class1.main\u window.root
来获取root的值,但它显示了一个错误,即函数没有属性root


请帮我解决我的问题。

首先,类2中类的名称“this”可能有错误。 我猜“this”是当前对象实例的保留名称。 你应该把它改成别的东西,例如“class2”


然后,您应该在class1中实例化class2,并将root作为参数传递给构造函数。只有这样,才能在类2中使用root。

我认为函数需要获得root

 def now(root): 
    new = tk.Toplevel(root)  #Error displayed: root is not defined
然后在类别1中:

def openanotherwin(root):
    Class2.this.now(root)
第三:

button1 = tk.Button(root, text="Open another window", command=lambda: main_window.openanotherwin(root) )
===

Class1.py

import tkinter as tk
import Class2
class main_window:
def openanotherwin(root):
    Class2.this.now(root)
    def create():
        root = tk.Tk()
button1 = tk.Button(root, text="Open another window", command=lambda: main_window.openanotherwin(root) )
        button1.pack()
        root.mainloop()
import tkinter as tk
class this():
def now(root): 
    new = tk.Toplevel(root)  #Error displayed: root is not defined
        lb = tk.Label(new, text = "Hello")
        lb.pack()
        new.mainloop()
Class2.py

import tkinter as tk
import Class2
class main_window:
def openanotherwin(root):
    Class2.this.now(root)
    def create():
        root = tk.Tk()
button1 = tk.Button(root, text="Open another window", command=lambda: main_window.openanotherwin(root) )
        button1.pack()
        root.mainloop()
import tkinter as tk
class this():
def now(root): 
    new = tk.Toplevel(root)  #Error displayed: root is not defined
        lb = tk.Label(new, text = "Hello")
        lb.pack()
        new.mainloop()

下面是一个向类构造函数传递参数的示例:

class DemoClass:
    num = 101

    # parameterized constructor
    def __init__(self, data):
        self.num = data

    # a method
    def read_number(self):
        print(self.num)


# creating object of the class
# this will invoke parameterized constructor
obj = DemoClass(55)

# calling the instance method using the object obj
obj.read_number()

# creating another object of the class
obj2 = DemoClass(66)

# calling the instance method using the object obj
obj2.read_number()

请用完整的错误回溯更新您的问题。这是否回答了您的问题?不,
这个
在python中没有保留,奇怪的是,
self也没有保留。名称“this”现在只是用作参考,实际的代码有一个不同的名称,它没有保留。请您通过编写代码来解释如何将root作为参数传递给构造函数,因为我对它还不熟悉,正在学习。谢谢你的建议。好的,这是问题的一部分。下一步是将
root
传递到对
now()
的每个调用中。您的第三个代码块无效。应该是
command=lambda:main\u window.openanotherwin(root)