Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

python - 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. So when the button named Next [in class Display load function] is clicked, it would open a new Tkinter window. And in the new window, the variable string1 from class Display needs to be retrieved for further action. May i know should i create another class Display2, or should i just add a method in the class Display?

Currently the string variable can be passed as reference from class Display to the class Action_Data. But how can it be passed to another Tkinter window when the button Next is clicked? I am trying to get the variable via the callback 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

enter image description here

Error

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use the lambda function to pass the button's command, that way you can pass the needed string as an argument.

You do not need to create two instances of Tk, if you need another window, create a 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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...