AttributeError: 'None' object has no attribute

Joal Heagney s713221 at student.gu.edu.au
Fri Jul 20 23:33:19 EDT 2001


phillip wrote:
> 
> Hi

Hi.

> I have the following code :
> 
> from Tkinter import *
> import Image, ImageTk
> 
> class UI(Label):
> 
>     def __init__(self, master=None):
> 
>         im = Image.open('c:\\tm.gif')
>         self.image = ImageTk.PhotoImage(im)
>         Label.__init__(self, master, image=self.image, bd=0)
>
>     def getSetupValues():
>             return {'Name': 'Phillip'}

Well the above should be "def getSetupValues(self)".

> if __name__ == "__main__":
> 
>     root = Tk()
>     root.geometry('800x600')
>     root.title('Malaysian Web Site Installation')
>     ui = UI(root).pack()

Now this line gives me a lot of problems. What you're doing is creating
an instance of UI, calling it's pack() function and assigning the
returned value of None to ui. What you want is

      ui = UI(root)
      ui.pack()

This wouldn't have mattered, except later you're trying to call ui's
getSetupValues().

>     root.leftFrame = Frame(width=400, height=600, bg='gray50',
> relief=RAISED, bd=4)
>     root.rightFrame = Frame(width=400, height=600, bg='gray50',
> relief=RAISED, bd=4)
>     setup = ui.getSetupValues()
>     for eachKey in root.setup.keys():

*chuckles* Make up your mind. Either setup or root.setup.

>         Label(root.leftFrame, text=eachKey, bg='gray50',
> anchor=E).pack()
>         Label(root.rightFrame, text=setup[eachKey], bg='gray50',
> anchor=E).pack()
>     root.rightFrame.pack(side=RIGHT)
>     root.rightFrame.pack_propagate(0)
>     root.leftFrame.pack(side=LEFT)
>     root.leftFrame.pack_propagate(0)
>     root.mainloop()
> 
> Everything is fine if i put the function in the __init__ function,
> which is not a good idea, I want to keep the code compartmentilized
> nicely.
> 
> any ideas?
> 
> Phill

Pretty good if I do say. Some suggestions:

Instead of putting all this into the "if __init__ == '__main__' section,
you may want to create a class that does all this, with a mainloop() or
run() method that wraps the self.root.mainloop function, 

class Myapp():
	def __init__(self):
		self.root = Tk()
		self.root.geometry('800x600')
		self.root.title('Malaysian Web Site Installation')
		self.root.ui = UI(root)
		self.root.ui.pack()
		self.root.leftFrame = Frame(width=400, height=600, bg='gray50',
relief=RAISED, bd=4)
		self.root.rightFrame = Frame(width=400, height=600, bg='gray50',
relief=RAISED, bd=4)
		self.setup = self.ui.getSetupValues()
		for eachKey in self.setup.keys():
			self.root.leftFrame.label = Label(self.root.leftFrame, text=eachKey,
bg='gray50',
anchor=E)
			self.root.leftFrame.label.pack()
			self.root.rightFrame.label = Label(self.root.rightFrame,
text=setup[eachKey], bg='gray50',anchor=E)
			self.root.rightFrame.label.pack()
# Note that I've embedded label into self.root.rightFrame/etc so I can
access it later if I want to.
		self.root.rightFrame.pack(side=RIGHT)
		self.root.rightFrame.pack_propagate(0)
		self.root.leftFrame.pack(side=LEFT)
		self.root.leftFrame.pack_propagate(0)
	def run(self):
		self.root.mainloop()
and then

if __init__ == '__main__':
	root = Myapp()
	root.run()

This way if there are some non-GUI testing that you want to add, you can
keep it seperate from your other code (which is all GUI-building.).
-- 
      Joal Heagney is: _____           _____
   /\ _     __   __ _    |     | _  ___  |
  /__\|\  ||   ||__ |\  || |___|/_\|___] |
 /    \ \_||__ ||___| \_|! |   |   \   \ !



More information about the Python-list mailing list