简体   繁体   English

optionMenu 没有出现在窗口 tkinter 中

[英]optionMenu does not appear in window tkinter

I am very new to python and tkinter.我对 python 和 tkinter 很陌生。 I am trying to build the below GUI interface which is a skincare program that will recommend products based on the result of picking from the 4 drop-down menus.我正在尝试构建下面的 GUI 界面,这是一个护肤程序,它将根据从 4 个下拉菜单中选择的结果推荐产品。 The last button - help will bring up contact info.最后一个按钮 - 帮助将显示联系信息。

I am at the very beginning stages and cannot get the first option menu for age to appear.我正处于起步阶段,无法显示年龄的第一个选项菜单。 Could anyone explain the reason for this please and how do I make it so that the combination of answers for Age, Skin Type, Main Skin Concern and Clean Beauty result in products recommended due to the combination?任何人都可以解释一下这个原因吗?我该如何做到这一点,以便将年龄、皮肤类型、主要皮肤问题和清洁美容的答案组合起来,从而产生由于组合而推荐的产品?

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()

在此处输入图像描述

So the first issue I had was with formatting so I rewrote it to follow pep8 guidelines.所以我遇到的第一个问题是格式化,所以我重写了它以遵循pep8指南。 I simply added the root.grid_rowconfigure(0, weight=1) and root.grid_columnconfigure(0, weight=1) which makes sure your gridding of the page treats all sections equally.我只是添加了root.grid_rowconfigure(0, weight=1)root.grid_columnconfigure(0, weight=1)以确保您的页面网格化处理所有部分。 Your window isn't large enough to display the info so I made the width of your OptionMenu smaller.您的窗口不够大,无法显示信息,因此我将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()

在此处输入图像描述

This sample would show you want I meant in the comments.此示例将显示您想要我在评论中的意思。 I removed a lot of width parameters since you don't really need it.我删除了很多width参数,因为你真的不需要它。 I also added the root.resizable(False, False) to set a permanent window size.我还添加了root.resizable(False, False)来设置永久窗口大小。 Sometimes that's easier for simple GUIs.有时这对于简单的 GUI 来说更容易。 I also removed the row and column configuration since I control the window size.我还删除了行和列配置,因为我控制了窗口大小。 I made a slight adjustment to the window size as well.我也对窗口大小进行了微调。

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()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM