我是一个高级计算机专业的学生,我是班上唯一一个会用Python编写代码的人。连我的老师都没学过这门语言。我正在尝试编写一个登录程序,当信息被正确输入并显示欢迎屏幕图像时退出(我还没有对该部分进行编码)。它必须关闭并在3次失败的登录尝试后显示失败消息。在多次登录失败后,尝试更改尝试变量使elif语句工作,以及根据相关的if/elif语句终止/关闭tkinter窗口时,我遇到了许多逻辑错误。这是失败的工作,我已经在这个网站上看了许多代码示例,找不到任何东西,我可以得到一些帮助来修复我的代码吗?在
代码: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()