我正在嘗試創建以下 tkinter 程式:
- 從 4 個選項選單中的每一個中選擇一個選項。
- 點擊提交
- 字典中的值,我在其中設定了鍵以匹配提交旁邊的選項列印組合。
例如,用戶從選項中選擇“18-29,干燥,痤瘡,是”。我在一個單獨的 python 檔案中有一個包含 200 對的字典。其中一對是:'18-29,干燥,痤瘡,是的':'Cetaphil'
列印的值應該是 Cetaphil。但是,什么都沒有列印。我究竟做錯了什么?
import tkinter as tk
from rp import recommendedproducts
age_menu = [
"18-29",
"30-49",
"50-81",
]
skintype_menu = [
"dry",
"combination",
"normal",
"oily",
"sensitive"
]
mainskinconcern_menu = [
"acne",
"anti-ageing",
"dehydration",
"pigmentation",
"sensitivity/redness"
]
cleanbeauty_menu = [
"Yes",
"No"
]
# root window
root = tk.Tk()
root.geometry("600x580")
root.title("SAVE MY SKIN")
root.configure(bg = "#32402f")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
greeting = tk.Label(root, text = "Hello!", fg = "#faf2e9", bg = "#32402f",
font = ("Courier",20,"bold"),
anchor = "w",
padx = 30,
pady = 10)
greeting.grid(row = 0, column = 0, sticky = tk.W)
instructions = tk.Label(root, text = "Fill in your details to get a list of "
"product recommendations perfect for your specific "
"skincare needs!",
fg = "#faf2e9",
bg = "#32402f",
wraplength = 400,
justify = "left",
font = ("Courier",20),
anchor = "w",
width = 60,
padx = 30,
pady = 10)
instructions.grid(row = 1, column = 0, sticky = tk.W)
disclaimer = tk.Label(root,text = "This is not intended as a subsititute "
"for professional medical advice and should not be "
"relied on as health or personal advice. Always seek "
"the guidance of your doctor or other qualified health "
"professional with any questions you may have "
"regarding your health or a medical condition.",
fg = "#faf2e9",
bg = "#32402f",
wraplength = 500,
justify = "left",
font = ("Helvetica Neue",10),
anchor = "w",
width = 100,
padx = 30,
pady = 20)
disclaimer.grid(row = 10, column = 0, sticky = tk.W)
ageclicked = tk.StringVar()
ageclicked.set("age")
agedropdown = tk.OptionMenu(root,ageclicked,*age_menu)
agedropdown.config(fg = "#faf2e9",
bg = "#32402f",
font = ("helvetica neue",20),
anchor = "e",
width = 27)
agedropdown.grid(row = 2, column = 0, padx = 25, pady = 10, sticky = tk.W)
stclicked = tk.StringVar()
stclicked.set("skin type")
stdropdown = tk.OptionMenu(root,stclicked,*skintype_menu)
stdropdown.config(fg = "#faf2e9",
bg = "#32402f",
font = ("helvetica neue",20),
anchor = "e",
width = 27)
stdropdown.grid(row = 4, column = 0, padx = 25, pady = 10, sticky = tk.W)
mscclicked = tk.StringVar()
mscclicked.set("main skin concern")
mscdropdown = tk.OptionMenu(root,mscclicked,*mainskinconcern_menu)
mscdropdown.config(fg = "#faf2e9",
bg = "#32402f",
font = ("helvetica neue",20),
anchor = "e",
width = 27)
mscdropdown.grid(row = 6, column = 0, padx = 25, pady = 10, sticky = tk.W)
cbclicked = tk.StringVar()
cbclicked.set("clean beauty")
cbdropdown = tk.OptionMenu(root,cbclicked,*cleanbeauty_menu)
cbdropdown.config(fg = "#faf2e9",
bg = "#32402f",
font = ("Helvetica Neue",20),
anchor = "e",
width = 27)
cbdropdown.grid(row = 7, column = 0, padx = 25, pady = 10, sticky = tk.W)
def helptext():
print("Contact [email protected] for more information.")
def userresult():
userage = ageclicked.get()
userst = stclicked.get()
usermsc = mscclicked.get()
usercb = cbclicked.get()
print(userage,",", userst,",",usermsc,",",usercb)
def userrp():
if userresult in recommendedproducts:
print(recommendedproducts[userresult])
def show():
result = tk.Label(root, text = userrp()).grid(row = 8, column = 1, sticky = tk.W)
helpButton = tk.Button(
text = "HELP",
width = 27,
highlightbackground = "#32402f",
fg = "#faf2e9",
font = ("avenir next condensed", 20, "bold"),
command = helptext
)
helpButton.grid(row = 9, column = 0, padx = 25, pady = 10, sticky = tk.W)
submitButton = tk.Button(
text = "SUBMIT",
width = 27,
highlightbackground = "#32402f",
fg = "#faf2e9",
font = ("avenir next condensed", 20, "bold"),
command = show
)
submitButton.grid(row = 8, column = 0, padx = 25, pady = 10, sticky = tk.W)
root.mainloop()
字典
recommendedproducts = {
'18-29, dry, acne, Yes': 'Cetaphil',
'18-29, dry, acne, No': 'Cetaphil',
'18-29, dry, anti-ageing, Yes': 'Cetaphil',
'18-29, dry, anti-ageing, No': 'Cetaphil',
'18-29, dry, dehydration, Yes': 'Cetaphil',
'18-29, dry, dehydration, No': 'Cetaphil',
'18-29, dry, pigmentation, Yes': 'Cetaphil',
'18-29, dry, pigmentation, No': 'Cetaphil',
'18-29, dry, sensitivity/redness, Yes': 'Cetaphil',
'18-29, dry, sensitivity/redness, No': 'Cetaphil',
'18-29, combination, acne, Yes': 'Cetaphil',
'18-29, combination, acne, No': 'Cetaphil',
'18-29, combination, anti-ageing, Yes': 'Cetaphil',
'18-29, combination, anti-ageing, No': 'Cetaphil',
'18-29, combination, dehydration, Yes': 'Cetaphil',
'18-29, combination, dehydration, No': 'Cetaphil',
'18-29, combination, pigmentation, Yes': 'Cetaphil',
'18-29, combination, pigmentation, No': 'Cetaphil',
'18-29, combination, sensitivity/redness, Yes': 'Cetaphil',
'18-29, combination, sensitivity/redness, No': 'Cetaphil',
'18-29, normal, acne, Yes': 'Cetaphil',
'18-29, normal, acne, No': 'Cetaphil',
'18-29, normal, anti-ageing, Yes': 'Cetaphil',
'18-29, normal, anti-ageing, No': 'Cetaphil',
'18-29, normal, dehydration, Yes': 'Cetaphil',
'18-29, normal, dehydration, No': 'Cetaphil',
'18-29, normal, pigmentation, Yes': 'Cetaphil',
'18-29, normal, pigmentation, No': 'Cetaphil',
'18-29, normal, sensitivity/redness, Yes': 'Cetaphil',
'18-29, normal, sensitivity/redness, No': 'Cetaphil',
'18-29, oily, acne, Yes': 'Cetaphil',
'18-29, oily, acne, No': 'Cetaphil',
'18-29, oily, anti-ageing, Yes': 'Cetaphil',
'18-29, oily, anti-ageing, No': 'Cetaphil',
'18-29, oily, dehydration, Yes': 'Cetaphil',
'18-29, oily, dehydration, No': 'Cetaphil',
'18-29, oily, pigmentation, Yes': 'Cetaphil',
'18-29, oily, pigmentation, No': 'Cetaphil',
'18-29, oily, sensitivity/redness, Yes': 'Cetaphil',
'18-29, oily, sensitivity/redness, No': 'Cetaphil',
'18-29, sensitive, acne, Yes': 'Cetaphil',
'18-29, sensitive, acne, No': 'Cetaphil',
'18-29, sensitive, anti-ageing, Yes': 'Cetaphil',
'18-29, sensitive, anti-ageing, No': 'Cetaphil',
'18-29, sensitive, dehydration, Yes': 'Cetaphil',
'18-29, sensitive, dehydration, No': 'Cetaphil',
'18-29, sensitive, pigmentation, Yes': 'Cetaphil',
'18-29, sensitive, pigmentation, No': 'Cetaphil',
'18-29, sensitive, sensitivity/redness, Yes': 'Cetaphil',
'18-29, sensitive, sensitivity/redness, No': 'Cetaphil',
'30-49, dry, acne, Yes': 'Cetaphil',
'30-49, dry, acne, No': 'Cetaphil',
'30-49, dry, anti-ageing, Yes': 'Cetaphil',
'30-49, dry, anti-ageing, No': 'Cetaphil', '30-49, dry, dehydration, Yes': 'Cetaphil', '30-49, dry, dehydration, No': 'Cetaphil', '30-49, dry, pigmentation, Yes': 'Cetaphil', '30-49, dry, pigmentation, No': 'Cetaphil', '30-49, dry, sensitivity/redness, Yes': 'Cetaphil', '30-49, dry, sensitivity/redness, No': 'Cetaphil', '30-49, combination, acne, Yes': 'Cetaphil', '30-49, combination, acne, No': 'Cetaphil', '30-49, combination, anti-ageing, Yes': 'Cetaphil', '30-49, combination, anti-ageing, No': 'Cetaphil', '30-49, combination, dehydration, Yes': 'Cetaphil', '30-49, combination, dehydration, No': 'Cetaphil', '30-49, combination, pigmentation, Yes': 'Cetaphil', '30-49, combination, pigmentation, No': 'Cetaphil', '30-49, combination, sensitivity/redness, Yes': 'Cetaphil', '30-49, combination, sensitivity/redness, No': 'Cetaphil', '30-49, normal, acne, Yes': 'Cetaphil', '30-49, normal, acne, No': 'Cetaphil', '30-49, normal, anti-ageing, Yes': 'Cetaphil', '30-49, normal, anti-ageing, No': 'Cetaphil', '30-49, normal, dehydration, Yes': 'Cetaphil', '30-49, normal, dehydration, No': 'Cetaphil', '30-49, normal, pigmentation, Yes': 'Cetaphil', '30-49, normal, pigmentation, No': 'Cetaphil', '30-49, normal, sensitivity/redness, Yes': 'Cetaphil', '30-49, normal, sensitivity/redness, No': 'Cetaphil', '30-49, oily, acne, Yes': 'Cetaphil', '30-49, oily, acne, No': 'Cetaphil', '30-49, oily, anti-ageing, Yes': 'Cetaphil', '30-49, oily, anti-ageing, No': 'Cetaphil', '30-49, oily, dehydration, Yes': 'Cetaphil', '30-49, oily, dehydration, No': 'Cetaphil', '30-49, oily, pigmentation, Yes': 'Cetaphil', '30-49, oily, pigmentation, No': 'Cetaphil', '30-49, oily, sensitivity/redness, Yes': 'Cetaphil', '30-49, oily, sensitivity/redness, No': 'Cetaphil', '30-49, sensitive, acne, Yes': 'Cetaphil', '30-49, sensitive, acne, No': 'Cetaphil', '30-49, sensitive, anti-ageing, Yes': 'Cetaphil', '30-49, sensitive, anti-ageing, No': 'Cetaphil', '30-49, sensitive, dehydration, Yes': 'Cetaphil', '30-49, sensitive, dehydration, No': 'Cetaphil', '30-49, sensitive, pigmentation, Yes': 'Cetaphil', '30-49, sensitive, pigmentation, No': 'Cetaphil', '30-49, sensitive, sensitivity/redness, Yes': 'Cetaphil', '30-49, sensitive, sensitivity/redness, No': 'Cetaphil', '50-81, dry, acne, Yes': 'Cetaphil', '50-81, dry, acne, No': 'Cetaphil', '50-81, dry, anti-ageing, Yes': 'Cetaphil', '50-81, dry, anti-ageing, No': 'Cetaphil', '50-81, dry, dehydration, Yes': 'Cetaphil', '50-81, dry, dehydration, No': 'Cetaphil', '50-81, dry, pigmentation, Yes': 'Cetaphil', '50-81, dry, pigmentation, No': 'Cetaphil', '50-81, dry, sensitivity/redness, Yes': 'Cetaphil', '50-81, dry, sensitivity/redness, No': 'Cetaphil', '50-81, combination, acne, Yes': 'Cetaphil', '50-81, combination, acne, No': 'Cetaphil', '50-81, combination, anti-ageing, Yes': 'Cetaphil', '50-81, combination, anti-ageing, No': 'Cetaphil', '50-81, combination, dehydration, Yes': 'Cetaphil', '50-81, combination, dehydration, No': 'Cetaphil', '50-81, combination, pigmentation, Yes': 'Cetaphil', '50-81, combination, pigmentation, No': 'Cetaphil', '50-81, combination, sensitivity/redness, Yes': 'Cetaphil', '50-81, combination, sensitivity/redness, No': 'Cetaphil', '50-81, normal, acne, Yes': 'Cetaphil', '50-81, normal, acne, No': 'Cetaphil', '50-81, normal, anti-ageing, Yes': 'Cetaphil', '50-81, normal, anti-ageing, No': 'Cetaphil', '50-81, normal, dehydration, Yes': 'Cetaphil', '50-81, normal, dehydration, No': 'Cetaphil', '50-81, normal, pigmentation, Yes': 'Cetaphil', '50-81, normal, pigmentation, No': 'Cetaphil', '50-81, normal, sensitivity/redness, Yes': 'Cetaphil', '50-81, normal, sensitivity/redness, No': 'Cetaphil', '50-81, oily, acne, Yes': 'Cetaphil', '50-81, oily, acne, No': 'Cetaphil', '50-81, oily, anti-ageing, Yes': 'Cetaphil', '50-81, oily, anti-ageing, No': 'Cetaphil', '50-81, oily, dehydration, Yes': 'Cetaphil', '50-81, oily, dehydration, No': 'Cetaphil', '50-81, oily, pigmentation, Yes': 'Cetaphil', '50-81, oily, pigmentation, No': 'Cetaphil', '50-81, oily, sensitivity/redness, Yes': 'Cetaphil', '50-81, oily, sensitivity/redness, No': 'Cetaphil', '50-81, sensitive, acne, Yes': 'Cetaphil', '50-81, sensitive, acne, No': 'Cetaphil', '50-81, sensitive, anti-ageing, Yes': 'Cetaphil', '50-81, sensitive, anti-ageing, No': 'Cetaphil', '50-81, sensitive, dehydration, Yes': 'Cetaphil', '50-81, sensitive, dehydration, No': 'Cetaphil', '50-81, sensitive, pigmentation, Yes': 'Cetaphil', '50-81, sensitive, pigmentation, No': 'Cetaphil', '50-81, sensitive, sensitivity/redness, Yes': 'Cetaphil', '50-81, sensitive, sensitivity/redness, No': 'Cetaphil'
}
將函式 userrp() 編輯為:
def userrp():
userresult()
print(recommendedproducts[userresult])
導致錯誤:
18-29, dry, acne, Yes
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
return self.func(*args)
File "/Users/xx/checki.py", line 153, in show
result = tk.Label(root, text = userrp(), bg = '#32402f', fg = '#faf2e9').grid(row = 8, column = 1, sticky = tk.E)
File "/Users/xx/checki.py", line 149, in userrp
print(recommendedproducts[userresult])
KeyError: <function userresult at 0x7ff01fd3c9d0>

