簡體   English   中英

optionMenu 沒有出現在窗口 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)以確保您的頁面網格化處理所有部分。 您的窗口不夠大,無法顯示信息,因此我將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)來設置永久窗口大小。 有時這對於簡單的 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()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM