如何创建自定义字典输出。当前拿起第一个字母并给出输出
How to create custom dictionary output. Currently picks up the first letter and gives output
我正在尝试创建一个简单的首字母缩略词定义 GUI。我大部分时间都有。我的目标是实现两种不同类型的输出。如果用户输入 A,我希望它输出所有以 A 开头的首字母缩写词,这就会发生。例如,当我键入 ACL 时,它会再次带回所有首字母缩略词,而不仅仅是 ACL 首字母缩略词。我怀疑错误或非错误可能来自 .join
from tkinter import *
import tkinter.messagebox
HEIGHT = 400
WIDTH = 700
root = Tk()
root.title("Testing")
a_canvas = Canvas(root, height=HEIGHT, width=WIDTH)
a_canvas.pack()
def clicked():
entered = a_entry.get()
a_textbox.delete(0.0, END)
try:
textin = ", ".join(loop_over_input(entered))
except:
textin = 'Sorry invalid information provided.\nResubmit with the correct information.\n'
a_textbox.insert(0.0, textin)
def clr():
a_entry.delete(0, 'end')
a_textbox.delete(0.0, 'end')
# def gone():
# tkinter.messagebox.showinfo("Goodbye", 'Come back soon.')
# exit()
# Keep this is the master code
def loop_over_input(the_str=''):
master_list = []
for char in the_str:
tmp_char = passwordConversion[char]
master_list.append(tmp_char)
print("Master Pass List: ", master_list)
return master_list
def cont():
tkinter.messagebox.showinfo("About",
'\n Wrote by Me \n Version 1.0 \n Python\Tkinter')
# Custom Dictionary
passwordConversion = {
"A": "AWS - Amazon Web Services \nAmazon ES - Amazon Elasticsearch Service \nAMI - Amazon Machine Image \nAPI - Application Programming Interface \nAI - Artificial Intelligence \nACL - Access Control List \nARN - Amazon Resource Name \nAZ - Availability Zone \nASG - Auto Scaling Group \nAES - Advanced Encryption System \nADFS - Active Directory Federation Service",
"AWS": "Amazon Web Services",
"Amazon ES": "Amazon Elasticsearch Service",
"AMI": "Amazon Machine Image",
"API": "Application Programming Interface",
"AI": "Artificial Intelligence",
"ACL": "Access Control List",
"ARN": "Amazon Resource Name",
"AZ": "Availability Zone",
"ASG": "Auto Scaling Group",
"AES": "Advanced Encryption System",
"ADFS": "Active Directory Federation Service",
}
# Creating the Frames
a_frame = Frame(root, bg='Red', bd=5)
a_frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')
a_middle_frame = Frame(root, bg='White')
a_middle_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.25, anchor='n')
# Creating entry boxes
a_entry_var = StringVar()
a_entry = Entry(a_frame, font=20, textvariable=a_entry_var)
a_entry.place(relwidth=0.65, relheight=1)
# Create a text box
a_textbox = Text(a_middle_frame, font=12)
a_textbox.place(relwidth=1.00, relheight=1)
# Creating button
a_button = Button(a_frame, padx=2, pady=2, text='Get Defination', command=clicked, bg='Silver', font=('Calibri 11 bold'))
a_button.place(relx=0.7, relheight=1, relwidth=0.3)
a_buttons_frame = Frame(root)
a_buttons_frame.place(relx=0.4, rely=0.6)
a_button2 = Button(a_buttons_frame, padx=2, pady=2, text='Clear Fields', font=('Calibri 11 bold'), bg='Silver', command=clr)
a_button2.grid(column=0, row=0, padx=(20, 20))
# a_button3 = Button(a_buttons_frame, padx=2, pady=2, text='Exit', command=gone, bg='Silver', font=('none 18 bold'))
# a_button3.grid(column=1, row=0, padx=(20, 0))
a_label = Label(text="- For Me Only - \n- Contains information for me only", )
a_label.pack(side='bottom')
# Define Menu Items
my_menu = Menu(root)
root.config(menu=my_menu)
# Create Menu Items
file_menu = Menu(my_menu)
my_menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Exit", command=root.quit)
# Create another submenu Edit
edit_menu = Menu(my_menu)
my_menu.add_command(label="About", command=cont)
root.mainloop()
至于我,你在 loop_over_input
中以错误的方式检查它,这会导致显示正确输出的问题。
您应该从字典中获取键并检查每个键是否以用户文本开头 - startswith()
。它解决了错误输出的问题。
然后你不需要 "A": "..."
因为它会找到所有以 "A"
.
开头的项目
当用户写 aws
.
时,我也会使用 .upper()
得到 AWS
def loop_over_input(the_str=''):
master_list = []
for key, value in passwordConversion.items():
if key.upper().startswith(the_str.upper()):
master_list.append(f'{key} - {value}')
print("Master Pass List: ", master_list)
return master_list
因为master_list
有列表所以我在
中使用"\n"
而不是", "
textin = "\n".join(loop_over_input(entered))
具有更好组织代码的完整工作代码。
import tkinter as tk # PEP8: `import *` is not preferred
import tkinter.messagebox
# --- constants --- (PEP8: UPPPER_CASE_NAMES)
HEIGHT = 400
WIDTH = 700
# --- classes --- (PEP8: CamelCaseNames)
# ... empty ...
# --- functions --- (PEP8: lower_case_names)
def clicked():
entered = a_entry.get()
a_textbox.delete(0.0, 'end')
try:
textin = "\n".join(loop_over_input(entered))
except:
textin = 'Sorry invalid information provided.\nResubmit with the correct information.\n'
a_textbox.insert(0.0, textin)
def clr():
a_entry.delete(0, 'end')
a_textbox.delete(0.0, 'end')
# def gone():
# tkinter.messagebox.showinfo("Goodbye", 'Come back soon.')
# exit()
# Keep this is the master code
def loop_over_input(the_str=''):
master_list = []
for key, value in passwordConversion.items():
if key.upper().startswith(the_str.upper()):
master_list.append(f'{key} - {value}')
print("Master Pass List: ", master_list)
return master_list
def cont():
tkinter.messagebox.showinfo("About",
'\n Wrote by Me \n Version 1.0 \n Python\Tkinter')
# --- main --- (PEP8: lower_case_names)
# - data -
# Custom Dictionary
passwordConversion = {
#"A": "AWS - Amazon Web Services \nAmazon ES - Amazon Elasticsearch Service \nAMI - Amazon Machine Image \nAPI - Application Programming Interface \nAI - Artificial Intelligence \nACL - Access Control List \nARN - Amazon Resource Name \nAZ - Availability Zone \nASG - Auto Scaling Group \nAES - Advanced Encryption System \nADFS - Active Directory Federation Service",
"AWS": "Amazon Web Services",
"Amazon ES": "Amazon Elasticsearch Service",
"AMI": "Amazon Machine Image",
"API": "Application Programming Interface",
"AI": "Artificial Intelligence",
"ACL": "Access Control List",
"ARN": "Amazon Resource Name",
"AZ": "Availability Zone",
"ASG": "Auto Scaling Group",
"AES": "Advanced Encryption System",
"ADFS": "Active Directory Federation Service",
}
# - code -
root = tk.Tk()
root.title("Testing")
a_canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
a_canvas.pack()
# Creating the Frames
a_frame = tk.Frame(root, bg='Red', bd=5)
a_frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')
a_middle_frame = tk.Frame(root, bg='White')
a_middle_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.25, anchor='n')
# Creating entry boxes
a_entry_var = tk.StringVar()
a_entry = tk.Entry(a_frame, font=20, textvariable=a_entry_var)
a_entry.place(relwidth=0.65, relheight=1)
# Create a text box
a_textbox = tk.Text(a_middle_frame, font=12)
a_textbox.place(relwidth=1.00, relheight=1)
# Creating button
a_button = tk.Button(a_frame, padx=2, pady=2, text='Get Defination', command=clicked, bg='Silver', font=('Calibri 11 bold'))
a_button.place(relx=0.7, relheight=1, relwidth=0.3)
a_buttons_frame = tk.Frame(root)
a_buttons_frame.place(relx=0.4, rely=0.6)
a_button2 = tk.Button(a_buttons_frame, padx=2, pady=2, text='Clear Fields', font=('Calibri 11 bold'), bg='Silver', command=clr)
a_button2.grid(column=0, row=0, padx=(20, 20))
# a_button3 = tk.Button(a_buttons_frame, padx=2, pady=2, text='Exit', command=gone, bg='Silver', font=('none 18 bold'))
# a_button3.grid(column=1, row=0, padx=(20, 0))
a_label = tk.Label(text="- For Me Only - \n- Contains information for me only", )
a_label.pack(side='bottom')
# Define Menu Items
my_menu = tk.Menu(root)
root.config(menu=my_menu)
# Create Menu Items
file_menu = tk.Menu(my_menu)
my_menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Exit", command=root.quit)
# Create another submenu Edit
edit_menu = tk.Menu(my_menu)
my_menu.add_command(label="About", command=cont)
root.mainloop()
PEP 8 -- Style Guide for Python Code
编辑:
我会使用 regex
来使用更复杂的搜索,即。 I$
获取结尾带有 I
的字符串。
最终我会使用 fnmatch 来搜索星标 - '*I'
或 'A*S'
代码 regex
import re
def loop_over_input(the_str=''):
master_list = []
for key, value in passwordConversion.items():
#if re.match(the_str, key, re.I): # check at the beginning of string
if re.search(the_str, key, re.I): # check in any place of string
master_list.append(f'{key} - {value}')
print("Master Pass List: ", master_list)
return master_list
以S
结尾的所有键
我正在尝试创建一个简单的首字母缩略词定义 GUI。我大部分时间都有。我的目标是实现两种不同类型的输出。如果用户输入 A,我希望它输出所有以 A 开头的首字母缩写词,这就会发生。例如,当我键入 ACL 时,它会再次带回所有首字母缩略词,而不仅仅是 ACL 首字母缩略词。我怀疑错误或非错误可能来自 .join
from tkinter import *
import tkinter.messagebox
HEIGHT = 400
WIDTH = 700
root = Tk()
root.title("Testing")
a_canvas = Canvas(root, height=HEIGHT, width=WIDTH)
a_canvas.pack()
def clicked():
entered = a_entry.get()
a_textbox.delete(0.0, END)
try:
textin = ", ".join(loop_over_input(entered))
except:
textin = 'Sorry invalid information provided.\nResubmit with the correct information.\n'
a_textbox.insert(0.0, textin)
def clr():
a_entry.delete(0, 'end')
a_textbox.delete(0.0, 'end')
# def gone():
# tkinter.messagebox.showinfo("Goodbye", 'Come back soon.')
# exit()
# Keep this is the master code
def loop_over_input(the_str=''):
master_list = []
for char in the_str:
tmp_char = passwordConversion[char]
master_list.append(tmp_char)
print("Master Pass List: ", master_list)
return master_list
def cont():
tkinter.messagebox.showinfo("About",
'\n Wrote by Me \n Version 1.0 \n Python\Tkinter')
# Custom Dictionary
passwordConversion = {
"A": "AWS - Amazon Web Services \nAmazon ES - Amazon Elasticsearch Service \nAMI - Amazon Machine Image \nAPI - Application Programming Interface \nAI - Artificial Intelligence \nACL - Access Control List \nARN - Amazon Resource Name \nAZ - Availability Zone \nASG - Auto Scaling Group \nAES - Advanced Encryption System \nADFS - Active Directory Federation Service",
"AWS": "Amazon Web Services",
"Amazon ES": "Amazon Elasticsearch Service",
"AMI": "Amazon Machine Image",
"API": "Application Programming Interface",
"AI": "Artificial Intelligence",
"ACL": "Access Control List",
"ARN": "Amazon Resource Name",
"AZ": "Availability Zone",
"ASG": "Auto Scaling Group",
"AES": "Advanced Encryption System",
"ADFS": "Active Directory Federation Service",
}
# Creating the Frames
a_frame = Frame(root, bg='Red', bd=5)
a_frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')
a_middle_frame = Frame(root, bg='White')
a_middle_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.25, anchor='n')
# Creating entry boxes
a_entry_var = StringVar()
a_entry = Entry(a_frame, font=20, textvariable=a_entry_var)
a_entry.place(relwidth=0.65, relheight=1)
# Create a text box
a_textbox = Text(a_middle_frame, font=12)
a_textbox.place(relwidth=1.00, relheight=1)
# Creating button
a_button = Button(a_frame, padx=2, pady=2, text='Get Defination', command=clicked, bg='Silver', font=('Calibri 11 bold'))
a_button.place(relx=0.7, relheight=1, relwidth=0.3)
a_buttons_frame = Frame(root)
a_buttons_frame.place(relx=0.4, rely=0.6)
a_button2 = Button(a_buttons_frame, padx=2, pady=2, text='Clear Fields', font=('Calibri 11 bold'), bg='Silver', command=clr)
a_button2.grid(column=0, row=0, padx=(20, 20))
# a_button3 = Button(a_buttons_frame, padx=2, pady=2, text='Exit', command=gone, bg='Silver', font=('none 18 bold'))
# a_button3.grid(column=1, row=0, padx=(20, 0))
a_label = Label(text="- For Me Only - \n- Contains information for me only", )
a_label.pack(side='bottom')
# Define Menu Items
my_menu = Menu(root)
root.config(menu=my_menu)
# Create Menu Items
file_menu = Menu(my_menu)
my_menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Exit", command=root.quit)
# Create another submenu Edit
edit_menu = Menu(my_menu)
my_menu.add_command(label="About", command=cont)
root.mainloop()
至于我,你在 loop_over_input
中以错误的方式检查它,这会导致显示正确输出的问题。
您应该从字典中获取键并检查每个键是否以用户文本开头 - startswith()
。它解决了错误输出的问题。
然后你不需要 "A": "..."
因为它会找到所有以 "A"
.
当用户写 aws
.
.upper()
得到 AWS
def loop_over_input(the_str=''):
master_list = []
for key, value in passwordConversion.items():
if key.upper().startswith(the_str.upper()):
master_list.append(f'{key} - {value}')
print("Master Pass List: ", master_list)
return master_list
因为master_list
有列表所以我在
"\n"
而不是", "
textin = "\n".join(loop_over_input(entered))
具有更好组织代码的完整工作代码。
import tkinter as tk # PEP8: `import *` is not preferred
import tkinter.messagebox
# --- constants --- (PEP8: UPPPER_CASE_NAMES)
HEIGHT = 400
WIDTH = 700
# --- classes --- (PEP8: CamelCaseNames)
# ... empty ...
# --- functions --- (PEP8: lower_case_names)
def clicked():
entered = a_entry.get()
a_textbox.delete(0.0, 'end')
try:
textin = "\n".join(loop_over_input(entered))
except:
textin = 'Sorry invalid information provided.\nResubmit with the correct information.\n'
a_textbox.insert(0.0, textin)
def clr():
a_entry.delete(0, 'end')
a_textbox.delete(0.0, 'end')
# def gone():
# tkinter.messagebox.showinfo("Goodbye", 'Come back soon.')
# exit()
# Keep this is the master code
def loop_over_input(the_str=''):
master_list = []
for key, value in passwordConversion.items():
if key.upper().startswith(the_str.upper()):
master_list.append(f'{key} - {value}')
print("Master Pass List: ", master_list)
return master_list
def cont():
tkinter.messagebox.showinfo("About",
'\n Wrote by Me \n Version 1.0 \n Python\Tkinter')
# --- main --- (PEP8: lower_case_names)
# - data -
# Custom Dictionary
passwordConversion = {
#"A": "AWS - Amazon Web Services \nAmazon ES - Amazon Elasticsearch Service \nAMI - Amazon Machine Image \nAPI - Application Programming Interface \nAI - Artificial Intelligence \nACL - Access Control List \nARN - Amazon Resource Name \nAZ - Availability Zone \nASG - Auto Scaling Group \nAES - Advanced Encryption System \nADFS - Active Directory Federation Service",
"AWS": "Amazon Web Services",
"Amazon ES": "Amazon Elasticsearch Service",
"AMI": "Amazon Machine Image",
"API": "Application Programming Interface",
"AI": "Artificial Intelligence",
"ACL": "Access Control List",
"ARN": "Amazon Resource Name",
"AZ": "Availability Zone",
"ASG": "Auto Scaling Group",
"AES": "Advanced Encryption System",
"ADFS": "Active Directory Federation Service",
}
# - code -
root = tk.Tk()
root.title("Testing")
a_canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
a_canvas.pack()
# Creating the Frames
a_frame = tk.Frame(root, bg='Red', bd=5)
a_frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')
a_middle_frame = tk.Frame(root, bg='White')
a_middle_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.25, anchor='n')
# Creating entry boxes
a_entry_var = tk.StringVar()
a_entry = tk.Entry(a_frame, font=20, textvariable=a_entry_var)
a_entry.place(relwidth=0.65, relheight=1)
# Create a text box
a_textbox = tk.Text(a_middle_frame, font=12)
a_textbox.place(relwidth=1.00, relheight=1)
# Creating button
a_button = tk.Button(a_frame, padx=2, pady=2, text='Get Defination', command=clicked, bg='Silver', font=('Calibri 11 bold'))
a_button.place(relx=0.7, relheight=1, relwidth=0.3)
a_buttons_frame = tk.Frame(root)
a_buttons_frame.place(relx=0.4, rely=0.6)
a_button2 = tk.Button(a_buttons_frame, padx=2, pady=2, text='Clear Fields', font=('Calibri 11 bold'), bg='Silver', command=clr)
a_button2.grid(column=0, row=0, padx=(20, 20))
# a_button3 = tk.Button(a_buttons_frame, padx=2, pady=2, text='Exit', command=gone, bg='Silver', font=('none 18 bold'))
# a_button3.grid(column=1, row=0, padx=(20, 0))
a_label = tk.Label(text="- For Me Only - \n- Contains information for me only", )
a_label.pack(side='bottom')
# Define Menu Items
my_menu = tk.Menu(root)
root.config(menu=my_menu)
# Create Menu Items
file_menu = tk.Menu(my_menu)
my_menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Exit", command=root.quit)
# Create another submenu Edit
edit_menu = tk.Menu(my_menu)
my_menu.add_command(label="About", command=cont)
root.mainloop()
PEP 8 -- Style Guide for Python Code
编辑:
我会使用 regex
来使用更复杂的搜索,即。 I$
获取结尾带有 I
的字符串。
最终我会使用 fnmatch 来搜索星标 - '*I'
或 'A*S'
代码 regex
import re
def loop_over_input(the_str=''):
master_list = []
for key, value in passwordConversion.items():
#if re.match(the_str, key, re.I): # check at the beginning of string
if re.search(the_str, key, re.I): # check in any place of string
master_list.append(f'{key} - {value}')
print("Master Pass List: ", master_list)
return master_list
以S