uj5u.com熱心網友回復:
您忘記對userresult().
if userresult in recommendedproducts在這里,您正在檢查函式物件是否是字典中的鍵recommendedproducts。因此,正在發生的事情可以與此進行比較:
def foo(): pass
dict1 = {"a": 1, foo: 2}
dict2 = {"a": 1, "b": foo}
print(foo in dict1) # True
print(foo in dict2) # False
編輯:
在再次查看代碼之后,發生了幾件事情,看起來很明顯,預期的代碼與正在執行的代碼不同。
讓我們從結構開始,然后向外作業。
目前這是嵌套函式呼叫的流程:
show()-> userrp()->userresult()
def userresult():
userage = ageclicked.get()
userst = stclicked.get()
usermsc = mscclicked.get()
usercb = cbclicked.get()
print(userage,",", userst,",",usermsc,",",usercb)
userresult()不回傳任何內容,默認情況下此函式將回傳None
if userresult()然后將與 相同,
if None換句話說,此 if 條件將永遠不會True與當前代碼一起回傳。
所以讓我們修復那個:
def userresult():
userage = ageclicked.get()
userst = stclicked.get()
usermsc = mscclicked.get()
usercb = cbclicked.get()
# We must return this string, so it will be used whenever the function is called.
return f"{userage}, {userst}, {usermsc}, {usercb}"
現在我們可以使用 if 條件的回傳值:
def userrp():
if userresult() in recommendedproducts:
# We want this function to return as well.
return recommendedproducts[userresult()]
此功能現在應該可以正常作業,或按預期作業。
def show():
result = tk.Label(root, text = userrp()).grid(row = 8, column = 1, sticky = tk.W)
我制作了一個代碼片段,希望能闡明嵌套函式、列印(內置函式)和回傳陳述句是如何作業的。
def foo():
print("foo")
# This function does not return anything and will return a NoneType object
def bar():
return "bar"
foo() # Prints foo.
bar() # Prints nothing.
print(foo()) # Prints foo, then prints None
print(bar()) # Prints bar
x = foo() # x = None, and prints foo
y = bar() # y = "bar"
print(x) # Prints None
print(y) # Prints bar
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/479603.html
上一篇:Python中不同長度的字典比較
