Python Forum
What is positional argument self?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is positional argument self?
#19
Actually we've explained this over and over and over. One last try.

The error message could not be any more descriptive.

The function conversionF(self) has an argument named "self". When you press the conversion button you call conversionF() without passing any arguments. Your function call is "missing 1 required positional argument".

But this is not the error. This error is one of two things. Either conversionF() is supposed to be a method of class Temperature, or it is supposed to be a standalone function.

Either your program should look more like this:
import tkinter as tk
 
class Temperature:
    """All classes should have a docstring describing their purpose"""
    def __init__(self):
        self.valeur = 0 

    # Indentation is important.  Needs to be indented to same level as __init__()
    def conversionF(self):
        """Methods should also have docstrings"""
        fahrenheit = float((9 * self.valeur) / 5 + 32)
        return resultat.configure(text=str(fahrenheit))
    
    def conversionC(self):
        """To describe what they do"""
        celcius = float((self.valeur - 32) * 5 / 9)
        return resultat.configure(text=str(celcius))
 
# This is not part of class temperature because it is indented the same level as "class Temperature"
fen1 = tk.Tk()
matemp = Temperature()
entree = tk.Entry(fen1).pack()
resultat = tk.Label(fen1, text='')
resultat.pack()
tk.Button(fen1, text='Conversion', command=matemp.conversionF).pack()
fen1.mainloop()
Or it should look more like this:
import tkinter as tk
 
# Treating this as a function.  There is no temperature class anymore
def conversionF():
    """Methods should also have docstrings"""
    fahrenheit = float((9 * valuer) / 5 + 32)
    return resultat.configure(text=str(fahrenheit))

def conversionC():
    """To describe what they do"""
    celcius = float((valuer - 32) * 5 / 9)
    return resultat.configure(text=str(celcius))
 
 # These should not all be global variables
fen1 = tk.Tk()
valuer = 0
entree = tk.Entry(fen1).pack()
resultat = tk.Label(fen1, text='')
resultat.pack()
tk.Button(fen1, text='Conversion', command=conversionF).pack()
fen1.mainloop()
Both programs convert 0 to something. Both programs are still riddled with logic errors.
Reply


Messages In This Thread
What is positional argument self? - by Frankduc - Mar-04-2022, 02:09 PM
RE: What is positional argument self? - by Frankduc - Mar-04-2022, 04:40 PM
RE: What is positional argument self? - by Frankduc - Mar-04-2022, 04:47 PM
RE: What is positional argument self? - by Frankduc - Mar-04-2022, 05:05 PM
RE: What is positional argument self? - by ndc85430 - Mar-04-2022, 09:04 PM
RE: What is positional argument self? - by Frankduc - Mar-04-2022, 11:18 PM
RE: What is positional argument self? - by ndc85430 - Mar-05-2022, 07:26 AM
RE: What is positional argument self? - by Frankduc - Mar-05-2022, 08:27 PM
RE: What is positional argument self? - by deanhystad - Mar-05-2022, 09:38 PM
RE: What is positional argument self? - by Frankduc - Mar-05-2022, 10:50 PM
RE: What is positional argument self? - by ndc85430 - Mar-05-2022, 11:35 PM
RE: What is positional argument self? - by Frankduc - Mar-06-2022, 01:18 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: Diagram.render() takes 1 positional argument but 2 were given sachin1361 0 319 Apr-23-2024, 06:39 AM
Last Post: sachin1361
  Error: _vhstack_dispatcher() takes 1 positional argument but 9 were given alexfrol86 3 6,024 May-09-2022, 12:49 PM
Last Post: deanhystad
  positional argument: 'self' mcmxl22 8 3,521 Dec-13-2021, 10:11 PM
Last Post: deanhystad
  TypeError: run_oracle_job() missing 1 required positional argument: 'connection_strin python_student 1 2,029 Aug-06-2021, 08:05 PM
Last Post: SheeppOSU
  TypeError: sum() missing 1 required positional argument: 'num2' Insen 3 5,624 Jan-06-2021, 04:25 PM
Last Post: Insen
  TypeError: forward() missing 1 required positional argument: 'x' sveto4ka 4 12,475 Jun-17-2020, 07:25 PM
Last Post: sveto4ka
  missing 1 required positional argument: 'self' yasser 7 11,787 Jun-07-2020, 06:48 AM
Last Post: ndc85430
  TypeError: _linspace_dispatcher() missing 1 required positional argument: 'stop' Ae_Lovecraft 3 8,729 May-28-2020, 03:33 PM
Last Post: Larz60+
  SyntaxError: positional argument follows keyword argument syd_jat 3 5,972 Mar-03-2020, 08:34 AM
Last Post: buran
  Type error: dump() missing 1 required positional argument: fp jaycuff13 2 22,317 Jul-13-2019, 10:21 AM
Last Post: jaycuff13

Forum Jump:

User Panel Messages

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