简体   繁体   中英

Syntax error in Python GUI program

I'm trying a simple GUI program in python. I'm kinda beginner in Python GUI programming. Here is the code:

import Tkinter
class simpleapp_tk(Tkinter.Tk):
    def_init_(self,parent):
        Tkinter.Tk._init_(self.parent)
        self.parent = parent    #Self keeps a reference for parent
        self.initialize()

    def intialization(self):
        self.grid()

        #Text widget
        self.entry = Tkinter.Entry(self)
        self.entry.grid(column=0,row=0,sticky='EW')

        #Button widget
        button = Tkinter.Button(self,text='Click meeeee!!')
        button.grid(column=1,row=0)

        #Lable widget
        lable = Tkinter.Lable(self,anchor="w",fg="white",bg="blue")
        lable.grid(column=0,row=1,columnspan=2,stick='EW')
        self.grid_columnfigure(0,weight=1)


    #main function
    if __name__ == '__main__':
        app = simpleapp_tk(None)
        app.title('My app')
        app.mainloop()

When I compile it, it is returning an error like this:

File "secondGUI.py", line 3
    def_init_(self,parent):
                          ^
SyntaxError: invalid syntax

How can i fix it?

As dano said before:

import Tkinter

class simpleapp_tk(Tkinter.Tk):
    def __init__(self, parent):
        Tkinter.Tk.__init__(parent) # <-- self is already here, implicit self

        self.parent = parent    # <-- okay as long as you are in __init__
        self.initialization()

...

Syntax highlighting would have solved your problem ;)

Otherwise I'd suggest PyCharm Studio. Helps a lot

There is just small a mistake and here i have sorted and i tried it and got the output :

import tkinter
class simpleapp_tk(tkinter.Tk):
    def _init_(self,parent):
        tkinter.Tk._init_(self.parent)
        self.parent = parent    #Self keeps a reference for parent
        self.initialize()

    def intialization(self):
        self.grid()

        #Text widget
        self.entry = Tkinter.Entry(self)
        self.entry.grid(column=0,row=0,sticky='EW')

        #Button widget
        button = Tkinter.Button(self,text='Click meeeee!!')
        button.grid(column=1,row=0)

        #Lable widget
        lable = Tkinter.Lable(self,anchor="w",fg="white",bg="blue")
        lable.grid(column=0,row=1,columnspan=2,stick='EW')
        self.grid_columnfigure(0,weight=1)


    #main function
if __name__ == '__main__':
    app = simpleapp_tk(None)
    app.title('My app')
    app.mainloop()

Here you can given some extra space in main function: https://i.stack.imgur.com/uZh9R.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM