Tkinter app=App(root) problem

S

snoylr

This is my first day working with Tkinter. I'm using python2.3 on
WindowsXP. I found this program and entered it.

from Tkinter import *

class App:
def _init_(self,master):
frame = Frame(master)
frame.pack()

self.button = Button(frame, text = "Quit", fg = "red", command
=frame.quit)
self.button.pack(side=LEFT)

self.hi_there = Button(frame, text = "Hello", command =
self.say_hi)
self.hi_there.pack(side=LEFT)

def say_hi(self):
print "hi there, everyone!"

root = Tk()
app = App(root)
root.mainloop()

When I run the code in IDLE I get the initial frame but no buttons with
the following error:

Traceback (most recent call last):
File "C:\Documents and Settings\INTERNET\Desktop\hello2.py", line 18,
in ?
app = App(root)
TypeError: this constructor takes no arguments

I have also tried to save the program as with a pyw extension. Nothing
happens when I try to run it with that extension.
 
S

Steve Holden

This is my first day working with Tkinter. I'm using python2.3 on
WindowsXP. I found this program and entered it.

from Tkinter import *

class App:
def _init_(self,master):
^^^^^^

This should be __init__ (the underscores should be doubled). Python
"magic methods" are all named in this way.
frame = Frame(master)
frame.pack()

self.button = Button(frame, text = "Quit", fg = "red", command
=frame.quit)
self.button.pack(side=LEFT)

self.hi_there = Button(frame, text = "Hello", command =
self.say_hi)
self.hi_there.pack(side=LEFT)

def say_hi(self):
print "hi there, everyone!"

root = Tk()
app = App(root)
root.mainloop()

When I run the code in IDLE I get the initial frame but no buttons with
the following error:

Traceback (most recent call last):
File "C:\Documents and Settings\INTERNET\Desktop\hello2.py", line 18,
in ?
app = App(root)
TypeError: this constructor takes no arguments

I have also tried to save the program as with a pyw extension. Nothing
happens when I try to run it with that extension.
That's not strictly true. Nothing *appears* to happen, because the same
error message is produced but there's no windows console to display it in.

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top