如果选择了选项菜单上的特定项目,则创建一个输入框

Create an Entry box if a specific item on Optionmenu is selected

我正在尝试为用户创建一个输入框,以手动输入选项菜单小部件列表中不存在的变量。这可能吗?

from Tkinter import *
import Tkinter as tk

class Demo1:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        x = (master.winfo_screenwidth() - master.winfo_reqwidth()) / 2
        y = (master.winfo_screenheight() - master.winfo_reqheight()) / 2
        master.geometry("+%d+%d" % (x, y))
        master.deiconify()

        self.subtests = StringVar()
        self.subtests.set("Enter Test Type")

        choices = ['Potato','Tomato','Onion','Other']
        self.testnumber = OptionMenu(master, self.subtests, *choices).grid(row = 2, column = 3)
        self.confirmbutton = Button (master, text="Confirm Test", width=20, command =lambda: self.confirmsubtest(master))
        self.confirmbutton.grid(row = 5, sticky = E)

def main(): 
    root = tk.Tk()
    app = Demo1(root)
    root.mainloop()

if __name__ == '__main__':
    main()

如前所述,如果用户需要select一个不在列表中的变量。是否可以允许用户通过相同 window 中的输入框手动输入变量(例如:在生成 entry/widget/something 的列表中选择 "Other")?

如果有人搜索同样的东西。该过程是将命令变量添加到选项菜单,并在所需的突出显示选项打开后禁用条目小部件。这是通过使用 .config(state=NORMAL) 或 .config(state=DISABLED) 配置 button/widget/anything 来完成的。

 self.testnumber = OptionMenu(master, self.subtests, *choices, command = self.optupdate).grid(row = 2, column = 3)
 self.testnumber.grid(row = 4, column = 1)

  def optupdate(self,value):
    if value == "Other":
        self.otherEntry.config(state=NORMAL)
    else:
        self.otherEntry.config(state=DISABLED)
    return