如何将变量值从一个文件中的一个 class 传递到另一个文件中的另一个 class python tkinter
How to pass variable value from one class in one file to another class in another file python tkinter
我是 python 的新手。致力于 python 3.7,windows os。 suppose 我已经创建了一个名为
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()
我的Main.py包含:
import Class1
Class1.main_window.create()
显示的错误是:root is not defined in Class2.py
。我已尝试 root = Class1.main_window.root
引入 root 的值,但它显示函数没有属性 root 的错误。
请帮我解决问题
首先,Class2 中您的 class 的名称“this”可能存在错误。
我猜“this”是当前对象实例的保留名称。
您应该将其更改为其他内容,例如"class2"
然后您应该在 class1 中实例化 class2 并将 root 作为参数传递给构造函数。只有这样,你才能在class2.
中使用root
我认为函数需要获取 root
def now(root):
new = tk.Toplevel(root) #Error displayed: root is not defined
然后在 class1:
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()
Class2.py
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 构造函数的示例:
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 的新手。致力于 python 3.7,windows os。 suppose 我已经创建了一个名为 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()
我的Main.py包含:
import Class1
Class1.main_window.create()
显示的错误是:root is not defined in Class2.py
。我已尝试 root = Class1.main_window.root
引入 root 的值,但它显示函数没有属性 root 的错误。
请帮我解决问题
首先,Class2 中您的 class 的名称“this”可能存在错误。 我猜“this”是当前对象实例的保留名称。 您应该将其更改为其他内容,例如"class2"
然后您应该在 class1 中实例化 class2 并将 root 作为参数传递给构造函数。只有这样,你才能在class2.
中使用root我认为函数需要获取 root
def now(root):
new = tk.Toplevel(root) #Error displayed: root is not defined
然后在 class1:
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()
Class2.py
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 构造函数的示例:
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()