2016-10-06 7 views
-1

私はAレベルコンピューティングの学生です。私はクラス内でPythonを使用してコードを作成できる唯一の人です。私の先生でさえ言語を学んだことはありません。私は、情報が正しく入力されたときに終了するログインプログラムをコーディングしようとしています。また、ウェルカムスクリーンイメージを表示しています(まだその部分をコーディングしていません)。 3回のログイン試行に失敗すると、失敗メッセージを閉じて表示する必要があります。関連するif/elif文に基づいてtkinterウィンドウを終了/終了させるだけでなく、多くの失敗したログイン後にelif文が動作するように試行変数を変更しようとすると、多くの論理エラーが発生しました。これはうまくいかず、私はこのサイトの多くのコード例を見てきましたが、何も見つかりませんでした。コードを修正して助けてください。ボタンなしでtkinterウィンドウを閉じる方法は?

コード:最初のテストが実行された後、ログイン資格情報は、コード正しければそう

if user_get == 'octo' and pass_get == 'burger': 
elif user_get != 'octo' or pass_get != 'burger': 

from tkinter import * #Importing graphics 

attempts = 0 #Defining attempts variable 

def OperatingProgram(): #Defining active program 

    class Application(Frame): 
     global attempts 
     def __init__(self,master): 
      super(Application, self).__init__(master) #Set __init__ to the master class 
      self.grid() 
      self.InnerWindow() #Creates function 

     def InnerWindow(self): #Defining the buttons and input boxes within the window 
      global attempts 
      print("Booted log in screen") 
      self.title = Label(self, text=" Please log in, you have " + str(attempts) + " incorrect attempts.") #Title 
      self.title.grid(row=0, column=2) 

      self.user_entry_label = Label(self, text="Username: ") #Username box 
      self.user_entry_label.grid(row=1, column=1) 

      self.user_entry = Entry(self)      #Username entry box 
      self.user_entry.grid(row=1, column=2) 

      self.pass_entry_label = Label(self, text="Password: ") #Password label 
      self.pass_entry_label.grid(row=2, column=1) 

      self.pass_entry = Entry(self)      #Password entry box 
      self.pass_entry.grid(row=2, column=2) 

      self.sign_in_butt = Button(self, text="Log In",command = self.logging_in) #Log in button 
      self.sign_in_butt.grid(row=5, column=2) 

     def logging_in(self): 
      global attempts 
      print("processing") 
      user_get = self.user_entry.get() #Retrieve Username 
      pass_get = self.pass_entry.get() #Retrieve Password 

      if user_get == 'octo' and pass_get == 'burger': #Statement for successful info 
       import time 
       time.sleep(2)  #Delays for 2 seconds 
       print("Welcome!") 
       QuitProgram() 
      elif user_get != 'octo' or pass_get != 'burger': #Statement for any failed info 
       if attempts >= 2: #Statement if user has gained 3 failed attempts 
        import time 
        time.sleep(2) 
        print("Sorry, you have given incorrect details too many times!") 
        print("This program will now end itself") 
        QuitProgram() 
       else:    #Statement if user still has enough attempts remaining 
        import time 
        time.sleep(2) 
        print("Incorrect username, please try again") 
        attempts += 1 
      else:     #Statement only exists to complete this if statement block 
       print("I don't know what you did but it is very wrong.") 

    root = Tk() #Window format 
    root.title("Log in screen") 
    root.geometry("320x100") 

    app = Application(root) #The frame is inside the widget 
    root.mainloop() #Keeps the window open/running 

def QuitProgram(): #Defining program termination 
    import sys 
    sys.exit() 

OperatingProgram() 
+0

プログラムコードが更新され、試行ベースのステートメントが修正されました。しかし、tkウィンドウに表示された試行回数は変更されず、正確な情報や3回の試行失敗後に自動的にtkウィンドウを閉じる方法はわかりません。 –

答えて

1

瞬間のためにあなたのlogging_in方法で次の2行を考えてみましょう。それらが正しくない場合、2回目のテストの後のコードが実行されます。

しかし、あなたは複数の障害後に実行を見たいコードは第3のテスト句の下にされています事を第一またはいずれかとして、実行のスレッドは、このテストを見ることはありません、ある

elif attempts >= 3: 

2番目のものは既にtrueに評価されています(ログイン資格は正しいです、ログイン資格は間違っています)。試行の値がチェックされる前に、両方をfalseに評価する必要があります。

この問題を解決する最も簡単な方法は、else節/あなたはそれが必要であると感じた場合、新しいものを追加することを調整

if attempts >= 3: 

を読むためにあなたの

elif attempts >= 3: 

行を変更することです。

+0

また、 'attempts + 1'は' attempts + = 1'でなければなりません。 – acw1668

+0

こんにちは@L.Fabryこの回答があなたの質問を解決した場合、チェックマークをクリックして[受諾](http://meta.stackexchange.com/q/5234/179419)を検討してください。これは、あなたが解決策を見つけ出し、回答者とあなた自身の両方に評判を与えていることを広範なコミュニティに示します。これを行う義務はありません。 – Lafexlos

関連する問題