[Tutor] Update a button content on click

ALAN GAULD alan.gauld at btinternet.com
Tue Feb 15 18:50:51 CET 2011


I modified your code slightly for Python 3.1 to this:

#######################
from tkinter import *
root=Tk()

def ok():
 b1["image"]=photo

photo=PhotoImage(file=r"M:\Photos\TomMcM\Chris.GIF")
b1=Button(root,text="",image=None,command=ok)
b1.grid()

root.mainloop()
#########################

Which works perfectly for me.
What happens for you? 
Do you get any error messages?

 Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/



>
>From: ANKUR AGGARWAL <coolankur2006 at gmail.com>
>To: ALAN GAULD <alan.gauld at btinternet.com>
>Cc: tutor at python.org
>Sent: Tuesday, 15 February, 2011 17:04:06
>Subject: Re: [Tutor] Update a button content on click
>
>
>from Tkinter import *
>root=Tk()
>
>
>photo=PhotoImage(file="Cross.gif")
>def ok():
> b1["image"]=photo
>
>
>b1=Button(root,text="",image=None,command=ok)
>b1.grid()
>root.mainloop()
>
>
>Here's my code . Not getting the desired output
>
>On Tue, Feb 15, 2011 at 10:26 PM, ALAN GAULD <alan.gauld at btinternet.com> wrote:
>
>> b1=button(root,image=None,height=4,width=4,command=ok)
>>
>>This says you want the button to e 4 pixels squarte. Is that really 
>>what you want?, because....
>>
>>
>>and then ok defination is as follows :
>>> def ok():
>>>  b1["image"]=photo
>>>
>>>
>>>now problem is that after clicking on the button, image is coming 
>>>up like a flash and then button becomes almost to zero size. 
>>>
>>>
>>Like to 4x4 pixels maybe, just as you asked?
>>What happens if you change the size to something like 40x40?
>>
>>Also in Tkinter to display an image you usually have to firtst create 
>>a PhotoImage object and assign that to the image attribute. It's not 
>>clear from your code whether you are doing that or not.
>>Is photo a PhotImage object?
>>
>>
>>I want to retain the height=4,width=4 along with the image after 
>>>clicking on the button. Image size is smaller than the button size.
>>>It sounds like your original size is greater than 4x4 and after setting 
>>the image it shrinks to 4x4...
>>
>>
>>HTH,
>>
>>Alan G.
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110215/9db4a4e8/attachment-0001.html>


More information about the Tutor mailing list