Is it possible to draw a BUTTON?

Christian Gollwitzer auriocus at gmx.de
Thu Jul 28 04:16:49 EDT 2016


Am 28.07.16 um 04:12 schrieb huey.y.jiang at gmail.com:
> On Wednesday, July 27, 2016 at 4:18:29 PM UTC-4, huey.y... at gmail.com wrote:
>> Hi Folks,
>>
>> It is common to put a BUTTON on a canvas by the means of coding. However, in my application, I need to draw a circle on canvas, and then make this circle to work as if it is a button. When the circle is clicked, it triggers a new image to be displayed. Somebody can help? Thanks!
>
> The example works. It is smart to bind a button to a widget

Maybe you misunderstood. There is no code which binds a button to a 
widget. I assume you talk about Rick's code, which is the most idiomatic 
solution:

> [...]
> canvas.create_oval(10,10,50,50, outline='red', fill='blue',
>                    tags=('button',)
>                    )
> canvas.tag_bind('button', '<Button>', cb_canvasButton)

Here, the 'button' is just an arbitrary string to identify this circle. 
You could also write:

 > canvas.create_oval(10,10,50,50, outline='red', fill='blue',
 >                    tags=('grmmpf',)
 >                    )
 > canvas.tag_bind('grmmpf', '<Button>', cb_canvasButton)

and it would work the same. Similarly, "<Button>" is an event which 
reacts to any mouse button press. Typically, a button would change it's 
shape when pressed and fire the event when released. So in a real app, 
you would probably do:

canvas.tag_bind('grmmpf', '<ButtonPress-1>', cb_canvasButtonPress)
canvas.tag_bind('grmmpf', '<ButtonRelease-1>', cb_canvasButtonRelease)

where in the first function you change the color of the button, say

canvas.tag_configure('grmmpf', fill='blue') # untested

and in the second you reset the color and execute the action.


	Christian



More information about the Python-list mailing list