0

I have following function:

def button_enter(self, event, type):
    if self.green_button_anim_on:
        return
    self.canvas.itemconfigure(self.green_button, image=self.green_button_image_hover)

This functions serves for the change of image of green button to "hover state" image when user hovers mouse over it. What I am trying to do - I want to use this function universally, not just for the green button, but also for the red button, quit button etc. So I decided to pass the argument type to the function that will have values like red, green, quit etc.

Now I need to change the word green everywhere inside this function according to the value of argument type. What is the easiest way to do it please? Thank you!

2 Answers 2

1

You could save the buttons and images in a dict:

self.buttons = { "green": self.green_button, ... }
self.images = {...}
...

Then, to retrieve the right objects:

self.canvas.itemconfigure(self.buttons[type], image=self.images[type])
Sign up to request clarification or add additional context in comments.

Comments

0

You can have use enum in python this way you can use meaningful names and map it to constant values.

3 Comments

add an example how enum helps the OP
yes, please, can you add some example? I am reading the description and usage of enum, but I am still not sure how exactly can I use it in my case... thanks!
Sure, I am just constructing a simple example. I will post it shortly Can you maybe post your class structures and relevant code so that I can give an example relevant exactly to your case?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.