[Tutor] Update a button content on click

ALAN GAULD alan.gauld at btinternet.com
Tue Feb 15 19:04:55 CET 2011


It works fine for me. Except that the button is too big before I press 
it - I'm not sure why - but after pressing it it shrinks to 4x4 pixels 
with the image inside.(albeit too small to see!).

OK, more experimenting...
It seems that Tkinter is smart enough to use characters for 
sizes when displaying a text label but pixels for images. So in  
the event handler you need to reset the size.

This works for me with a GIF size of 200x100

####################
from tkinter import *
root=Tk()
photo=PhotoImage(file=r"M:\Photos\TomMcM\Chris.GIF")

def ok():
   # reset size to pixels for image
   b1.configure(image=photo, height="90", width="140")

# original size in chars
b1=Button(root,text=" ", command=ok, height="6", width="20")
b1.grid()

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


 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:14:38
>Subject: Re: [Tutor] Update a button content on click
>
>upper code posted earlier  is running fine....
>
>
>The problem is in the code below bcoz of the height nd width factor...
>
>from Tkinter import *
>root=Tk()
>
>
>photo=PhotoImage(file="Cross.gif")
>def ok():
> b1["image"]=photo
>
>
>b1=Button(root,text="",image=None,height=4,width=4,command=ok)
>b1.grid()
>
>
>On Tue, Feb 15, 2011 at 10:34 PM, ANKUR AGGARWAL <coolankur2006 at gmail.com> 
>wrote:
>
>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/87594131/attachment.html>


More information about the Tutor mailing list