如果选择了Optionmenu上的特定项目,请创建一个条目框

时间:2016-01-12 23:25:48

标签: python python-2.7 tkinter optionmenu tkinter-entry

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

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

如上所述,如果用户需要选择不在列表中的变量。是否可以允许用户通过同一窗口中的输入框手动输入变量(例如:在列表中选择“其他”生成条目/窗口小部件/东西)?

1 个答案:

答案 0 :(得分:0)

如果有人搜索同一件事。该过程是将命令变量添加到optionmenu,并在所需的突出显示选项打开后禁用条目小部件。这是通过使用.config(state = NORMAL)或.config(state = DISABLED)配置按钮/小部件/任何内容来完成的。

 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