(no subject)

Quinn Dunkan quinn at hork.ugcs.caltech.edu
Fri Aug 17 14:04:54 EDT 2001


On Fri, 17 Aug 2001 01:02:25 -0000, moonlite56 at yahoo.com
<moonlite56 at yahoo.com> wrote:
>How do you predefine a function?

You can't.  Python has to know what a function is before doing anything with
it (like calling it).  But it only tries to do something with it when it
actually sees it, so you don't need compiler reassurances like C forward
declarations.

I.e. you can type things like:

def f():
    x()

def x():
    pass

f()



>                                 I want to change an image in a 
>canvas and update it, but it (of course) comes up the error that the 
>canvas is "referenced before assignment." :) I know there's a way to 
>predefine the function at the beginning of my program but actually 
>define it later on, but I don't know how. Can someone else help?

No, there is no way.  If you could "predefine" a function, what would happen
when you called that function (before it was actually defined)?

You don't give an exact error message or line number, and the code below has
no "canvas" variable, so I can't tell what your problem is.

>Here's my program thus far:
>
>---------------
>
>from Tkinter import *
>import Image,ImageTk
>
>def changeimage():
>        cKuti3.delete(myimage)
>	iKuti3 = 
>ImageTk.PhotoImage(Image.open("outfit/pinkvest.gif"))
>	cKuti3 = cpic.create_image(0, 0, anchor = NW, image = iKuti3)

... but you should be aware that creating a new 'cKuti3' in changeimage isn't
going to affect the global cKuti3 at all.  Even if you did rebind the global
name (with a global declaration), the container it's packed into won't know
about that.  Not that you're ever packing it into a container, so it probably
wouldn't even get that far.  You should mutate cKuti3 with method calls:

cKuti3.configure(image=ImageTk.PhotoImage(...))


>top = Tk()
>top.title("Kuti")
>top.geometry("200x100")
>
>iKuti1 = ImageTk.PhotoImage(Image.open("body/lighttan.gif"))
>iKuti2 = ImageTk.PhotoImage(Image.open("hair/brownbuns.gif"))
>iKuti3 = ImageTk.PhotoImage(Image.open("outfit/whiteblouse.gif"))
>
>cpic = Canvas(width = 40, height = 40)
>cpic.grid(column = 0, row = 0, columnspan = 2)
>
>cKuti1 = cpic.create_image(0, 0, anchor = NW, image = iKuti1)
>cKuti2 = cpic.create_image(0, 0, anchor = NW, image = iKuti2)
>cKuti3 = cpic.create_image(0, 0, anchor = NW, image = iKuti3)
>
>changeimage()
>
top.mainloop()



More information about the Python-list mailing list