TuanNN
TuanNN

Reputation: 105

when will Text widget update after insert a text

I create a simple GUI which contain a button and a text widget (TextArea). My business is when I click the button, the text widget will be insert some text.

At my attached image, After the GUI appeared, I clicked the button and I'm in breakpoint at line 9, my expectation is the text widget have 2 lines: text1, text2

However, nothing is showed until the function callback is finished

The debug status

from tkinter import *

master = Tk()

def callback(text: Text):
    text.delete('1.0', END)
    text.insert(END, 'text1\r\n')
    text.insert(END, 'text2\r\n')
    text.insert(END, 'text3\r\n')
    text.insert(END, 'text4\r\n')
    text.insert(END, 'text5\r\n')
    text.insert(END, 'text6\r\n')
    text.insert(END, 'text7\r\n')

textwidget = Text(master)
textwidget .pack()
b = Button(master, text="OK", command=lambda :callback(textwidget))
b.pack()

mainloop()

My question is How can I force the gui update immediately after execute insert method of textwidget.

Update

Thank you for @Saad recommendation, I update the code (insert text.update() at line 9) and I can see the text appear in text widget

def callback(text: Text):
    text.delete('1.0', END)
    text.insert(END, 'text1\r\n')
    text.insert(END, 'text2\r\n')
    text.update()
    text.insert(END, 'text3\r\n')
    text.insert(END, 'text4\r\n')
    text.insert(END, 'text5\r\n')
    text.insert(END, 'text6\r\n')
    text.insert(END, 'text7\r\n')

Upvotes: 2

Views: 609

Answers (1)

Saad
Saad

Reputation: 3430

Ok now I understand what exactly the problem is. As I can see in the picture that you've set a breakpoint at line 9 in your code . what that does is to pause the compile at the breakpoint so we can test different stuff like - errors, but this feature is not healthy for GUI at-least on what I've seen. So just remove that red breakpoint in your idle that should fix the issue.

Then you don't have to use text.update().

enter image description here

Upvotes: 2

Related Questions