在动态创建时,在python中不选择组合框值

时间:2015-09-04 08:34:34

标签: python-2.7 tkinter ttk

我创建了一个Tkinter窗口Form,根据用户选择我动态替换组合框并删除Exist one.But问题是当我这样做时,在组合框中选择的值不会更新。它始终显示默认值。请参阅以下代码

from Tkinter import * 
import ttk
final=[]
field_0=['1','0']
field_1=['1','2','23','45','6']
field_2=['2','5','7','8','9']

class header:
    def __init__(self,root):
        self.parent=root
        self.row_number= 1
        self.value=0
        root.title("                                    Select fields                                     ")
        root.minsize(width=450, height=530)
        root.maxsize(width=450, height=550)
        frame=Frame(root,height=20,relief=FLAT)
        frame.grid(row=0,column=0,sticky=W)
        Label(frame,text="Enter value:").grid(row=0,column=0,padx=70,pady=5,sticky=W)
        entryValue=StringVar()
        port_e = Entry(root,width=5,textvariable=entryValue)
        port_e.delete(0,END)
        port_e.insert(0,'0')
        port_e.grid(row=0,column=0,padx=100,pady=5)
        self.value=self.value+1
        self.change_row=self.row_number
        self.combocreate(self.change_row,root,field_0)
        self.change_row=self.change_row+1
        self.combocreate(self.change_row,root,field_1)



    def  combocreate(self,row_number,msg_frame,field):
        comboBoxValue = []              # 'request'command  for sink only
        self.box_value=StringVar()
        self.combo=ttk.Combobox(root,textvariable=self.box_value,state='readonly')
        self.combo['values'] = tuple(field)
        self.combo.set(field[1])
        self.combo.grid(row = row_number, column = 1)
        self.combo.bind("<<ComboboxSelected>>",self.selected_field)
        final.append(self.combo)

    def selected_field(self,event):
       global cnt_sel
       print final[0].get()

       if(final[0].get()=='1'):
            self.control=Label(root,text="Choose one type").grid(row=self.change_row,column=0,padx=20,pady=5,sticky=W)
            self.combocreate(self.change_row,root,field_1)
            final[1]=final[2]    #replacing combobox dynamically based on selection
            del final[2]
       elif(final[0].get()=='0'):
            Label(root,text="Choose zerotype     ").grid(row=self.change_row,column=0,padx=20,pady=5,sticky=W)
            self.combocreate(self.change_row,root,field_2)
            final[1]=final[2]
            del final[2]
       else:
            pass

if __name__=="__main__":
    root=Tk()
    app=header(root)
    root.mainloop()

1 个答案:

答案 0 :(得分:0)

您的代码存在的问题是,每次调用Combobox时,它都会自动将self.selected_field绑定到self.selected_field,因此每当您更改Combobox类型的值时,都会调用{ {1}},它会自动将类型重置为默认值。要解决此问题,您应该只绑定第一个Combobox。这应该可以解决问题:

替换

self.change_row=self.row_number
self.combocreate(self.change_row,root,field_0)
self.change_row=self.change_row+1
self.combocreate(self.change_row,root,field_1)

使用

self.change_row=self.row_number
self.combocreate(self.change_row,root,field_0)
final[0].bind("<<ComboboxSelected>>",self.selected_field)
self.change_row=self.change_row+1
self.combocreate(self.change_row,root,field_1)

def combocreate(self,row_number,msg_frame,field):
    comboBoxValue = []              # 'request'command  for sink only
    self.box_value=StringVar()
    self.combo=ttk.Combobox(root,textvariable=self.box_value,state='readonly')
    self.combo['values'] = tuple(field)
    self.combo.set(field[1])
    self.combo.grid(row = row_number, column = 1)
    self.combo.bind("<<ComboboxSelected>>",self.selected_field)
    final.append(self.combo)

使用

def combocreate(self,row_number,msg_frame,field):
        comboBoxValue = []              # 'request'command  for sink only
        self.box_value=StringVar()
        self.combo=ttk.Combobox(root,textvariable=self.box_value,state='readonly')
        self.combo['values'] = tuple(field)
        self.combo.set(field[1])
        self.combo.grid(row = row_number, column = 1)

        final.append(self.combo)