LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Error in basic button response program in Python 2.4 with the Tkinter module (http://www.linuxquestions.org/questions/programming-9/error-in-basic-button-response-program-in-python-2-4-with-the-tkinter-module-447236/)

jojotx0 05-22-2006 12:52 AM

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****

jojotx0 05-23-2006 08:43 PM

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


All times are GMT -5. The time now is 06:39 AM.