3

I'm trying to create a program which displays images using tkinter. The code I've written so far displays the image perfectly, however the program does not continue until the tkinter window is closed. I was wondering if there was any function instead of .mainloop() to keep the program running alongside the tkinter window? Any help much appreciated

window = Tk()                                                         
canvas1 = Canvas(window,height=600,width=600)                         
file = PhotoImage(file = 'GoogleDrive.png')                                          
image = canvas1.create_image(300, 300, anchor = CENTER, image = file) 
canvas1.pack()
canvas1.mainloop()

print ('\nA: Google Drive')
time.sleep(1)
print ('B: Google Slides')
time.sleep(1)
print ('C: Google Photos')
KHumphs
  • 55
  • 1
  • 6
  • 2
    use `tkinter.after()` instead of `sleep()` – furas Dec 08 '17 at 20:21
  • Your program _does_ keep running after `mainloop`. It's just that is an infinite loop. If this is not what you're asking then this question is a duplicate of [How do you run your own code alongside tkinter's event loop?](https://stackoverflow.com/questions/459083/how-do-you-run-your-own-code-alongside-tkinters-event-loop). – Nae Dec 09 '17 at 12:48
  • Possible duplicate of [How do you run your own code alongside Tkinter's event loop?](https://stackoverflow.com/questions/459083/how-do-you-run-your-own-code-alongside-tkinters-event-loop) – Nae Dec 09 '17 at 12:49

2 Answers2

4

The mainloop needs to be the last logical line of code. The mainloop should also be called on the Tk object, in your case window.

In order for your code to run, you either need to move the print and sleep statements before the mainloop, or move them to a function and instead call that function, before the mainloop.

It is also worth noting that in Tkinter, you can use after for timings instead of sleep.

SneakyTurtle
  • 1,831
  • 1
  • 13
  • 23
3

Tkinter is event driven. This means that there must be an action (from the script or user) given to tkinter to change things.

The mainloop() function executes the window so there is no alternative to it. Fortunately there are solutions:

If you want to perform actions alongside tkinter window(s) you have two options:

You can perform them all before mainloop() is called.

Or make them execute when a user performs an action in form of a button ect.

Or you can run functions in the background with threading or multiprocessing.

If you go for this option I would suggest threading because:

  • It's easy to use
  • You can communicate between different parts of the script. You can struggle to do this in multithreading

However this depends entirely on the specific task.


In the example code you have given your best option is to call it before mainloop() is executed. For example:
window = Tk()                                                         
canvas1 = Canvas(window,height=600,width=600)                         
file = PhotoImage(file = 'GoogleDrive.png')                                          

image = canvas1.create_image(300, 300, anchor = CENTER, image = file) 
canvas1.pack()
print ('\nA: Google Drive') 
time.sleep(1)
print ('B: Google Slides')
time.sleep(1)
print ('C: Google Photos')
canvas1.mainloop()

Although you could put it anywhere before mainloop().

Xantium
  • 11,201
  • 10
  • 62
  • 89