2010-03-10 43 views
2
#!/usr/bin/python 
# -*- coding: iso-8859-1 -*- 

import Tkinter 

class simpleapp_tk(Tkinter.Tk): 
    def __init__(self,parent): 
     Tkinter.Tk.__init__(self,parent) 
     self.parent=parent 
    def initialize(self): 
     self.grid() 

     self.entry=Tkinter.Entry(self) 
     self.entry.grid(column=0,row=0,sticky='EW') 
     self.entry.bind("<Return>",self.OnPressEnter) 

     button=Tkinter.Button(self,test="Post it!",command=self.OnButtonClick) 
     button.grid(column=1,row=0) 

     label=Tkinter.Label(self,anchor="w",fg="white",bg="blue") 
     label=grid(column=0,row=1,columnspan=2,sticky='EW') 

     self.grid_columnconfigure(0,weight=1) 

    def OnButtonClick(self): 
     print "you clicked the button!" 

    def OnPressEnter(self,event): 
     print "you pressed enter!" 

if __name__=="__main__": 
    app=simpleapp_tk(None) 
    app.title('poster') 
    app.mainloop() 

我編寫了這個程序,以便創建一個框來輸入文本和按鈕,但它除了窗口外沒有任何顯示。錯誤在哪裏?在使用tkinter的python編程中的錯誤

回答

4

主要問題是你忘記打電話給app.initialize(),但你也有幾個錯別字。我已經指出了這個固定版本中的評論。

import Tkinter 

class simpleapp_tk(Tkinter.Tk): 
    def __init__(self,parent): 
     Tkinter.Tk.__init__(self,parent) 
     self.parent=parent 
    def initialize(self): 
     self.grid() 

     self.entry=Tkinter.Entry(self) 
     self.entry.grid(column=0,row=0,sticky='EW') 
     self.entry.bind("<Return>",self.OnPressEnter) 

     button=Tkinter.Button(self,text="Post it!",command=self.OnButtonClick) 
     # the text keyword argument was mis-typed as 'test' 

     button.grid(column=1,row=0) 

     label=Tkinter.Label(self,anchor="w",fg="white",bg="blue") 
     label.grid(column=0,row=1,columnspan=2,sticky='EW') 
     # the . in label.grid was mis-typed as '=' 

     self.grid_columnconfigure(0,weight=1) 

    def OnButtonClick(self): 
     print "you clicked the button!" 

    def OnPressEnter(self,event): 
     print "you pressed enter!" 

if __name__=="__main__": 
    app=simpleapp_tk(None) 
    app.title('poster') 
    app.initialize() # you forgot this 
    app.mainloop()