You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter窗口中OptionMenu未显示问题排查及多条件护肤产品推荐功能实现咨询

Solution to Your Tkinter Skincare Recommendation App Issues

Hey there! Let's break down your problems and fix them step by step.

1. Why Your Age OptionMenu Isn't Showing Up

The core issue here is layout overlap and excessive padding pushing your menu out of view. Let's look at the details:

  • Your disclaimer label is placed in row=2 with pady=180—this massive vertical padding makes the row take up almost all the window's height.
  • The disclaimer also has width=100, which combined with your 800px-wide window, means it fills almost the entire horizontal space of column=0. Your age menu is in row=2, column=1, which gets pushed outside the window's visible area.

Fix for Visibility

Adjust the row positions and padding to give your OptionMenus room to breathe. Here's the modified layout section of your code:

import tkinter as tk
age_menu = ["<=18", "19 - 25", "25 - 34", "35 - 45"]
# root window
root = tk.Tk()
root.geometry("800x600")  # Increased height to fit all elements
root.title("SAVE MY SKIN")
root.configure(bg = "#32402f")

# Greeting and instructions
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 = 700, justify = "left", font = ("Courier",14), anchor = "w", width = 100, padx = 30, pady = 20 )
instructions.grid(row = 1, column = 0, columnspan=2, sticky = tk.W)

# Age OptionMenu (now visible!)
var_age = tk.StringVar()
var_age.set("Select Age")
ageclicked = tk.OptionMenu(root, var_age, *age_menu)
ageclicked.config(fg = "#faf2e9", bg = "#32402f", width = 20, font = ("Helvetica",12))
ageclicked.grid(row = 2, column = 0, padx=30, pady=10)

# Disclaimer moved to bottom with reduced padding
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 = 750, justify = "left",font = ("Helvetica Neue",10), anchor = "w", width = 100, padx = 30, pady = 20 )
disclaimer.grid(row = 5, column = 0, columnspan=2, sticky = tk.W)

root.mainloop()

Key changes made:

  • Increased window height to 800x600 to fit all elements
  • Moved disclaimer to row=5 and added columnspan=2 to span both columns
  • Reduced disclaimer's pady from 180 to 20
  • Adjusted age menu's width to a reasonable 20 and added padding for spacing
  • Made instructions span 2 columns for better readability

2. Implementing Product Recommendations Based on 4 Selections

To generate recommendations, we'll add the remaining 3 OptionMenus, create a recommendation button, and build a rule-based system to match selections to products.

Step 1: Add the Remaining OptionMenus

Add these variables and menus after the age menu:

# Skin type options
skin_type_menu = ["Dry", "Oily", "Combination", "Sensitive"]
var_skin = tk.StringVar()
var_skin.set("Select Skin Type")
skin_clicked = tk.OptionMenu(root, var_skin, *skin_type_menu)
skin_clicked.config(fg = "#faf2e9", bg = "#32402f", width = 20, font = ("Helvetica",12))
skin_clicked.grid(row = 2, column = 1, padx=30, pady=10)

# Skin concern options
concern_menu = ["Acne", "Dryness", "Fine Lines", "Dark Spots"]
var_concern = tk.StringVar()
var_concern.set("Select Main Concern")
concern_clicked = tk.OptionMenu(root, var_concern, *concern_menu)
concern_clicked.config(fg = "#faf2e9", bg = "#32402f", width = 20, font = ("Helvetica",12))
concern_clicked.grid(row = 3, column = 0, padx=30, pady=10)

# Clean beauty requirement
clean_menu = ["Yes", "No"]
var_clean = tk.StringVar()
var_clean.set("Clean Beauty?")
clean_clicked = tk.OptionMenu(root, var_clean, *clean_menu)
clean_clicked.config(fg = "#faf2e9", bg = "#32402f", width = 20, font = ("Helvetica",12))
clean_clicked.grid(row = 3, column = 1, padx=30, pady=10)

Step 2: Build the Recommendation Logic

Create a function that retrieves user selections, matches them to products, and displays results. We'll use a dictionary to store recommendation rules (you can expand this as needed):

# Recommendation dictionary - add more combinations as needed
recommendation_rules = {
    ("19 - 25", "Oily", "Acne", "Yes"): [
        "Oil-Free Mattifying Moisturizer (Clean)",
        "Salicylic Acid Cleanser (Clean)",
        "Non-Comedogenic Sunscreen (Clean)"
    ],
    ("25 - 34", "Dry", "Fine Lines", "No"): [
        "Hydrating Anti-Aging Serum",
        "Rich Night Cream",
        "Broad-Spectrum Sunscreen"
    ],
    ("<=18", "Sensitive", "Dryness", "Yes"): [
        "Fragrance-Free Gentle Cleanser (Clean)",
        "Ceramide-Infused Moisturizer (Clean)",
        "Mineral Sunscreen (Clean)"
    ]
}

def show_recommendations():
    # Get user selections
    age = var_age.get()
    skin_type = var_skin.get()
    concern = var_concern.get()
    clean = var_clean.get()
    
    # Check if all selections are made
    if age == "Select Age" or skin_type == "Select Skin Type" or concern == "Select Main Concern" or clean == "Clean Beauty?":
        result_label.config(text="Please fill in all options first!")
        return
    
    # Get matching recommendations or default message
    key = (age, skin_type, concern, clean)
    recommendations = recommendation_rules.get(key, ["Sorry, no recommendations match your selections yet."])
    
    # Format results as a string
    result_text = "Recommended Products:\n" + "\n".join([f"- {prod}" for prod in recommendations])
    result_label.config(text=result_text)

# Add recommendation button and result label
recommend_btn = tk.Button(root, text="Get Recommendations", command=show_recommendations, bg="#faf2e9", fg="#32402f", font=("Helvetica",12,"bold"))
recommend_btn.grid(row=4, column=0, columnspan=2, pady=20)

result_label = tk.Label(root, text="", fg="#faf2e9", bg="#32402f", wraplength=750, justify="left", font=("Helvetica",12))
result_label.grid(row=5, column=0, columnspan=2, padx=30, pady=10)

How This Works:

  • We use a dictionary where each key is a tuple of the 4 user selections, and the value is a list of matching products.
  • The show_recommendations function checks if all options are selected, looks up the matching products, and displays them in a label.
  • You can easily expand the recommendation_rules dictionary with more combinations as you add products.

3. Adding the "Help" Button

To implement the help button that shows contact info, add this code snippet after the recommendation button:

def show_contact_info():
    contact_text = "Help & Contact:\n- Email: support@savemyskin.com\n- Phone: 1-800-SKIN-HELP"
    result_label.config(text=contact_text)

help_btn = tk.Button(root, text="Help", command=show_contact_info, bg="#faf2e9", fg="#32402f", font=("Helvetica",10))
help_btn.grid(row=0, column=1, padx=30, pady=10, sticky=tk.E)

This button will replace the recommendation results with your contact information when clicked.

内容的提问来源于stack exchange,提问作者joobs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 21:39:08