Hi i was wondering if anyone could help me, im wanting to when you click in a certain area it changes the background image

thanks

import Tkinter as tk
from Tkinter import*
import urllib
import base64
def click(event):

    if event.x > 100 and event.x < 430:
        if event.y > 100 and event.y < 450:
              #if inbetween here change background image
root = tk.Tk()
root.title('click to change image')
URL = "image1.gif"
a = urllib.urlopen(URL)
raw_input = a.read()
a.close()
b = base64.encodestring(raw_input)
image = PhotoImage(data=b)
label = Label(image=image)
label.pack()
label.bind('<Button-1>', click)

root.mainloop()

Edited 2 Years Ago by Dani: Formatting fixed

well maybe on line 9 you could say url = "FILENAME.EXT" where filename is the name and ext is the extention (DUR DEE DUR)

well I have no idea what python even is really so Im in the dark i just did what javascript would do
maybe like a.update() or something

There ar three basic ways to load and display an image with Tkinter:

1) from an image file you have, GIF is native any other file format needs PIL
see tk example in:
http://www.daniweb.com/software-development/python/threads/191210/python-gui-programming#post866067

2) from a base64 encoded image string
pick an example from:
http://www.daniweb.com/software-development/python/code/440446/python2python3-base64-encoded-image

3) from a web page on the internet
example:
http://www.daniweb.com/software-development/python/code/467528/show-internet-image-with-tkinter

You seem to mix those thing up. Stick with one way or the other.

Edited 2 Years Ago by ZZucker

I would use this approach, use whatever .gif file you have, sorry this is from my iPad and it does not work well with DaniWeb commands:

,,,
display an image using Tkinter
for Python2
'''
import Tkinter as tk

root = tk.Tk()

image_file = "Vacation2013.gif"
photo = tk.PhotoImage(file=image_file)
root.title(image_file)

label = tk.Label(root, image=photo)
label.pack(padx=5, pady=5)

root.mainloop()

Edited 2 Years Ago by Dani: Added code tags

Using a base64 encoded GIF image string with Tkinter is very easy and usefull for smaller images. First you create the image string ...

''' Base64encGIF2.py
create a base64 image string
changes the bytes of an image file into printable char combinations
you can then copy and paste this string into your program

with Tkinter you can use the b64 data directly
photo = tk.PhotoImage(data=ladybug_gif_b64)
note Tkinter needs GIF format

with other GUI toolkits use decode with
ladybug_gif = base64.b64decode(ladybug_gif_b64)

Python2 syntax
'''

import base64

# pick an image file you have in the working directory
# or give full path
img_file = "ladybug.gif"
b64 = base64.encodestring(open(img_file,"rb").read())
print("ladybug_gif_b64='''\\\n" + b64 + "'''")

""" highlight, copy and paste from here ...

ladybug_gif_b64='''\
R0lGODlhIAAgALMAAP///wAAADAwMP99Hf8AAP+lAP//AMopUwAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAACH5BAAAAAAALAAAAAAgACAAAwTHEMhJq714hp3lDh0GiqH2UWOVAt96pUIsBLKglWg87Dwv
4xMBj0Asxgg+XKxwLBJrxUGsI5TKnARoVHoLDp5XbNP5cwmNAqwa/aOc13ByrfKOw2UGw1SSxrb+
AWIxeXsAaX92UDQ1Nh6BdneMhQIHkHGSjYaVlmt4epmacp19YAKEoJRspKWrjBapWWGqcm1uB5tj
ok4HZa+3m5wEt5kuhpTAcb+FVL/NzspAfDHPysslMJjEIS0oLygnOMVd0SwcHeDk6errEQA7
'''

"""

Then you copy the string to your program ...

""" Base64encGIF_tk_show2.py
use a base64 encoded image file with Tkinter
tested with Python 2.7.5
"""

import base64
import Tkinter as tk

root = tk.Tk()

ladybug_gif_b64='''\
R0lGODlhIAAgALMAAP///wAAADAwMP99Hf8AAP+lAP//AMopUwAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAACH5BAAAAAAALAAAAAAgACAAAwTHEMhJq714hp3lDh0GiqH2UWOVAt96pUIsBLKglWg87Dwv
4xMBj0Asxgg+XKxwLBJrxUGsI5TKnARoVHoLDp5XbNP5cwmNAqwa/aOc13ByrfKOw2UGw1SSxrb+
AWIxeXsAaX92UDQ1Nh6BdneMhQIHkHGSjYaVlmt4epmacp19YAKEoJRspKWrjBapWWGqcm1uB5tj
ok4HZa+3m5wEt5kuhpTAcb+FVL/NzspAfDHPysslMJjEIS0oLygnOMVd0SwcHeDk6errEQA7
'''

photo = tk.PhotoImage(data=ladybug_gif_b64)

# put the image on a typical widget
label = tk.Button(root, image=photo)
label.pack(expand='yes', fill='both')

root.mainloop()

Edited 2 Years Ago by vegaseat

This question has already been answered. Start a new discussion instead.