我對 python 和 tkinter 很陌生。我正在嘗試構建下面的 GUI 界面,這是一個護膚程式,它將根據從 4 個下拉選單中選擇的結果推薦產品。最后一個按鈕 - 幫助將顯示聯系資訊。
我正處于起步階段,無法顯示年齡的第一個選項選單。任何人都可以解釋一下這個原因嗎?我該如何做到這一點,以便將年齡、皮膚型別、主要皮膚問題和清潔美容的答案組合起來,從而產生由于組合而推薦的產品?
import tkinter as tk
age_menu = [
"<=18",
"19 - 25",
"25 - 34",
"35 - 45"
]
# root window
root = tk.Tk()
root.geometry("800x500")
root.title("SAVE MY SKIN")
root.configure(bg = "#32402f")
greeting = tk.Label(root, text = "Hello!", fg = "#faf2e9",
bg = "#32402f",font = ("Courier",20,"bold"),
anchor = "w", width = 60, padx = 30,
pady = 40
).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 = 300, justify = "left",
font = ("Courier",20), anchor = "w",
width = 60, padx = 30, pady = 20
).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 = 180
).grid(row = 2, column = 0, sticky = tk.W)
var = tk.StringVar()
var.set("Age")
ageclicked = tk.OptionMenu(root,var,*age_menu)
ageclicked.config(fg = "#faf2e9", bg = "#32402f",width = 90,
font = ("Helvetica",12), anchor = "e")
ageclicked.grid(row = 2,column = 1)
root.mainloop()

uj5u.com熱心網友回復:
所以我遇到的第一個問題是格式化,所以我重寫了它以遵循
此示例將顯示您想要我在評論中的意思。我洗掉了很多width引數,因為你真的不需要它。我還添加了root.resizable(False, False)設定永久視窗大小。有時這對于簡單的 GUI 來說更容易。我還洗掉了行和列配置,因為我控制了視窗大小。我也對視窗大小進行了微調。
import tkinter as tk
age_menu = [
"<=18",
"19 - 25",
"25 - 34",
"35 - 45"
]
# root window
root = tk.Tk()
root.geometry("750x450")
root.title("SAVE MY SKIN")
root.configure(bg="#32402f")
root.resizable(False, False) # stops the user from maximizing the window
#might not need or want the row and column config this way
#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)
greeting.grid(row=1, 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=300, justify="left",
font=("Courier", 20), anchor="w",
padx=20, pady=20)
instructions.grid(row=2, 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", padx=20)
disclaimer.grid(row=3, column=0, sticky=tk.W, ipadx=20)#ipadx=20 added for spacing
var = tk.StringVar()
var.set("Age")
ageclicked = tk.OptionMenu(root, var, *age_menu) #removed width parameter
ageclicked.config(fg="#faf2e9", bg="#32402f",
font=("Helvetica", 12), anchor="e", padx=10) #pad the pixel by 10 title will center itself
ageclicked.grid(row=3, column=1)
root.mainloop()

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/479819.html
上一篇:使用滑鼠在框架中移動放大的影像
