Click here to Skip to main content
15,946,158 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)

class first:
    def __init__(self, root):
        self.root = root
        self.root.title('First window')

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

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

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

    def second_window(self):
        main_window = Tk()
        app = second(main_window)

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

class second:
    def __init__(self, root):
        self.root = root
        self.root.title('Second window')
        id = first.return_id

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

if __name__ == '__main__':
Updated 5-May-21 0:11am

1 solution

Please, read this: 9. Classes — Python 3.9.5 documentation[^]
There you'll find an answer to your question.
Share this answer
CPallini 5-May-21 7:09am    
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