[英]Passing class values to another class using Tkinter in Python 3.5
I have the following code (example of my real program): 我有以下代码(我的真实程序示例):
from tkinter import *
def class1(Frame)
def nv(self,x):
self.vent=Toplevel(self.master)
self.app=class2(self.vent)
self.value=x
def __init__(self,master):
super().__init__(master)
self.master=master
self.frame=Frame(self.master)
self.btn=Button(self, text="example", command=lambda: self.nw(1))
self.btn.pack()
self.pack()
def class2(Frame):
def __init__(self, master):
super().__init__(master)
self.master=master
self.frame=Frame(self.master)
self.value=class1.nw.value.get()
root= Tk()
marco=Frame(root)
marco.pack
lf=class1(marco)
root.mainloop()
this last part is the problem, i cant use .get() properly for this problem, I want to get the value of x when I create the new window. 这最后一部分是问题,我无法正确使用.get()解决此问题,我想在创建新窗口时获取x的值。 I use lambda so i can execute the command with parameters.
我使用lambda,以便可以执行带参数的命令。 So the question is, is there a way for me to access the value of x in class 2?
所以问题是,我是否可以访问类2中的x值?
You appear to be quite confused about the usage of classes when using tkinter
. 使用
tkinter
时,您似乎对类的用法感到很困惑。 super()
should not be used with tkinter as explained here and when declaring a class you should use the class
keyword, not def
. super()
不应该与Tkinter的被用作解释这里和声明一个类时,你应该使用class
的关键字,而不是def
。 .get()
is a method of a tkinter
variable class such as tkinter.IntVar
, tkinter.StringVar
, etc. so is not needed in the example you have given. .get()
是一个的方法tkinter
变量类如tkinter.IntVar
, tkinter.StringVar
等所以不需要在已给出的例子。
What you need is the function in the Frame
you are trying to get the value of x from ( nv
) and then parse that value to the __init__
method in the child Frame
. 您需要的是
Frame
的函数,您正试图从( nv
)获取x的值,然后将该值解析为子Frame
的__init__
方法。
Here is my solution: 这是我的解决方案:
from tkinter import *
class Class1(Frame):
def nv(self,x):
self.vent = Toplevel(self.master)
self.app = Class2(self.vent,x)
def __init__(self,master):
Frame.__init__(self,master)
self.master = master
self.btn = Button(self, text="example", command=lambda: self.nv(1))
self.btn.pack()
self.pack()
class Class2(Frame):
def __init__(self, master, x):
Frame.__init__(self,master)
self.master=master
self.frame=Frame(self.master)
self.x_text = Label(self, text=str(x))
self.x_text.pack()
self.pack()
root = Tk()
marco = Frame(root)
marco.pack()
lf = Class1(marco)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.