Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python


I have a Python code that describes two Tkinter windows.
I would like to be able to display data entered in the second window within the first window.
I mention that the data is entered in an Entry-type window in the second window and I would like it to be displayed in a Label-type Widget in the first window.
The code that generates my problem is the following:

from tkinter import *
def main():
    root = Tk ()
    application  = Window1 (root)
    
#=============================================================
class Window1:
     
    def __init__(self,master):
        self.master.title('Title')
        self.master.geometry('1350x750+0+0')
        self.frame = Frame(self.master)
        self.frame.pack()
        self.label = Label(self.frame, text = 'x')
        self.label.grid() 
        self.btn = Button (self.frame , text= ' Open Window 2' , command = self.window2) 
        self.btn.grid()
    
    def window2 (self):
         self.window2 = Toplevel (self.master)
         self.application = window2(self.window2)

class window2():
    def __init__ (self,master):
        
        self.name= StringVar()
        self.master=master
        self.master.title('Window2')
        self.master.geometry('1350x750')
        self.framew2 = Frame(self.master)
        self.framew2.pack()
        self.name = Entry(self.framew2, textvariable = self.name)
        self.name.grid()

        
    
if __name__ == '__main__':
   main()


What I have tried:

For this purpose, I tried the following:
1. I tried to pass the variable from the Window2 class to Window1, which turned out to be ineffective because there was no instance of Window2 at that time.
2. Transmission of data from the Stringvar() type variable to the Label Widget within the Window2 class. this aspect involved calling the associated variable of the Window1 class. I tried this using the Window1.label command, but I found that the self.label variable is an instance variable of the Window1 class.
Therefore, it must be called with the help of a court.
Using application as an instance of the Window1 class didn't have the intended effect, so I thought I could create an instance of the class within the Window2 class. This had the effect of an error specifying the lack of the master argument in Window1. What value can this argument have because it defines a value related to a tkinter window?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900