Home > other >  TypeError: missing 3 required positional arguments
TypeError: missing 3 required positional arguments

Time:04-19

I am doing a coding class assignment where i am trying to code something to tell you the day of the week on any given date.

#importing tkinter and creating a window pop up
import tkinter

window = tkinter.Tk()



#titling the window

window.title("day of the week on any given date")


#add widgets here

canvas = tkinter.Canvas(window, bd=3, height=500, width=500)

#making a border
xy_black = 500, 500, 500, 10, 10, 10, 10, 500
border1 = canvas.create_polygon(xy_black, fill="black")
xy_white = 495, 495, 495, 15, 15, 15, 15, 495
border2 = canvas.create_polygon(xy_white, fill="white")

canvas.pack()
#year
entry1 = tkinter.Entry (window) 
canvas.create_window(150, 70, window=entry1)

labelyear = tkinter.Label(window, text= 'Enter any year' ,font=('helvetica', 10))
canvas.create_window(110, 40, window=labelyear)

#month
entry2 = tkinter.Entry (window) 
canvas.create_window(150, 140, window=entry2)

labelmonth = tkinter.Label(window, text= 'Enter any month (0-12)' ,font=('helvetica', 10))
canvas.create_window(110, 110, window=labelmonth)

#day
entry3 = tkinter.Entry (window)
canvas.create_window(250, 70, window=entry3)

labelday = tkinter.Label(window, text= 'Enter a day' ,font=('helvetica', 10))
canvas.create_window(220, 40, window=labelday)





def getDay(d, m, y):
    y = entry1.get()
    x1 = int(y)
    m = entry2.get()
    x2 = int(m)
    d = entry3.get()
    x3 = int(d)

    t = [ 0, 3, 2, 5, 0, 3,
          5, 1, 4, 6, 2, 4 ]
    y -= m < 3
    return (( x1   int(x1 / 4) - int(x1 / 100)
               int(x1 / 400)   t[x2 - 1]   x3) % 7)
 

    day = getDay(x3, x2, x1)
    print(day)


button1 = tkinter.Button(text= 'submit', command=getDay)
canvas.create_window(200, 180, window=button1)

I keep getting this error

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
TypeError: getDay() missing 3 required positional arguments: 'd', 'm', and 'y'

how do i resolve the error? I have tried adding arguments in lots of places and cant figure out what the error even means. anyways i dont know what else to say but my post is mostly code so it makes me add more details

CodePudding user response:

When you are passing get_day function as a command in the Button section so at that time it only call function and you do not pass any arguments to it so basically you have to use partial from functools import the partial and you should have to pass the command using the partial like as

button1 = tkinter.Button(text='submit', command=partial(get_day,x1,x2,x3)) 

CodePudding user response:

Your function is defined to require three parameters:

def getDay(d, m, y):

Like the error says, you're calling it with zero parameters here, since you didn't specify any arguments as part of the definition of command:

button1 = tkinter.Button(text= 'submit', command=getDay)

If you want to be able to call the function both with or without parameters, make them optional:

def getDay(d=None, m=None, y=None):
    d = d if d is not None else int(entry3.get())
    m = m if m is not None else int(entry2.get())
    y = y if y is not None else int(entry1.get())

    ...

Or, never require a parameter and always get the values from the entry widgets:

def getDay():
    d = int(entry3.get())
    m = int(entry2.get())
    y = int(entry1.get())

    ...
  • Related