Findlay Smith Findlay Smith - 4 years ago 325
Python Question

object.__new__ takes no parameters

I keep getting the following error on a piece of python code:

Traceback (most recent call last):
File"..." <module>
app = App(root)
TypeError: object.__new__() takes no parameters

Here is the code it is referring to:

from tkinter import *
from ScaleAndOffsetConverter import *

class App():
def __init_(self, master):

self.c_var = DoubleVar()
self.t_conv.ScaleAndOffsetConverter("F", "C", 1.8, 32)
frame = Frame(master)
L1 = Label(frame, textvariable = self.result_var)
L2 = Label(frame, text = "Deg C")
E = Entry(frame, textvariable=self.c_var)
L3 = Label( frame, text = "Deg F")
B = Button(frame, text = "convert", command = self.convert)
L2.grid(row=1, column=0)
E.grid(row=0, column=1)
B.grid(row=2, columnspan=2)
self.result_var = DoubleVar

def convert(self):
c = self.c_var.get()

root = Tk()
app = App(root)
root.wm_title("Temp Converter")

I know that similar questions have been posted to this one but i still don't fully understand how to fix this problem - does anybody know why it is appearing or how to fix it?

Answer Source

Your __init__() function is misspelled!


def __init_(self, master): with

def __init__(self, master):

Make sure there are double underbars before and after the word init. These functions are known as "dunder" methods and as you can see the number of underbars does matter!

Python looks for your implementation of __init__ and cannot find it, so it uses Object's __init__, which does nothing but check that no arguments have been passed to it according to this post. This check results in the TypeError you are seeing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download