如何自动通知用户他们忘记了 select 某项操作?
How do I automatically notify a user that they forgot to select an action?
我正在创建一个登录系统,但我遇到了一个我不知道如何处理的错误,情况是这样的:我希望每个用户都能够在使用加密和不使用加密之间做出选择加密。例如,某人输入了正确的登录信息,但他忘记了 select 消息类型,当此人按下 Enter 按钮时,他们会收到一个错误消息,即他们忘记了 select消息类型。我该如何实施?代码如下:
from tkinter import messagebox
from tkinter import *
window = Tk()
window.title('Login')
window.geometry('320x200')
window.resizable(True, True)
name = StringVar()
password = StringVar()
def crypt():
r = (lis.get(lis.curselection()))
c = (lis.get(lis.curselection()))
string_name = name.get()
string_password = password.get()
#r = (lis.get(lis.curselection()))
#c = (lis.get(lis.curselection()))
if string_name == 'John':
if string_password == '6789':
if r == 'Use encrypted':
window.after(1000, lambda: window.destroy())
print('Hello.')
if string_name == 'John':
if string_password == '6789':
if r == 'Use decrypted':
window.after(1000, lambda: window.destroy())
print('Hello bro!')
if string_name not in 'John':
messagebox.showerror('Error', 'Error')
elif string_password not in '6789':
messagebox.showerror('Error', 'Error')
elif r not in r:
messagebox.showerror('Error', 'Oops, please crypt message') #This Error
elif string_name == 'John':
messagebox.showerror('Error', 'Error')
elif string_password == '6789':
messagebox.showerror('Error', 'Error')
entry = Entry(window, textvariable=name, width=10)
entry.grid(column=1, pady=7, padx=4)
label = Label(window, text='Enter name: ')
label.grid(row=0, padx=1)
entry1 = Entry(window, textvariable=password, width=10, show='*')
entry1.grid(column=1, pady=7, padx=2)
label1 = Label(window, text='Enter password: ')
label1.grid(row=1, padx=1)
listbox = Listbox(window, selectmode=SINGLE, width=12, height=2)
listbox.grid(column=1, row=2, pady=7, padx=2)
r = ['Use encrypted']
c = ['Use decrypted']
lis = Listbox(window, selectmode=SINGLE, width=10, height=2)
lis.grid(column=1, row=2, pady=7, padx=2)
for i in r:
lis.insert(END, i)
for i in c:
lis.insert(END, i)
label_crypto = Label(window, text='Encrypted/decrypted message: ', bg='black', fg='red')
label_crypto.grid(row=2)
button = Button(window, text='Enter', command=crypt)
button.grid(pady=30)
window.mainloop()
正如我在评论中所建议的那样,改进变量的名称可以更好地区分它们。
下面的代码使用 try-catch 块来检测用户没有从列表框中选择项目。如果您尝试在未选择的情况下从列表中获取所选项目,Tkinter 将抛出错误。
from tkinter import messagebox
from tkinter import *
import _tkinter
window = Tk()
window.title('Login')
window.geometry('320x200')
window.resizable(True, True)
name = StringVar()
password = StringVar()
def crypt():
try:
user_encryption_selection = (encryption_listbox.get(encryption_listbox.curselection()))
except _tkinter.TclError:
messagebox.showerror('Error','User has not selected an encryption type')
return
string_name = name.get()
string_password = password.get()
if string_name == 'John':
if string_password == '6789':
if user_encryption_selection == 'Use decrypted':
window.after(1000, lambda: window.destroy())
print('Hello bro!')
else:
messagebox.showerror('Error', 'Error Password')
else:
messagebox.showerror('Error', 'Invalid Username')
entry = Entry(window, textvariable=name, width=10)
entry.grid(column=1, pady=7, padx=4)
label = Label(window, text='Enter name: ')
label.grid(row=0, padx=1)
entry1 = Entry(window, textvariable=password, width=10, show='*')
entry1.grid(column=1, pady=7, padx=2)
label1 = Label(window, text='Enter password: ')
label1.grid(row=1, padx=1)
listbox = Listbox(window, selectmode=SINGLE, width=12, height=2)
listbox.grid(column=1, row=2, pady=7, padx=2)
encryption_options = ['Use encrypted','Use decrypted']
encryption_listbox = Listbox(window, selectmode=SINGLE, width=10, height=2)
encryption_listbox.grid(column=1, row=2, pady=7, padx=2)
for i in encryption_options:
encryption_listbox.insert(END, i)
label_crypto = Label(window, text='Encrypted/decrypted message: ', bg='black', fg='red')
label_crypto.grid(row=2)
button = Button(window, text='Enter', command=crypt)
button.grid(pady=30)
window.mainloop()
我还删除了一些不必要的代码。您的目标应该是只检查一次 username/password/encryption 值,而不是在单独的 if/elif/else 条件
中多次检查
我正在创建一个登录系统,但我遇到了一个我不知道如何处理的错误,情况是这样的:我希望每个用户都能够在使用加密和不使用加密之间做出选择加密。例如,某人输入了正确的登录信息,但他忘记了 select 消息类型,当此人按下 Enter 按钮时,他们会收到一个错误消息,即他们忘记了 select消息类型。我该如何实施?代码如下:
from tkinter import messagebox
from tkinter import *
window = Tk()
window.title('Login')
window.geometry('320x200')
window.resizable(True, True)
name = StringVar()
password = StringVar()
def crypt():
r = (lis.get(lis.curselection()))
c = (lis.get(lis.curselection()))
string_name = name.get()
string_password = password.get()
#r = (lis.get(lis.curselection()))
#c = (lis.get(lis.curselection()))
if string_name == 'John':
if string_password == '6789':
if r == 'Use encrypted':
window.after(1000, lambda: window.destroy())
print('Hello.')
if string_name == 'John':
if string_password == '6789':
if r == 'Use decrypted':
window.after(1000, lambda: window.destroy())
print('Hello bro!')
if string_name not in 'John':
messagebox.showerror('Error', 'Error')
elif string_password not in '6789':
messagebox.showerror('Error', 'Error')
elif r not in r:
messagebox.showerror('Error', 'Oops, please crypt message') #This Error
elif string_name == 'John':
messagebox.showerror('Error', 'Error')
elif string_password == '6789':
messagebox.showerror('Error', 'Error')
entry = Entry(window, textvariable=name, width=10)
entry.grid(column=1, pady=7, padx=4)
label = Label(window, text='Enter name: ')
label.grid(row=0, padx=1)
entry1 = Entry(window, textvariable=password, width=10, show='*')
entry1.grid(column=1, pady=7, padx=2)
label1 = Label(window, text='Enter password: ')
label1.grid(row=1, padx=1)
listbox = Listbox(window, selectmode=SINGLE, width=12, height=2)
listbox.grid(column=1, row=2, pady=7, padx=2)
r = ['Use encrypted']
c = ['Use decrypted']
lis = Listbox(window, selectmode=SINGLE, width=10, height=2)
lis.grid(column=1, row=2, pady=7, padx=2)
for i in r:
lis.insert(END, i)
for i in c:
lis.insert(END, i)
label_crypto = Label(window, text='Encrypted/decrypted message: ', bg='black', fg='red')
label_crypto.grid(row=2)
button = Button(window, text='Enter', command=crypt)
button.grid(pady=30)
window.mainloop()
正如我在评论中所建议的那样,改进变量的名称可以更好地区分它们。
下面的代码使用 try-catch 块来检测用户没有从列表框中选择项目。如果您尝试在未选择的情况下从列表中获取所选项目,Tkinter 将抛出错误。
from tkinter import messagebox
from tkinter import *
import _tkinter
window = Tk()
window.title('Login')
window.geometry('320x200')
window.resizable(True, True)
name = StringVar()
password = StringVar()
def crypt():
try:
user_encryption_selection = (encryption_listbox.get(encryption_listbox.curselection()))
except _tkinter.TclError:
messagebox.showerror('Error','User has not selected an encryption type')
return
string_name = name.get()
string_password = password.get()
if string_name == 'John':
if string_password == '6789':
if user_encryption_selection == 'Use decrypted':
window.after(1000, lambda: window.destroy())
print('Hello bro!')
else:
messagebox.showerror('Error', 'Error Password')
else:
messagebox.showerror('Error', 'Invalid Username')
entry = Entry(window, textvariable=name, width=10)
entry.grid(column=1, pady=7, padx=4)
label = Label(window, text='Enter name: ')
label.grid(row=0, padx=1)
entry1 = Entry(window, textvariable=password, width=10, show='*')
entry1.grid(column=1, pady=7, padx=2)
label1 = Label(window, text='Enter password: ')
label1.grid(row=1, padx=1)
listbox = Listbox(window, selectmode=SINGLE, width=12, height=2)
listbox.grid(column=1, row=2, pady=7, padx=2)
encryption_options = ['Use encrypted','Use decrypted']
encryption_listbox = Listbox(window, selectmode=SINGLE, width=10, height=2)
encryption_listbox.grid(column=1, row=2, pady=7, padx=2)
for i in encryption_options:
encryption_listbox.insert(END, i)
label_crypto = Label(window, text='Encrypted/decrypted message: ', bg='black', fg='red')
label_crypto.grid(row=2)
button = Button(window, text='Enter', command=crypt)
button.grid(pady=30)
window.mainloop()
我还删除了一些不必要的代码。您的目标应该是只检查一次 username/password/encryption 值,而不是在单独的 if/elif/else 条件
中多次检查