简体   繁体   English

如何从 class 到另一个 Tkinter window 获取变量

[英]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.

相关问题 Tkinter:如何将变量从一个 class(窗口)传递到另一个 class(窗口)-(不是顶层框架)? - Tkinter: how to pass variable from one class (window) to another class (window) - (not toplevel frame)? 使用.get()从另一个类中提取Tkinter变量 - Using .get() to pull a Tkinter variable from another class TKinter:在 Python class 中获取变量并将其发送给另一个 - TKinter : get variable in Python class and send it to another 如何从同一类中的另一个方法访问方法变量(Tkinter)? - How to access a method variable (Tkinter) from another method in same class? 从另一个类将变量插入Tkinter列表框 - Inserting a Variable into Tkinter Listbox from Another Class 使用从一个 class 到另一个 tkinter 的变量 - Using a variable from one class to another tkinter 如何从 tkinter 输入框获取输入到另一个 function 中的变量? - How to get input from a tkinter entry box, to a variable in another function? 另一个类中的tkinter变量 - tkinter variable in another class Tkinter - 如何将实例变量传递给另一个类? - Tkinter - How to pass instance variable to another class? 如何将变量值从一个文件中的一个类传递到另一个文件中的另一个类 python tkinter - How to pass variable value from one class in one file to another class in another file python tkinter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM