Python Tkinter Simple Qn

Discussion in 'Python' started by ChrisChia, Aug 12, 2010.

  1. ChrisChia

    ChrisChia Guest

    Hi i have the following problem with Python Tkinter.
    I switch to switch the image background (which i used Tkinter.Label
    with image arg to display on the GUI).

    How can I do that? the callback function which i have created doesn't
    seem to work...
    some advice?

    below is my code:


    import Tkinter as tk
    from PIL import Image, ImageTk

    root = tk.Tk()

    # having problem with switching the image
    def callback(event):
    global root
    root.panel1.pack_forget()
    root.panel1.image = image2
    root.panel1.pack()


    def app():

    root.title('FIT 2022 Assignment 1')

    # pick an image file you have .bmp .jpg .gif. .png
    # load the file and covert it to a Tkinter image object
    imageFile = "c:\\test1.jpg"
    image1 = ImageTk.PhotoImage(Image.open(imageFile))
    imageFile2 = "c:\\test2.jpg"
    image2 = ImageTk.PhotoImage(Image.open(imageFile2))


    # get the image size
    w = image1.width()
    h = image1.height()


    # position coordinates of root 'upper left corner'
    x = 0
    y = 0


    # make the root window the size of the image
    root.geometry("%dx%d+%d+%d" % (w, h, x, y))


    # root has no image argument, so use a label as a panel
    panel1 = tk.Label(root, image=image1)
    panel1.pack(side='top', fill='both', expand='yes')
    panel1.image = image1
    panel1.bind("<Button-1>", callback)


    panel1.pack()
    root.mainloop()


    app()
    ChrisChia, Aug 12, 2010
    #1
    1. Advertising

  2. ChrisChia

    Eric Brunel Guest

    In article
    <>,
    ChrisChia <> wrote:

    > Hi i have the following problem with Python Tkinter.
    > I switch to switch the image background (which i used Tkinter.Label
    > with image arg to display on the GUI).
    >
    > How can I do that? the callback function which i have created doesn't
    > seem to work...


    It is usually way better to post the actual error than just saying
    "doesn't seem to work"Š Here your problem is quite clear, but in most
    circumstances, it would be quite difficult to figure out what's
    happening.

    > some advice?
    >
    > below is my code:
    >
    >
    > import Tkinter as tk
    > from PIL import Image, ImageTk
    >
    > root = tk.Tk()
    >
    > # having problem with switching the image
    > def callback(event):
    > global root
    > root.panel1.pack_forget()
    > root.panel1.image = image2


    The variable image2 is not known in this context, so no wonder the
    callback doesn't work. Let me guess: it says something like "NameError:
    name 'image2' is not defined"?

    > root.panel1.pack()
    >
    >
    > def app():
    >
    > root.title('FIT 2022 Assignment 1')
    >
    > # pick an image file you have .bmp .jpg .gif. .png
    > # load the file and covert it to a Tkinter image object
    > imageFile = "c:\\test1.jpg"
    > image1 = ImageTk.PhotoImage(Image.open(imageFile))
    > imageFile2 = "c:\\test2.jpg"
    > image2 = ImageTk.PhotoImage(Image.open(imageFile2))


    Here, you create image2 as a local variable in function app. So the
    variable name will not be known outside of app. If you want it to be
    global, you have to add a line:
    global image2
    at the beginning of your app function. Then it should work correctly.

    By the way, I usually find it better to organize a GUI into classes
    rather than using functions. This way, you could store the images in
    object attributes and define your callback as a method, and not as a
    function.

    > # get the image size
    > w = image1.width()
    > h = image1.height()
    >
    >
    > # position coordinates of root 'upper left corner'
    > x = 0
    > y = 0
    >
    >
    > # make the root window the size of the image
    > root.geometry("%dx%d+%d+%d" % (w, h, x, y))
    >
    >
    > # root has no image argument, so use a label as a panel
    > panel1 = tk.Label(root, image=image1)
    > panel1.pack(side='top', fill='both', expand='yes')
    > panel1.image = image1
    > panel1.bind("<Button-1>", callback)
    >
    >
    > panel1.pack()
    > root.mainloop()
    >
    >
    > app()


    HTH
    - Eric -
    Eric Brunel, Aug 12, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Epler
    Replies:
    0
    Views:
    506
    Jeff Epler
    Aug 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    454
    Jeff Epler
    Aug 23, 2004
  3. Pierre Dagenais
    Replies:
    0
    Views:
    310
    Pierre Dagenais
    Aug 3, 2008
  4. Hidekazu IWAKI
    Replies:
    1
    Views:
    377
    Peter Otten
    Dec 14, 2009
  5. Hidekazu IWAKI
    Replies:
    0
    Views:
    523
    Hidekazu IWAKI
    Dec 15, 2009
Loading...

Share This Page