0

The Python app I'm developing loads a largish initialization file from the net. The process is taking so long that the users are relaunching the app before the window can pop open. What I'm looking for is a way to pop open the apps tkinter window (with or without a loading message) before the app loads the network file. This will let the user know what is happening and keep them from relaunching the app.

In other words, how do I automatically run some code after the call to mainloop() to open the window, but before the user interface goes idle waiting for clicks?

Code I'm currently using to pop open the window:

winWid = 650
winHei = 390
window = tkinter.Tk()
window.geometry(str(winWid) + "x" + str(winHei))
window.title("AppTitle")
window.configure(bg="#FFFFDC")

# Load Initialization file - goal is to have this automatically happen after window opens

window.mainloop()

Edit: Launching an initialization thread might do the trick, but it seems like drastic over kill when all I want to do is populate a list in the gui with the contents of a network file.

12
  • I would recommend to use two threads, but I dont think tkinter would communicate between two different threads. Commented Mar 18, 2021 at 18:21
  • Sounds like you need to look at threading Commented Mar 18, 2021 at 18:22
  • Not this is the sort of reason why programs originally used splashscreens - you can have a small notification come up while another thread loads the config, then both shut down and move onto the main application Commented Mar 18, 2021 at 18:25
  • Adding a second thread seems like a drastic measure for my little app. I was hoping for something a little more straight forward. Commented Mar 18, 2021 at 18:29
  • 1
    Try putting the loading task in a function, e.g. loading_configs() and call window.after(100, loading_configs) before window.mainloop(). Note that it is still blocking you from interacting with the window if loading_configs() is time-consuming task. Commented Mar 19, 2021 at 8:30

2 Answers 2

2

The answer came from @CoolCloud and @acw1668 in the form of the after() method.

window.after(1, loadAndDisplay) # Wait for a single millisecond before calling
window.mainloop()               # Pop open the window

after(milliseconds,function,*args) will launch the given function after a millisecond timer expires. By making the timer a single millisecond, the function launches almost without delay. Documentation suggests that a delay of 0 will launch the method as soon as mainloop completes its setup. In practice however, I found that a zero cause the call to happen as soon as mainloop is called.

Further detail can be found at this link: tkinter: how to use after method

Sign up to request clarification or add additional context in comments.

Comments

1

What about you make a button and prompt them to start the process and then they can wait for it to finish instead of restarting the whole time. For example : Like in a app they ask you first to and then download or do whatever needs to be done.

from tkinter import *
root = Tk()


def process():
    pass
    #do your process here
    
    
btn1 = Button(root, text="Start process",command=process)
btn1.pack()

root.mainloop()

5 Comments

The init file populates a list that supports the main purpose of the app. In other words, the app does nothing without the file. This is why I want to start this lengthy process before when the app launches rather than holding up the user waiting for them to start it.
Ooh let me see what i can do
But if the app does nothing without the file launching it and doing the task or not launching it does thr same thing
The gui can exist without without the data from the file. It is just a lot of data to load into a list, and I want it to load automatically while still displaying the window as quickly as possible.
Okay I think i can crack this

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.