我的邏輯/ if/else語句似乎有問題!問題在於如果我輸入了錯誤的密碼可以說但是正確的用戶名,什麼都沒有發生,對於學生和老師來說都是類似的,我只是不確定要改變什麼。謝謝。其他語句問題,tkinter,python
錯誤:
File "/Users/Sebastian/Desktop/DQS/login.py", line 43, in _login_btn_clickked
if (student_usernames.index(username) == student_passwords.index(password)):
ValueError: tuple.index(x): x not in tuple
from tkinter import *
import tkinter.messagebox as tm
class LoginFrame(Frame):
def __init__(self, master):
super().__init__(master)
self.label_1 = Label(self, text="Username")
self.label_2 = Label(self, text="Password")
self.entry_1 = Entry(self)
self.entry_2 = Entry(self, show="*")
self.label_1.grid(row=0, sticky=E)
self.label_2.grid(row=1, sticky=E)
self.entry_1.grid(row=0, column=1)
self.entry_2.grid(row=1, column=1)
self.checkbox = Checkbutton(self, text="Keep me logged in")
self.checkbox.grid(columnspan=2)
self.logbtn = Button(self, text="Login", command = self._login_btn_clickked)
self.logbtn.grid(columnspan=2)
self.pack()
def _login_btn_clickked(self):
#print("Clicked")
username = self.entry_1.get()
password = self.entry_2.get()
#print(username, password)
student_usernames = ("C100", "C200", "C300")
student_passwords = ("PASS", "PASS1", "PASS2")
teacher_usernames = ("T100", "T200", "T300")
teacher_passwords = ("TPASS", "TPASS1", "TPASS3")
if username in student_usernames:
if (student_usernames.index(username) == student_passwords.index(password)):
tm.showinfo("Login info", "Welcome Student")
else:
tm.showerror("Login error", "Incorrect information")
elif username in teacher_usernames:
if (teacher_usernames.index(username) == teacher_passwords.index(password)):
tm.showinfo("Login info", "Welcome Teacher")
else:
tm.showerror("Login error", "Incorrect information")
else:
tm.showerror("Login error", "Incorrect information")
root = Tk()
lf = LoginFrame(root)
root.mainloop()
你可以發佈你測試它的shell輸出嗎? – Tommy
'student_passwords.index(密碼)'假定''password'實際存在於'student_passwords'中。你可以使用'如果用戶名在student_usernames和密碼在student_passwords:'中,或者用'try/except'包圍整個塊, – jDo