changing the image on a button

Post here if you need help with creating a Graphical User Interface in Python.

changing the image on a button

Postby natty066 » Thu Jan 23, 2014 5:14 am

I would like to change the image on a button by clicking on another button, but its not working. All suggestions are welcome. (the 'DEAL' button should be clicked to change the image)

Code: Select all
from Tkinter import *

import tkMessageBox

root = Tk()
frame = Frame(root)
frame.pack()

topframe = Frame(root)
topframe.pack(side = TOP)

bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )

card_list = ('c_1_.gif','c_2_.gif','c_3_.gif','c_4_.gif','c_5_.gif')

ButtonImage1 = PhotoImage(file= 'card_back.gif')

num1 = 100
num2 = 5
sum1 = num1 - num2

v=StringVar()
v.set('win amount')

#photo = PhotoImage(...)

#label = Label(image=photo)
#label.image = photo # keep a reference!
#label.pack()
   
def func_1():
   v.set(sum1)
   ButtonImage1 = PhotoImage(file= 'c_1_.gif')
   
   
   
def deal():
   ButtonImage1 = PhotoImage(file= card_list[0])
   

button_card_one = Button(topframe, image=ButtonImage1 )
button_card_one.pack( side = LEFT)

w = Label(bottomframe, textvariable=v)
w.pack(side=TOP)


Play_button1 = Button(bottomframe, text="DEAL", fg="black" , command = func_1)
Play_button1.pack(side = LEFT)

Play_button2 = Button(bottomframe, text="STAND", fg="black")
Play_button2.pack(side = LEFT)

Play_button3 = Button(bottomframe, text="EXIT", fg="black")
Play_button3.pack(side = LEFT)

# save the button's image from garbage collection (needed?)
button_card_one.image = ButtonImage1

root.mainloop()
Last edited by metulburr on Thu Jan 23, 2014 5:41 am, edited 1 time in total.
Reason: initial post lock and added code tags
natty066
 
Posts: 5
Joined: Thu Jan 23, 2014 5:08 am

Re: changing the image on a button

Postby metulburr » Thu Jan 23, 2014 5:46 am

it would make it a lot easier if you supplied all the images required to run this code.... upload them to something lke photobucket, and then have the code load it from the url, or create a repo on github.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1450
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: changing the image on a button

Postby natty066 » Thu Jan 23, 2014 5:57 am

http://s828.photobucket.com/user/natty066/library/

Am pretty new at python, dont know how to load the images from photo bucket
natty066
 
Posts: 5
Joined: Thu Jan 23, 2014 5:08 am

Re: changing the image on a button

Postby metulburr » Thu Jan 23, 2014 6:32 am

i dont really feel like rewriting your code to suit the expectations, so i only changed what is needed.

the expectations being:
not using star imports
naming convention (classes are the only names that are capped)
to switch it to use OOP using classes, which would simplify this code


Code: Select all
from Tkinter import *

import tkMessageBox

root = Tk()
frame = Frame(root)
frame.pack()

topframe = Frame(root)
topframe.pack(side = TOP)

bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )

card_list = ('c_1_.gif','c_2_.gif','c_3_.gif','c_4_.gif','c_5_.gif')

ButtonImage1 = PhotoImage(file= 'card_back.gif')

num1 = 100
num2 = 5
sum1 = num1 - num2

v=StringVar()
v.set('win amount')

#photo = PhotoImage(...)

#label = Label(image=photo)
#label.image = photo # keep a reference!
#label.pack()
   
def func_1(image):
   v.set(sum1)
   #ButtonImage1 = PhotoImage(file= 'c_1_.gif')
   image.configure(file= 'c_1_.gif')
   
   
   
def deal(image):
   ButtonImage1 = PhotoImage(file= card_list[0])
   

button_card_one = Button(topframe, image=ButtonImage1 )
button_card_one.pack( side = LEFT)

w = Label(bottomframe, textvariable=v)
w.pack(side=TOP)


Play_button1 = Button(bottomframe, text="DEAL", fg="black" , command = lambda x=ButtonImage1:func_1(x))
Play_button1.pack(side = LEFT)

Play_button2 = Button(bottomframe, text="STAND", fg="black")
Play_button2.pack(side = LEFT)

Play_button3 = Button(bottomframe, text="EXIT", fg="black")
Play_button3.pack(side = LEFT)

# save the button's image from garbage collection (needed?)
button_card_one.image = ButtonImage1

root.mainloop()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1450
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: changing the image on a button

Postby natty066 » Thu Jan 23, 2014 6:45 am

Thanks. As I get more experience I will try to reach the expectations.
natty066
 
Posts: 5
Joined: Thu Jan 23, 2014 5:08 am

Re: changing the image on a button

Postby metulburr » Thu Jan 23, 2014 7:52 am

the best way to learn is just to do it. By conintuing on the path of "i'll do it later" you get accustomed to the way you are doing it now. Making it much harder. I think that OOP should be known before moving on to tkinter GUI design. IF you dont know it, then you can easily do command line apps to get accustomed to using it.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1450
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: changing the image on a button

Postby natty066 » Thu Jan 23, 2014 1:02 pm

Ok.
natty066
 
Posts: 5
Joined: Thu Jan 23, 2014 5:08 am

Re: changing the image on a button

Postby natty066 » Thu Jan 23, 2014 1:18 pm

Could u point me to a good tutorial site?
natty066
 
Posts: 5
Joined: Thu Jan 23, 2014 5:08 am


Return to GUI

Who is online

Users browsing this forum: No registered users and 3 guests