Ruturaj
Ruturaj

Reputation: 3

Output text in multiple text-boxes rather than single..tkinter python

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

Upvotes: 0

Views: 909

Answers (1)

Tresdon
Tresdon

Reputation: 1211

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

Upvotes: 1

Related Questions