2

I have just started learning tkinter and came across a problem.I have two tkinter classes. I am entering a value in an entry field of one tkinter class 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. Here is my code.

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()

The way I am doing it is not showing the actual value. Instead it gives 2064283946496return_id

Any help would be greatly appreciated.

Anmol
  • 37
  • 3
  • `id = first.return_id` will assign the function reference of `first.return_id` to `id`. That is why you get the result. In order to get the value input, better pass the value directly to `second` class when creating it. – acw1668 May 03 '21 at 09:18
  • Try adding `user_input = self.txt_id.get()` before `self.root.destroy()` and then passing that input to the second class as an argument. Right now you are trying to access `self.mystring` after you destroy its master. The `self.root.destroy()` destroys `self.mystring` and `self.txt_id` – TheLizzard May 03 '21 at 09:19
  • 2
    Well usually we instantiate the class and then use its method. Here I think you can create a parameter with `def __init__(self, root, id)` of `second` and then `id = self.txt_id.get()` before `self.root.destroy()` and then `app = second(main_window,id)` – Cool Cloud May 03 '21 at 09:56

1 Answers1

2

What you can do is pass the first class object as an argument to the second class intializer and then call the method on it. Something like this seems to work -:

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, self)
        main_window.mainloop()

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


class second:
    def __init__(self, root, first):
        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()