2014-05-23 87 views
1

我有以下代碼,需要它用新值更新同一個下拉列表。目前它只是在現有的權利中創建一個新的清單。有關如何實現這一目標的任何建議?更新選項菜單問題

import os 
import sys 
import Tkinter as tk 

def edit(): 
var.set('Choose build') 
choices =['one','two','three','four','five','six','seven','eight','nine','ten'] 

option = tk.OptionMenu(root, var, *choices) 
option.pack(side='left', padx=10, pady=20) 

root.mainloop() 

root = tk.Tk() 
var = tk.StringVar(root) 
var.set('Choose build') 
option = tk.OptionMenu(root, var, '') 

root.geometry("%dx%d+%d+%d" % (500, 150, 200, 150)) 
root.title("QCT Multimedia Build Metrics") 

button = tk.Button(root, text="Edit Config", command=edit) 
button.pack(side='left', padx=20, pady=10) 

choices = ['one','two','three','four','five'] 

option = tk.OptionMenu(root, var, *choices) 
option.pack(side='left', padx=10, pady=20) 

root.mainloop() 
+0

什麼是無功?修正你的格式。 –

回答

0

我用這個方法我自己 - 我傳遞一個新的列表到updateOptions()方法

def updateOptions(self,optionList=[]): 
    option.set('') 
    option['menu'].delete(0, 'end') # Delete old options 

    for choice in optionList: 
     option['menu'].add_command(label=choice, command=tk._setit(var, choice)) 
    var.set(optionList[0]) # Set default value to be the first in the list