Python Forum
[Tkinter] Button command getting TypeError: radsa() missing 3 required positional arguments
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Button command getting TypeError: radsa() missing 3 required positional arguments
#1
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()
Reply
#2
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)
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Button 'command' Argument Confusion gw1500se 11 5,718 Nov-11-2021, 08:45 PM
Last Post: menator01
  [Kivy] Type error:takes 1 positional argument but 2 required hammer 3 2,595 Nov-09-2021, 06:01 AM
Last Post: deanhystad
  Get name of command button Heyjoe 3 2,241 Dec-10-2020, 04:30 AM
Last Post: deanhystad
  Class function does not create command button Heyjoe 2 2,230 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  [Tkinter] Command button, then more command buttons Heyjoe 4 2,820 Aug-08-2020, 11:28 AM
Last Post: Yoriz
  [Tkinter] button command tkinter Heyjoe 6 5,007 Jul-30-2020, 07:06 PM
Last Post: deanhystad
  Button Command Heyjoe 4 2,306 Jul-20-2020, 01:45 AM
Last Post: Heyjoe
  tkinter button not accessing the command when clicked jhf2 1 3,505 Nov-23-2019, 10:17 PM
Last Post: DT2000
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,952 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] command button automaticaly fired kferhat 2 2,475 Jun-16-2019, 08:21 PM
Last Post: kferhat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020