Python Forum
[Tkinter] Button command getting TypeError: radsa() missing 3 required positional arguments - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Button command getting TypeError: radsa() missing 3 required positional arguments (/thread-17744.html)



Button command getting TypeError: radsa() missing 3 required positional arguments - nonzzo - Apr-22-2019

what is the problem?
Error:
TypeError: radsa() missing 3 required positional arguments: 'guessesTaken', 'randnummer', and 'gæt'
import tkinter
import math
import random
import time
from tkinter import *
r = tkinter.Tk()
tk = Tk()
f = Frame(tk)
r.title("userinterface")
r.minsize(width=600, height=400)
# r.configure(background='gray49')



navn = Entry(r, width=10)
alder = Entry(r, width=10)

navn.place(x=180,y=100)
alder.place(x=180,y=120)

aldertekst = alder.get()

label3 = Label(r, text="år")
label3.place(x=240, y=120)

# def b(Event):
#     def destroy():
#         f.destroy()
label2 = Label(r, text="navn?")
label2.place(x=140, y=100)
label1 = Label(r, text="alder?")
label1.place(x=140, y=120)
def radsa(guessesTaken, randnummer, gæt):
            w = Label(r, text=guessesTaken)
            w.place(relx=0.0, rely=0.0, anchor="nw") 
            print ("wreeeeeee")
            guessesTaken = guessesTaken + 1
            gættet = int(gæt.get())
            print (gættet)
            if gættet < randnummer:

                valueofgeusnumb = "for lavt" 
            if gættet > randnummer:
                valueofgeusnumb = "for højt"
                print('for højt.')
            if gættet == randnummer:
                # guessesTaken = str(guessesTaken)
                valueofgeusnumb = 'nice ' + navn + '! du gættede mit nummer med ' + guessesTaken + ' guesses!'
                
            if guessesTaken > 6:
                print ("done")   
            if gættet != randnummer:
                print ("nej")
            print (valueofgeusnumb)

def viderfunction1():
    
    print("ooh")
    label1.place_forget()
    
    label2.place_forget()
    navn.place_forget()
    alder.place_forget()
    label3.place_forget()
    b.place_forget()
    navntekst = navn.get()
   
    
    guessesTaken = 0
    
    
    randnummer = random.randint(1,61)
    
    
   
    
    
    
        
    
    l = Button(r, text="gæt2", command=radsa, compound=CENTER)
    l.place(relx=0.5, rely=0.5, anchor=CENTER)    
    # 
    #     break
        
    # if guessesTaken > 6:
    #     break
        
    # 
    # if gættet != randnummer:
    #     randnummer = str(randnummer)
    #     print('det var det dog ikke mit nummer var ' + randnummer + ' bedre held næste gang')
    #     time.sleep(5)
        
    #print ("næste opgave")
            

        

        
   
    gættal = Label(r, text='okay ' + navntekst +'')
    gættal.place(x=280, y=100)
    gættal2 = Label(r, text='før du kan komme vider skal du gætte et tal mellem 1 og 60 og du har 6 forsøg')
    gættal2.place(x=100, y=120)
    gættal3 = Label(r, text='hvad gætter du?')
    gættal3.place(x=250, y=140)
    gæt = Entry(r, width=4)
    
    gæt.place(x=280, y=160)

    
    
    
    # navn = input("Navn?: ")
    # alder = input("alder?: ") 
    # køn = input("kvinde eller mand?: ")
    
    
    
    
    
b = Button(r, text="vider", command=viderfunction1, compound=CENTER)
b.place(relx=0.5, rely=0.5, anchor=CENTER)
r.mainloop()



RE: TypeError: radsa() missing 3 required positional arguments - Yoriz - Apr-22-2019

The function radsa has been made to take 3 arguments guessesTaken, randnummer, gæt
In the following
l = Button(r, text="gæt2", command=radsa, compound=CENTER)
makes it call radsa when the button is pressed but it will call it with no arguments
you can make another function for command to call that calls radsa with the appropriate arguments
def call_radsa:
    guessesTaken = #code for this
    randnummer = #code for this
    gæt = #code for this
    radsa(guessesTaken, randnummer, gæt)
l = Button(r, text="gæt2", command=call_radsa, compound=CENTER)



RE: TypeError: radsa() missing 3 required positional arguments - Larz60+ - Apr-22-2019

l = Button(r, text="gæt2", command=radsa, compound=CENTER)
line 81, command will call radsa without arguments, thus causing the error.

you need to pass arguments:
There are a couple of ways to do this, the cleanest is with partial
1. With use of partial:
  • At top of script, add from functools import partial
  • Then before button definition, add:
       pcommand = partial(radsa, guessesTaken, randnummer, gæt):
       l = Button(r, text="gæt2", command=pcommand, compound=CENTER)
       
  • This is untested but should work

The other is using lambda:
    l = Button(r, text="gæt2", command=lambda: radsa(guessesTaken, randnummer, gæt), compound=CENTER)
In either case, you need to make sure argument values are available when called.