[英]How to get variable from a class to another Tkinter window
The goal is to pass the variable string1 from class Display
to be used in another Tkinter window.目标是从
class Display
传递变量string1以用于另一个 Tkinter window。 So when the button named Next [in class Display
load function] is clicked, it would open a new Tkinter window.因此,当单击名为Next [在
class Display
加载功能中] 的按钮时,它将打开一个新的 Tkinter window。 And in the new window, the variable string1 from class Display
needs to be retrieved for further action.在新的 window 中,需要检索来自
class Display
的变量string1以进行进一步操作。 May i know should i create another class Display2, or should i just add a method in the class Display?我可以知道我应该创建另一个 class Display2,还是应该在 class 显示器中添加一个方法?
Currently the string variable can be passed as reference from class Display
to the class Action_Data
.目前,字符串变量可以作为参考从
class Display
传递到class Action_Data
。 But how can it be passed to another Tkinter window when the button Next is clicked?但是当单击下一步按钮时,如何将其传递给另一个 Tkinter window 呢? I am trying to get the variable via the callback function new_window.
我正在尝试通过回调 function new_window 获取变量。 Just not sure if it's how it's done.
只是不确定它是否是如何完成的。 Any pointer would be appreciated.
任何指针将不胜感激。 Many thanks.
非常感谢。
from tkinter import *
import tkinter as tk
#Application window
root = tk.Tk()
#Display Class
class Display (tk.Frame):
def __init__(self, master, display_data):
tk.Frame.__init__(self,master)
self.master = master
#passing data as reference
self.display= display_data
#button
self.load_button = tk.Button(self, text="Load", command=self.load)
self.load_button.pack()
def new_window(self):
self.master = tk.Tk() # create another Tk instance
var_string2 = Label(self, text="<<string1 value>>")
var_string2.pack()
print (var_string2)
def load(self):
#get value
string1='value1'
self.display.action1(string1)
self.acition_button = tk.Button(self, text="Next",
command=self.new_window)
self.acition_button.pack()
#Action_Data Class
class Action_Data(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self,master)
def action1(self, path1):
var_path1 = path1
print(var_path1)
display= Action_Data(root)
display.pack()
reader = Display(root, display)
reader.pack()
pathlabel2 = Label(root)
root.mainloop()
Issue
Now the new window is blank and cannot retrieve the value of variable string1 from the load function现在新的 window 为空,无法从加载 function 中检索变量 string1 的值
Error错误
Use the lambda
function to pass the button's command
, that way you can pass the needed string as an argument.使用
lambda
function 传递按钮的command
,这样您就可以将所需的字符串作为参数传递。
You do not need to create two instances of Tk
, if you need another window, create a Toplevel
.您不需要创建两个
Tk
实例,如果您需要另一个 window,请创建一个Toplevel
。
from tkinter import *
import tkinter as tk
#Application window
root = tk.Tk()
#Display Class
class Display (tk.Frame):
def __init__(self, master, display_data):
tk.Frame.__init__(self,master)
self.master = master
#passing data as reference
self.display= display_data
#button
self.load_button = tk.Button(self, text="Load", command=self.load)
self.load_button.pack()
def new_window(self, string):
#self.master.destroy() # close the current window
new_window = tk.Toplevel() # create toplevel
var_string2 = Label(new_window, text=string)
var_string2.pack()
new_window.focus()
print (var_string2.cget("text"))
def load(self):
#get value
string1='value1'
self.display.action1(string1)
self.acition_button = tk.Button(self, text="Next",
command= lambda : self.new_window(string1))
self.acition_button.pack()
#Action_Data Class
class Action_Data(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self,master)
def action1(self, path1):
var_path1 = path1
print(var_path1)
display= Action_Data(root)
display.pack()
reader = Display(root, display)
reader.pack()
pathlabel2 = Label(root)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.