0

I want to show "Loading..." and then execute a script while the user waits.

I understand that the canvas can only update itself after every loop, and that while the button command is called the canvas is frozen.

How could I fix this?

Currently, there is no text that appears. The success text does appear.

import tkinter as tk
import main


root = tk.Tk()  # create GUI instance
root.title('Exam Closeout')  # name GUI

def run_script():
    '''
    Run the script from main, that is, the data script.

    :return: void
    '''
    # show loading status: currently doesn't work, need to understand queue better
    current_text = tk.StringVar()
    current_text.set('Loading...')
    tk.Label(root, textvariable=current_text).grid(row=4, column=1, sticky='w')
    subject = entry_1.get() # grab subject from user input
    try:
        # call main
        main.test(subject)
        # show finish status
        current_text.set('Script successfully finished. You may exit out of this window now')
    except Exception as e:   # catch exceptions. currently doesn't show specific exception
        current_text.set("Error occurred. Try looking at documentation")


# label with instructions
tk.Label(root, text="Thank you for using me...").grid(row=1, column=1, sticky='w')

# label to show users where to enter exam
tk.Label(root, text="Enter exam code:").grid(row=2, column=1, sticky='w')

# blank box to enter exam into
entry_1 = tk.Entry(root,width=40)   # entry has to be its own line - for more, see below
entry_1.grid(row=2, column=2, columnspan=2)

# button to run script
tk.Button(root,text="Run", command=run_script).grid(row=3, column=3)

# start canvas
root.mainloop()

main.py

import time

def test(exam):
    time.sleep(5)
    print(exam)

In case it matters, main method is actually a data analysis script that takes in dataframes, manipulates them, and exports an Excel dataset.

1 Answer 1

1

Add a call to the universal update_idletasks() widget method at the location indicated below:

def run_script():
    '''
    Run the script from main, that is, the data script.

    :return: void
    '''
    # Show loading status.
    current_text = tk.StringVar()
    current_text.set('Loading...')
    tk.Label(root, textvariable=current_text).grid(row=4, column=1, sticky='w')
    subject = entry_1.get() # grab subject from user input
    try:
        root.update_idletasks()  #### ADD THIS LINE ####
        # call main
        main.test(subject)
        # show finish status
        current_text.set('Script successfully finished. '
                         'You may exit out of this window now')
    except Exception as e:  # Catch exceptions.
        current_text.set("Error occurred. Try looking at documentation")
Sign up to request clarification or add additional context in comments.

2 Comments

Bonus: Note that you can define and initialize a StringVar (or IntVar, DoubleVar, etc) in a single statement like this: current_text = tk.StringVar(value='Loading...') — i.e. by passing it the keyword argument value= followed by the initial value you want it assigned.
Another tip: You can force the focus to be on the Entry widget by adding a entry_1.focus_force() right after the entry_1.grid(row=2, column=2, columnspan=2 call.

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.