optionMenu 没有出现在 window tkinter 中
optionMenu does not appear in window tkinter
我对 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()
所以我遇到的第一个问题是格式化,所以我重写了它以遵循 pep8 指南。我只是添加了 root.grid_rowconfigure(0, weight=1)
和 root.grid_columnconfigure(0, weight=1)
以确保您对页面的网格划分对所有部分一视同仁。您的 window 不够大,无法显示信息,所以我将您的 OptionMenu
的宽度缩小了。
import tkinter as tk
age_menu = [
"<=18",
"19 - 25",
"25 - 34",
"35 - 45"
]
# root window
root = tk.Tk()
root.geometry("800x500") #this needs to be expanded to show everything
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",
width=60, padx=30, pady=40)
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=300, justify="left",
font=("Courier", 20), anchor="w",
width=60, padx=30, pady=20)
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=180)
disclaimer.grid(row=2, column=0, sticky=tk.W)
var = tk.StringVar()
var.set("Age")
ageclicked = tk.OptionMenu(root, var, *age_menu) #changed from 90 to 20
ageclicked.config(fg="#faf2e9", bg="#32402f", width=20,
font=("Helvetica", 12), anchor="e")
ageclicked.grid(row=2, column=1)
root.mainloop()
此示例将显示您想要我在评论中的意思。我删除了很多 width
参数,因为您并不真正需要它。我还添加了 root.resizable(False, False)
以设置永久 window 大小。有时这对于简单的 GUI 来说更容易。我还删除了行和列配置,因为我控制了 window 大小。我也对 window 大小进行了微调。
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()
我对 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()
所以我遇到的第一个问题是格式化,所以我重写了它以遵循 pep8 指南。我只是添加了 root.grid_rowconfigure(0, weight=1)
和 root.grid_columnconfigure(0, weight=1)
以确保您对页面的网格划分对所有部分一视同仁。您的 window 不够大,无法显示信息,所以我将您的 OptionMenu
的宽度缩小了。
import tkinter as tk
age_menu = [
"<=18",
"19 - 25",
"25 - 34",
"35 - 45"
]
# root window
root = tk.Tk()
root.geometry("800x500") #this needs to be expanded to show everything
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",
width=60, padx=30, pady=40)
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=300, justify="left",
font=("Courier", 20), anchor="w",
width=60, padx=30, pady=20)
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=180)
disclaimer.grid(row=2, column=0, sticky=tk.W)
var = tk.StringVar()
var.set("Age")
ageclicked = tk.OptionMenu(root, var, *age_menu) #changed from 90 to 20
ageclicked.config(fg="#faf2e9", bg="#32402f", width=20,
font=("Helvetica", 12), anchor="e")
ageclicked.grid(row=2, column=1)
root.mainloop()
此示例将显示您想要我在评论中的意思。我删除了很多 width
参数,因为您并不真正需要它。我还添加了 root.resizable(False, False)
以设置永久 window 大小。有时这对于简单的 GUI 来说更容易。我还删除了行和列配置,因为我控制了 window 大小。我也对 window 大小进行了微调。
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()