Re: [Tutor] Update a button content on click

2011-02-15 Thread ALAN GAULD
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 
>To: ALAN GAULD 
>Cc: tutor@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  
>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  
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.
>>>
>>
>___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Update a button content on click

2011-02-15 Thread ANKUR AGGARWAL
your code was fine. I am having problem with this code

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()
root.mainloop()


On Tue, Feb 15, 2011 at 11:20 PM, ALAN GAULD wrote:

> 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 
> *To:* ALAN GAULD 
> *Cc:* tutor@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 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.
>>
>>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Update a button content on click

2011-02-15 Thread ALAN GAULD
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 
>To: ALAN GAULD 
>Cc: tutor@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  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.
>>
>___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Update a button content on click

2011-02-15 Thread ANKUR AGGARWAL
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 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 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.
>>
>>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Update a button content on click

2011-02-15 Thread ANKUR AGGARWAL
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 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.
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Update a button content on click

2011-02-15 Thread ALAN GAULD
> 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.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Update a button content on click

2011-02-15 Thread ANKUR AGGARWAL
hmmm... i got the point.
I applied it in image case.

b1=button(root,image=None,height=4,width=4,command=ok)

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. 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.

On Tue, Feb 15, 2011 at 5:58 PM, Alan Gauld wrote:

>
> "ANKUR AGGARWAL"  wrote
>
>
>  I am looking for a method using Tkinter module to update the button on the
>> click.
>>
>
> Define an event handler command for the button.
> Lets call it sayHi()
>
> def sayHi(self):
>self.myButton['text'] = 'hi'
># Note:event handlers don't return results so store it as an attribute
>self.myButtonResult = 'hi'
>
> Now create the widget with spam as the handler.
>
> self.myButton = Button(parent, text='hello', command = spam)
>
> That's it.
>
>
>  the its click should be "hi". I tried it through event handling but failed
>> to do so.
>>
>
> It would be more helpful if you posted the code that you tried
> and a more detailed explanation of "failed to do so".
> What happened? Did it update but not store a value?
> Did you get an error message? Did anything happen?
>
> The more specific information you provide the more likely you
> are to get a specific solution that works.
>
> HTH,
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Update a button content on click

2011-02-15 Thread Alan Gauld


"ANKUR AGGARWAL"  wrote

I am looking for a method using Tkinter module to update the button 
on the

click.


Define an event handler command for the button.
Lets call it sayHi()

def sayHi(self):
self.myButton['text'] = 'hi'
# Note:event handlers don't return results so store it as an 
attribute

self.myButtonResult = 'hi'

Now create the widget with spam as the handler.

self.myButton = Button(parent, text='hello', command = spam)

That's it.

the its click should be "hi". I tried it through event handling but 
failed

to do so.


It would be more helpful if you posted the code that you tried
and a more detailed explanation of "failed to do so".
What happened? Did it update but not store a value?
Did you get an error message? Did anything happen?

The more specific information you provide the more likely you
are to get a specific solution that works.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor