Python GUI程序中的语法错误

时间:2014-09-18 14:58:27

标签: python python-2.7 tkinter

我在python中尝试一个简单的GUI程序。我是Python GUI编程的初学者。这是代码:

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()

当我编译它时,它会返回如下错误:

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

我该如何解决?

2 个答案:

答案 0 :(得分:0)

正如dano之前所说:

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()

...

语法突出显示可以解决您的问题;)

否则我建议使用PyCharm Studio。帮助很多

答案 1 :(得分:0)

这里有一个小错误,我已经排序并尝试并得到了输出:

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()

这里你可以在 main 函数中给一些额外的空间:https://i.stack.imgur.com/uZh9R.png