2015-11-14 57 views
-2

在Python 2.7.10程序上工作,對於密碼gui,但它給出應用程序未定義的錯誤。我不確定是否類應用程序(框架)或應用程序(應用程序)(根)有問題,因爲它要求我定義應用程序,但我不明白爲什麼他們會問我。Python-2.7.10密碼GUI不起作用

from Tkinter import * 

class Application(Frame): 

    def _init_(self, master): 
     Frame._init_(self,master) 
     self.grid() 
     self.create_widgets() 

    def create_widgets(self): 
     self.instruction = Label(self, text = "Enter the password") 
     self.instruction.grid(row = 0, column = 0, columnspan = 2, sticky = W) 
     self.password = Entry(self) 
     self.password.grid(row = 1, column = 1, sticky = W) 
     self.submit_button = Button(self, text = "Submit", commmand = self.reveal) 
     self.submit_button.grid(row = 2, column = 0, sticky = W) 
     self.text = Text(self, width = 35, height = 5, wrap = WORD) 
     self.text.grid(row = 3, column = 0, columnspan = 2, sticky = W) 
    def reveal(self): 
     content = self.password.get() 

     if content == "password": 
      message = "You have the order" 

     else: 
      message = "Access denied." 
      self.text.delete(0.0, END) 
      self.text.insert(0.0, message) 

root = Tk() 
root.title("Password") 
root.geometry("250x150") 
app= Application(root) 

root.mainloop() 
+0

您的縮進不正確。 'def'與'class Application(Frame)'具有相同的縮進級別。無論是否修復縮進問題,此代碼都不會給出您所說的錯誤。請給我們可運行的代碼,它實際上會產生你說的錯誤。 –

+0

'__init__'中應該有'_' – furas

+0

我建議在'app'和'='之間加一個空格。另外,你可以發佈你得到的_exact_錯誤嗎? –

回答

0

,我已經修復你的代碼,這就是:

from Tkinter import * 

class Application(Frame): 

    def __init__(self, master): 
     Frame.__init__(self,master) 
     self.grid() 
     self.create_widgets() 

    def create_widgets(self): 
     self.instruction = Label(self, text = "Enter the password") 
     self.instruction.grid(row = 0, column = 0, columnspan = 2, sticky = W) 
     self.password = Entry(self) 
     self.password.grid(row = 1, column = 1, sticky = W) 
     self.submit_button = Button(self, text = "Submit", command = self.reveal) 
     self.submit_button.grid(row = 2, column = 0, sticky = W) 
     self.text = Text(self, width = 35, height = 5, wrap = WORD) 
     self.text.grid(row = 3, column = 0, columnspan = 2, sticky = W) 

    def reveal(self): 

     content = self.password.get() 

     if content == "password": 
      message = "You have the order" 

     else: 
      message = "Access denied." 

     self.text.delete(0.0, END) 
     self.text.insert(0.0, message) 

root = Tk() 
root.title("Password") 
root.geometry("250x150") 
app= Application(root) 

root.mainloop() 

你的問題是:

  • 您使用_init_代替__init__
  • 錯字的 「條命令」,而不是「命令」
  • 最後兩行程序顯示在應用程序中被錯誤縮進(邏輯錯誤,否t語法或運行時間)

真的不太清楚你的問題是誠實的,我只是在Python 3.4.3中做了這些修改,並且進行了修改,以便在Python 2.7.10中運行。我希望有所幫助。

+1

感謝隊友,工作很棒! –

+0

如果你喜歡它,你可以+1我的回答(請):D –