Click here to Skip to main content
15,123,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two tkinter classes. I am entering a value in an entry box and trying to show it in the label in the other class. I have tried it many ways but not able to do this. Please if anyone can help me in doing so. I have wasted my two days trying to do it!

What I have tried:

import tkinter
from tkinter import Tk, Toplevel
from tkinter import *


def main():
    main_window = Tk()
    app = first(main_window)
    main_window.mainloop()


class first:
    def __init__(self, root):
        self.root = root
        self.root.title('First window')
        self.root.geometry('1350x700+0+0')

        single_id = Label(self.root, text="Enter id", font=("Times New Roman", 14), bg='white',
                          fg='black')
        single_id.place(x=200, y=200)

        self.mystring = tkinter.StringVar(self.root)

        self.txt_id = Entry(self.root, textvariable=self.mystring, font=("Times New Roman", 14), bg='white')
        self.txt_id.place(x=300, y=200, width=280)
        btn_search = Button(self.root, command=self.second_window, font=("Times New Roman", 15, 'bold'), text='Get Id')
        btn_search.place(x=300, y=400, width=220, height=35)

    def second_window(self):
        self.root.destroy()
        main_window = Tk()
        app = second(main_window)
        main_window.mainloop()

    def return_id(self):
        return self.mystring.get()


class second:
    def __init__(self, root):
        self.root = root
        self.root.title('Second window')
        self.root.geometry('1350x700+0+0')
        id = first.return_id

        get_id = Label(self.root, text=id, font=("Times New Roman", 14), bg='white',
                       fg='black')
        get_id.place(x=200, y=350)


if __name__ == '__main__':
    main()
Posted
Updated 5-May-21 1:11am

1 solution

Please, read this: 9. Classes — Python 3.9.5 documentation[^]
There you'll find an answer to your question.
   
Comments
CPallini 5-May-21 7:09am
   
5.
Maciej Los 5-May-21 8:08am
   
Thank you, Carlo.

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