0

I have created a textbox in a tkinter window which is called by a button. I want to get the value of textbox in another window on clicking a button. But when i print the value on console, it prints an empty string instead of the value entered in textbox.

from tkinter import *

def click():
    print(id.get())

def submit():
    win2=Tk()
    Label1=Label(win2,text='Id').pack()
    global id
    id=StringVar()
    textbox=Entry(win2,textvariable=id).pack()
    btn2=Button(win2,text='Click',command=click).pack()

win1=Tk()
btn1=Button(win1,text='Submit',command=submit).pack()

Please check my code.

3 Answers 3

1

You might want to try something like this:

import tkinter as tk


class MainPage(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.btn1 = tk.Button(self, text='Submit', command=self.submit)
        self.btn1.pack()

    def submit(self):
        self.win2 = tk.Tk()
        self.Label1 = tk.Label(self.win2, text='Id')
        self.Label1.pack()
        self.id = tk.StringVar()
        self.textbox = tk.Entry(self.win2, textvariable=self.id)
        self.textbox.pack()
        self.btn2 = tk.Button(self.win2, text='Click', command=self.click)
        self.btn2.pack()

    def click(self):
        print(self.textbox.get())

interface = MainPage()
interface.mainloop()

Edit: The main difference with the original code is that the print calls the Entry widget, rather than the StringVar. If you would like to set the StringVar to the text inserted in the textbox, then you would have to call at some point

self.id.set(self.textbox.get())

then the self.id.get() would return what you expect.

Sign up to request clarification or add additional context in comments.

1 Comment

This answer would be better with an explanation. Otherwise we have to compare your code to the code in the question line by line and character by character to see what you've done differently.
1

From previous answer by @neko,i got an answer similar to my code:

from tkinter import *

def click():
    print(textbox.get())

def submit():
    win2=Tk()
    Label1=Label(win2,text='Id').pack()
    global textbox
    id=StringVar()
    textbox=Entry(win2,textvariable=id)
    textbox.pack()
    btn2=Button(win2,text='Click',command=click).pack()


win1=Tk()
btn1=Button(win1,text='Submit',command=submit).pack()

The problem was that i was using id.get(), which is not working in multiple tkinter window. So now i used textbox.get() which is working fine here.

Comments

-1

Have you tried to do this? :

print(id.get("1.0",'end-1c'))

The '1.0' part tells the method to start reading at the first line (starting with the first character, because of the 0). The 'end-1c' argument tells the method to read until it reaches the end of the text. Note that 1c removes 1 character from the text, because if you used just 'end', it would add a new line character at the end of your text.

1 Comment

It is showing error after using print(id.get("1.0",'end-1c')) as TypeError: get() takes 1 positional argument but 3 were given

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.