LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-21-2006, 11:52 PM   #1
jojotx0
Member
 
Registered: Mar 2004
Distribution: Debian Lenny
Posts: 181

Rep: Reputation: 30
Error in basic button response program in Python 2.4 with the Tkinter module [SOLVED]


Today I started using Tkinter with Python 2.4, I have been reading tutorials, and running example programs and looking at the code used, but I have a small problem when I run code of my own.

Code:
from Tkinter import *
import tkMessageBox

class Application(Frame):

	def __init__( self, master):
		Frame.__init__( self, master )
		self.grid()
		self.create_widget()

		
def create_widget( self ):
	self.bttn = Button( self )
	self.bttn["text"] = "Click Me!"
	self.bttn["command"] = self.respond_to_click
	self.bttn.grid()

	
def respond_to_click( self ):
	tkMessageBox.showinfo( title = "Response", message="You clicked me!")

	
if __name__ == '__main__':

	root = Tk()
	root.title( "Button Response" )
	root.geometry( "200x100" )
	myApp = Application(root)
	root.mainloop()
I get an error of:
Code:
Traceback (most recent call last):
  File "/****/****/****/*****/test.pyw", line 28, in -toplevel-
    myApp = Application(root)
  File "/****/****/****/*****/test.pyw", line 9, in __init__
    self.create_widget()
AttributeError: Application instance has no attribute 'create_widget'
If I take out the "root" in "myApp = Application(root)" I get this error:
Code:
Traceback (most recent call last):
  File "/****/****/*****/*****/test.pyw", line 28, in -toplevel-
    myApp = Application()
TypeError: __init__() takes exactly 2 arguments (1 given)
Any ideas?

****Error messages edited taking out file path****

Last edited by jojotx0; 05-23-2006 at 09:36 PM.
 
Old 05-23-2006, 07:43 PM   #2
jojotx0
Member
 
Registered: Mar 2004
Distribution: Debian Lenny
Posts: 181

Original Poster
Rep: Reputation: 30
thanks to know ones help but my own the below code works.

Code:
from Tkinter import *
import tkMessageBox

class Application(Frame):

    def __init__( self, master):
	Frame.__init__( self, master )
	self.grid()
	self.create_widget()

		
    def create_widget( self ):
	self.bttn = Button( self )
	self.bttn["text"] = "Click Me!"
	self.bttn["command"] = self.respond_to_click
	self.bttn.grid()

	
    def respond_to_click( self ):
	tkMessageBox.showinfo( title = "Response", message="You clicked me!")

	
if __name__ == '__main__':

	root = Tk()
	root.title( "Button Response" )
	root.geometry( "200x100" )
	myApp = Application(root)
	root.mainloop()
apparently the idle-python2.4 IDE adds 2 tab spaces if you use
"class Application(Frame):"
and then
"def __init__( self, master):"
making everything 1 tab to far over, causing everything to screw up
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error installing TKinter with Python 2.4.3 jojotx0 Linux - Software 1 05-21-2006 03:10 PM
tkinter integration to python 2.3.4 gaddargarson Programming 1 03-09-2005 04:58 AM
Python and Tkinter 1337 Twinkie Fedora 2 08-03-2004 11:35 AM
Python: Tkinter. Chu Programming 0 11-10-2003 01:56 AM
Python and Tkinter Error Gerardoj Programming 1 11-08-2003 09:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration