Anas AG
Anas AG

Reputation: 97

highlight clicked items in tkinter canvas?

General idea:

Many items (majority small images) are created on the canvas. The user can click on any item and move it.

I need the user to know which item was last clicked, by showing (drawing) a border/change brightness/any method.. around that item.

Is there any Image/item options to help apply this idea.

Upvotes: 0

Views: 1727

Answers (2)

Rachit Tayal
Rachit Tayal

Reputation: 1292

You can achieve that by writing a simple modify appearance method for a widget last clicked. Here is the sample code. Below we are performing two actions. First changing the appearance of last widget to normal and then changing the appearance of last clicked widget to highlight it.

def modifyAppearance(self, widget):
    global previously_clicked
    if 'previously_clicked' in globals():
        # rolling back the appearance of previous widget to normal
        previously_clicked['bg'] = widget['bg']
        previously_clicked['activebackground'] = widget['activebackground']
        previously_clicked['relief'] = widget['relief']

    # changing the appearance of the last clicked widget
    widget['bg'] = 'green'
    widget['activebackground'] = '#33B5E5'
    widget['relief'] = 'sunken'
    previously_clicked = widget

You will need to define global previously_clicked in other methods also, where you will be defining the widgets. You can refer my full code here. It has this functionality

Upvotes: 2

DJKarma
DJKarma

Reputation: 182

For example this is your button-

B1 = Button(root, text = "Click me", command = clickme)

we can pass more parameters here such as--

highlightcolor= The color to use for the highlight border when the button has focus. The default is system speciific. (highlightColor/HighlightColor)

and

highlightthickness= The width of the highlight border. The default is system specific (usually one or two pixels). (highlightThickness/HighlightThickness)

...

OR

...

Whenever the button is clicked you must be specifying some action to do in a function. What you can do is you can tell that function to slight increase the thickness of border by above parameters. :)

Upvotes: 0

Related Questions