pythonclassobjectkivypositional-argument

Kivy/Python: missing 1 required positional argument


I am not too terribly familiar with classes/class methods and how they work, and I know my code is atrocious! I wanted to dip my toe into the world of UI, and so I wanted to take a simple function I wrote in Python to see if I could create an app out of it. (Edit: The script produces a ":(" if today is not Monday, and a ":)" if today IS Monday.) (Double edit: this is my first post, I apologize for my ignorance re: coding and also Stack Overflow formatting.) We've got:

#!/usr/bin/env python                                                                                                          
from kivy.app import App
from kivy.uix.label import Label
import datetime
from datetime import date
import calendar


today = date.today().strftime('%Y-%d-%m')
def findDay(self, today):
    day = datetime.datetime.strptime(today, '%Y-%d-%m').weekday()
    y = (calendar.day_name[day])
    if y == 'Monday':
        x = ':)'
        return x
    else:
        x = ':('
        return x
    print(x)



class MyApp(App):
    def build(self):
        today = date.today().strftime('%Y-%d-%m')
        z = findDay(today)
        return Label(z)

if __name__ == "__main__":
    MyApp().run()

With the error:

Traceback (most recent call last):
   File "main.py", line 30, in <module>
     MyApp().run()
   File "/Users/myusernamehere/opt/anaconda3/lib/python3.7/site-packages/kivy/app.py", line 829, in run
     root = self.build()
   File "main.py", line 26, in build
     z = findDay(today)
 TypeError: findDay() missing 1 required positional argument: 'today'

I know this error arises from incorrect instantiation of an object of a class... but based on how I have defined "today", I am unsure what this means in my given context! (Unless it has to do with "self"?)


Solution

  • You need self only when you write an instance method (in a class). For normal functions you don't need self in the signature.

    This should work fine:

    def findDay(today):
        day = datetime.datetime.strptime(today, '%Y-%d-%m').weekday()
        y = (calendar.day_name[day])
        if y == 'Monday':
            x = ':)'
            return x
        else:
            x = ':('
            return x
        print(x)