0

When trying to disable Tkinter widgets immediately after a callback function has been invoked (by clicking a button for example), the widgets only disable after the for-loop in the function has been completed. Example code:

from tkinter import *
import time


def start_forloop():
    for x in range(10):
        time.sleep(1)


def disable_widget():
    button_start.configure(state='disabled')
    entry_test.configure(state='disabled')
    start_forloop()


# Window settings
app = Tk()
app.title('Test')

# Define widgets
button_start = Button(app, width=30, text='Start', command=disable_widget)
entry_test = Entry(app, width=30)

# Pack widgets
button_start.pack()
entry_test.pack()

# Start processing app
app.mainloop()

Is there a way such that the widgets disable before the for-loop ends?

woeterb
  • 161
  • 4
  • 2
    Try adding a `app.update_idletasks()` right before calling `start_forloop()` in the `disable_widget()` function. – martineau Mar 18 '19 at 00:11
  • First you have to understand [Event-driven programming](https://stackoverflow.com/a/9343402/7414759) **don't** use `time.sleep(x)` or long term running tasks within the `tkinter.mainloop()`. – stovfl Mar 18 '19 at 08:35
  • 1
    woeterb: The answer to preventing the freezing-up is somewhat complicated, so I suggest you ask another question...although I think it's likely a duplicate, so you might want to do some searching for similar questions here or via google first. See the answers to a similar/related question [Trying to fix tkinter GUI freeze-ups (using threads)](https://stackoverflow.com/questions/53525746/trying-to-fix-tkinter-gui-freeze-ups-using-threads). – martineau Mar 18 '19 at 08:51

0 Answers0