karltheneale
Posts: 4
Joined: Tue Dec 09, 2014 11:05 am

working through Python programming

Wed Dec 10, 2014 9:03 am

Hi, I've been working through some tasks and keep getting an error can anyone help please?

Code: Select all

from tkinter import *

class App:

    def _init_(self, master):
        frame = Frame(master)
        frame.pack()
        Label (frame, text='deg C').grid(row=0, column=0)
        button = Button(frame, text='Convert', command=self.convert)
        button.grid(row=1)

    def convert(self):
        print('Not Implemented')

root = Tk()
root.wm_title('Temp Converter')
app = App(root)
root.mainloop()
error
Traceback (most recent call last):
File "C:/Users/Steve/Desktop/Python/Hello.py", line 17, in <module>
app = App(root)
TypeError: object() takes no parameters
Last edited by karltheneale on Wed Dec 10, 2014 9:39 am, edited 2 times in total.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: working through Python programming

Wed Dec 10, 2014 9:14 am

Please use tags around your code, otherwise it's impossible to read.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: working through Python programming

Wed Dec 10, 2014 9:26 am

ame wrote:Please use tags around your code, otherwise it's impossible to read.
+1

But from what we can see -

Code: Select all

File "C:/Users/Steve/Desktop/Python/Hello.py", line 17, in <module>
app = App(root)
TypeError: object() takes no parameters
app is going to be an instance of the class App. And you have defined App to have no parameters. So the error message is telling you that you have provided a parameter ( root ) that it can't use.

Whether you fix this in the class definition or at the place where it is called depends on what you want to achieve.

karltheneale
Posts: 4
Joined: Tue Dec 09, 2014 11:05 am

Re: working through Python programming

Wed Dec 10, 2014 9:43 am

This works fine

Code: Select all

from tkinter import *

class MyApp:                         ### (1)
	def __init__(self, myParent):      ### (1a)
		self.myContainer1 = Frame(myParent)
		self.myContainer1.pack()
		
		self.button1 = Button(self.myContainer1) 
		self.button1["text"]= "Hello, World!"     
		self.button1["background"] = "green"      
		self.button1.pack()	                       
		
root = Tk()
myapp = MyApp(root)  ### (2)
root.mainloop()      ### (3)
i don't understand why this would work and the other doesn't

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: working through Python programming

Wed Dec 10, 2014 9:49 am

Your "init" has a single underscore. The working example has two.

karltheneale
Posts: 4
Joined: Tue Dec 09, 2014 11:05 am

Re: working through Python programming

Wed Dec 10, 2014 9:55 am

ame wrote:Your "init" has a single underscore. The working example has two.
brilliant reply thank you

Return to “Python”