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