Tkinter, static error

I’m working with tkinter and got an error. Method ‘say_hello’ may be ‘static’. What does this mean, as the script works? How do you correct this? I hate it when I don’t know what messages are trying to tell me. As you can see I’m starting to use class, baby steps. Thanks for your help.

import tkinter as tk


class App(tk.Tk):
    """This is the template for tkinter"""
    def __init__(self):
        super().__init__()
        self.btn = tk.Button(self,
                             text="Click me!",
                             command=self.say_hello)  # can be removed
        self.btn.pack(padx=120,
                      pady=30)  # can be removed

    def say_hello(self):  # This is the offending line of code.
        """Output to terminal"""
        print('Hello Tkinter!')  # can be removed


if __name__ == '__main__':
    """Sets up the app window with the icon and title"""
    app = App()
    app.title('Dog House Programming')
    photo = tk.PhotoImage(file='dog_house.png')
    app.iconphoto(False, photo)  # False means only on the first window
    app.mainloop()

Are you using pycharm? This sounds like your IDE is giving you some suggestions on the code (not python directly). This is not an “error” that is breaking your code.

In say_hello(), you have written it as a normal method (so the instance is passed in to the method as self). But you never use that variable at all. So it’s suggesting that you might want to make it a static method and avoid passing in the instance.

You can ignore the suggestion for now, or look up static methods and see how it might apply in this case.

Hi Leonard,

You’ve been posting here often enough that you should know to post the entire error message, not just an abbreviated and possibly paraphrased summary of it.

Did you get an actual runtime error, complete with traceback, or did you get a linter warning?

What did it say exactly, in full? Please copy and paste as text, not a screenshot.

I’ve never seen an exception that some method “may be” static. At runtime, the interpreter will know with 100% certainty whether or not the method is or isn’t a staticmethod, so that seems like an unlikely error. So I’m going to guess that maybe you are using a linter and it is suggesting that since your say_hello method doesn’t use self, you might like to turn it into a staticmethod instead.

I won’t say that’s bad advice exactly, but it’s borderline, and certainly not something that beginners should care about.

Linters are sometimes very opinionated about code style, and not always in a good way. This is why I feel that linters and beginners don’t mix. One should not use a linter until you know the language well enough to not need a linter :slight_smile:

Thank you. I’m using PyCharm. It is automatic lintering in the GUI. Some of them are really helpful, formating lines of code, correct amount of spaces, lines etc. I find that working through them creates better standard looking(looks the same) code. Just like docstrings. The one bright spot of this is that there is more to learn. Decorators are now on the list to learn in there time. Enough for now to understand why it is considered static and how to fix it. I now know this is cleared by making it part of the App class using the @classmethod and cls in place of self. This clears the static warning errror because it is part of the class. I will understand this better when I become better aquainted with Python.