user3927455
user3927455

Reputation: 19

Tkinter gets typeerror in main method

I have a Tkinter application which produces me an error, typeerror takes exactly 3 arguements

Traceback (most recent call last):
  File "C:\Python34\aa2.py", line 62, in <module>
    app = simpleapp_tk(None)          # it provides th class name to be connected
TypeError: __init__() takes exactly 3 arguments (2 given)
>>>

My coding:

import Tkinter
class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent,master):
        Tkinter.Tk.__init__(self,parent,master)
        self.parent = parent
        self.master=master
        self.initialize()                # connects the initialise method
        self.create_widgets()

    def initialize(self):
        self.grid()

        self.entryVariable = Tkinter.StringVar() # makes the entry 
        self.entry1 = Tkinter.Entry(self,textvariable1=self.entryVariable)
        self.entry1.grid(column=0,row=0,sticky='EW')       # connects the grid to the entry
        self.entry1.bind("<Return>", self.OnPressEnter)
        self.entryVariable.set(u"Enter text here.") 
        button = Tkinter.Button(self,text=u"Click me !",                                                               #OUTER CONNECTION(done after inner connections)
                                command=self.OnButtonClick) # connects to the OnbuttonClick method for button click    #
        button.grid(column=1,row=0)          # connects the grid to the button


        self.labelVariable = Tkinter.StringVar()         # makes the label
        label = Tkinter.Label(self,textvariable1=self.labelVariable,
                              anchor="w",fg="white",bg="blue")
        label.grid(column=0,row=1,columnspan=2,sticky='EW') # connects the grid to the label
        self.labelVariable.set(u"Hello !")

        self.grid_columnconfigure(0,weight=1)
        self.resizable(True,False)
        self.update()
        self.geometry(self.geometry())       
        self.entry1.focus_set()
        self.entry1.selection_range(0, Tkinter.END)

    def create_widgets(self):         
        btn1 = Button(self.master, text = "I ")
        btn1.pack()

    def OnButtonClick(self):
        self.labelVariable.set( self.entryVariable.get()+" (You clicked the button)" ) # label variable is connected to the entryvariable #INNER CONNECTION(done within when doing the assignment
        self.entry1.focus_set()
        self.entry1.selection_range(0, Tkinter.END)       

    def OnPressEnter(self,event):
        self.labelVariable.set( self.entryVariable.get()+" (You pressed ENTER)" )     # label variable is connected to the entryvariable
        self.focus_set()
        self.entry1.selection_range(0, Tkinter.END)

if __name__ == "__main__":        # sets the name for the gui that is going to be creitated
    app = simpleapp_tk(None)          # it provides th class name to be connected
    app.title('my application')             # it will set the title
    app.mainloop()

Please help me to rectify my code!Answers will be appreciated!I have an application ,now my main method has to be modified!

Upvotes: 0

Views: 115

Answers (2)

skrrgwasme
skrrgwasme

Reputation: 9633

Your answer is right there in your traceback. This line:

app = simpleapp_tk(None)

...Is passing None to your init routine that is defined like this:

def __init__(self,parent,master):

When the simpleapp_tk() object is created, the implicit self is passed along with None; those are the two arguments reported in the traceback, while the init routine expects three arguments. You need to modify that hardcoded line so that you are also passing in parent and master when you create the simpleapp_tk() object.

Here is an example of a trivially simple program lifted straight from the tkinter reference:

import Tkinter as tk   

class Application(tk.Frame):          
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)  
        self.grid()                    
        self.createWidgets()

    def createWidgets(self):
        self.quitButton = tk.Button(self, text='Quit',
        command=self.quit)  
        self.quitButton.grid() 

app = Application()
app.master.title('Sample application')
app.mainloop()

The fact that you have that much code written while still having issues with your init routine suggests to me that you wrote a lot of code without doing any intermediate debugging or checking for functionality. When programming, it is generally better to make small incremental changes while making sure each change works before moving on to the next. Try starting with just this example GUI to get your base set up, then slowly add the components from your existing code and make sure each new component works as you add them.

Upvotes: 1

jcfollower
jcfollower

Reputation: 3158

near the end, you have ...

app = simpleapp_tk(None)

(with only one parameter) but your init function needs another parameter (parent and master).

So, should it be ...

app = simpleapp_tk(None, None)

Upvotes: 0

Related Questions