Bottom Page

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()

Quote
#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)
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  tkinter button not accessing the command when clicked jhf2 1 242 Nov-23-2019, 10:17 PM
Last Post: DT2000
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 579 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] command button automaticaly fired kferhat 2 412 Jun-16-2019, 08:21 PM
Last Post: kferhat
  Error TypeError: KeyboardSwitch() missing 8 required positional arguments: 'msg', 'vk Camember_beret_baguette 1 1,091 Dec-11-2018, 08:09 PM
Last Post: Larz60+
  [Tkinter] how to assign a command to a button ChickenWithRice 2 1,366 Dec-08-2018, 09:35 PM
Last Post: ChickenWithRice
  [Tkinter] Tkinter newly created button does not execute command ChipsSlave 7 1,922 Jul-25-2018, 01:01 AM
Last Post: JUANCARLOS
  [Tkinter] Selected radio button in push button in Tkinter prashantfunde91 1 7,184 Jun-22-2017, 05:27 PM
Last Post: DeaD_EyE
  [Tkinter] Arguments in function for button command kristrek 14 11,743 Jan-11-2017, 09:16 PM
Last Post: kristrek

Forum Jump:


Users browsing this thread: 1 Guest(s)