Resolved: How do I to call a custom object with custom style methods and use it as a style for a label(and widgets in general) in tkinter?

In this post, we will see how to resolve How do I to call a custom object with custom style methods and use it as a style for a label(and widgets in general) in tkinter?

Question:

So I am trying to import my own custom style methods into my main app to then use as a custom style in ttk.Label(), by calling the class method, but I’m having trouble finding a way to call it. Below is the example code of the main app.
And below is the class I am calling the methods above from, which is different file.
I’ve tried to call just the name of the style method like this
I’ve also tried to call the method by calling the class then method like this
I knew this wouldn’t work, but I still tried it
I also tried not making an object out of the methods, so I took away the class and just made a list of functions, but that didn’t work either. Of course, I looked on the internet and searched stack for questions, but to no avail. Maybe this structure I am trying to maintain is not efficient?
I’m honestly just looking to understand a way to call the methods w/o putting them in the same file, but I just don’t know how to.

Best Answer:

The style option requires the name of a style as a string. Since your function test returns None, it’s the same as if you did ttk.Label(..., style=None)
One solution is to have your test function return the style name:
Of course, that means you can only use it for that one specific style. Another solution is that you leave it as-is and return nothing. In that case you can just hard-code the style. You must ensure that you call the test function, however, so that the style is initialized.
Arguably, a better option would be to add attributes to the class, and initialize the styles when you instantiate the class. It might look something like this:
There are probably even better ways to do this. The point is, you need to pass a valid style name as a string to the style parameter of a ttk widget.

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com