Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77328

Re: Sequencing images using tkinter?

From Peter Otten <__peter__@web.de>
Subject Re: Sequencing images using tkinter?
Date 2014-08-30 21:54 +0200
Organization None
References <dcff98af-47cd-4dbc-9fa1-02dc6813df81@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.13650.1409428506.18130.python-list@python.org> (permalink)

Show all headers | View raw


theteacher.info@gmail.com wrote:

> I've started to learn to use tkinter but can't seem to rotate images.
> 
> Here is a Python 3.4 program to illustrate the problem. Anyone spot why
> the for loop doesn't seem to want to display a sucssession of images
> please? Thanks.

GUI programs are different from simple scripts; they have to react when the 
user resizes the window, clicks on a button, etc. The usual way to do that 
is to run an event loop permanently that calls functions that do something 
for a relatively small amount of time and then give control back to the 
loop.

time.sleep() in contrast stops the whole script (I'm simplifying) and thus 
should not be used here. Instead you can use myGui.after() to trigger the 
execution of a Python function:

> import sys
> from tkinter import *
> import random
> from time import sleep
> 
> myGui = Tk()
> myGui.geometry("1000x800+400+100")
> myGui.title("The Random Machine")
> 
> monsters = ["py01.gif", "py02.gif", "py03.gif", "py04.gif", "py05.gif",
> "py06.gif", "py07.gif", "py08.gif",
>             "py09.gif", "py10.gif", "py11.gif", "py12.gif", "py13.gif",
>             "py14.gif", "py15.gif", "py16.gif", "py17.gif", "py18.gif",
>             "py19.gif", "py20.gif",]
> 
> 
> #Main canvas
> canvas1 = Canvas(myGui, width=1000, height=800, bg="white")
> canvas1.place(x=0,y=0)

# prepare a list of `PhotoImage`s to avoid having to load an image
# more than once
monster_images = [
    PhotoImage(file="MonsterImages/Converted/" + monster)
    for monster in monsters]

#Main canvas
canvas1 = Canvas(myGui, width=1000, height=800, bg="white")
canvas1.place(x=0,y=0)

#button
myButton1 = Button(canvas1, text='OK', justify=LEFT)
myButton1.config(width="100", height="200")
myButton1.place(x=500, y=300)

def next_image():
    myButton1.config(image=random.choice(monster_images))

    # tell tkinter to invoke next_image() again after 200 miliseconds
    myGui.after(200, next_image)

# invoke next_image() for the first time
next_image()

myGui.mainloop()

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Sequencing images using tkinter? theteacher.info@gmail.com - 2014-08-30 08:54 -0700
  Re: Sequencing images using tkinter? Terry Reedy <tjreedy@udel.edu> - 2014-08-30 15:52 -0400
  Re: Sequencing images using tkinter? Peter Otten <__peter__@web.de> - 2014-08-30 21:54 +0200
  Re: Sequencing images using tkinter? theteacher.info@gmail.com - 2014-08-30 13:11 -0700
  Re: Sequencing images using tkinter? theteacher.info@gmail.com - 2014-08-30 13:27 -0700
    Re: Sequencing images using tkinter? "Albert Visser" <albert.visser@gmail.com> - 2014-08-30 22:49 +0200
  Re: Sequencing images using tkinter? theteacher.info@gmail.com - 2014-08-30 13:37 -0700
    Re: Sequencing images using tkinter? Peter Otten <__peter__@web.de> - 2014-08-30 23:14 +0200
      Re: Sequencing images using tkinter? theteacher.info@gmail.com - 2014-08-31 02:51 -0700

csiph-web