2

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')
TuanNN
  • 95
  • 10
  • 1
    _I insert some text in TEXT widget but the text did not display immediately until the mainloop() execute_. The window won't even appear properly before the `mainloop` then how can you able to see the text, I'm bit confused based on your post. If you are not using `update()` or `update_idletasks()` in your main code. – Saad Apr 24 '19 at 15:19
  • Sorry for my confuse question. I mean that after the UI appear and I clicked the button. As your recommendation, I have to call update() method to display the text which I expect. – TuanNN Apr 25 '19 at 12:39
  • That shouldn't happen if this is the exact code. Or you want to have text display without the press of a button? – Saad Apr 25 '19 at 12:44
  • At the code in my question, when I click button, I can see that the text is displayed in the textwidget, but when I debug at line text.insert(END, 'text3\r\n') as I mentioned, the textWidget is empty. So I update add text.update() after text.insert(END, 'text2\r\n'). And I can see the texts when I create breakpoint at line text.insert(END, 'text3\r\n'). Anyway, Thank you very much. – TuanNN Apr 25 '19 at 12:50
  • Please provide the non updated code. You can add information to your post by editing the post – Saad Apr 25 '19 at 12:56
  • I updated the question. Hope that it clearly enough for my issue. Thank you. – TuanNN Apr 25 '19 at 13:35

1 Answers1

2

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

Saad
  • 3,340
  • 2
  • 10
  • 32
  • I just want to understand how the text widget update the GUI after execute the insert command. For example, when I clicked 2nd time, At breakpoint at line 9, the text widget still have 7 lines text because the text widget GUI did not update after execute command text.delete('1.0', END). Anyway, thank you for your answer. – TuanNN Apr 26 '19 at 02:32
  • I see, do one thing if you wanna see how things update in `text` widget just to see then try `after()` with some delay and you'll notice and can see how one by one line is getting added, it also depends on the delay time you put I think 400- 600 ms ( 0.4 - 0.6 sec ) should be good to notice a difference. In case you don't know how to use `after()` here is [how to use `after()` function in tkinter.](https://stackoverflow.com/questions/25753632/tkinter-how-to-use-after-method). Also if this helps you consider accepting the answer. – Saad Apr 26 '19 at 04:17