0
import tkinter as tk
win = tk.Tk()
win.title('New App')
win.geometry('800x800')


def passgen():
    num = str(inp.get())
# text field for output
    disp = tk.Text(master=win, height=4, width=80, )
    disp.pack()
    disp.insert(tk.END, num)


lab = tk.Label(text='First Label')
lab.pack()
inp = tk.Entry()
inp.pack()
btn = tk.Button(text='Submit', command=passgen)
btn.pack()


win.mainloop()

Above is my simple tkinter code but when I run it I get the output in multiple boxes. All I want is each time I use the submit button the output should be in one single box rather than multiple boxes. Is there any way to do it?I am using python 3. Screenshot

1 Answer 1

1

the issue is in the way that the passgen() method works where it creates a new tk.Text() object. to fix this you want to add to the same Text object which means creating it outside of the function and then using the global object from the function:

...

def passgen():
    global disp
    num = str(inp.get())
    disp.insert(tk.END, num)

disp = tk.Text(master=win, height=4, width=80, )
disp.pack()
...
Sign up to request clarification or add additional context in comments.

Comments

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.