现在是两个星期,我全职寻找相关问题的解决方案:如何在动态循环中创建命令?主题很简单(想象一下我采取了食物元素):为了启动程序,我有许多包来确定(例如5,然后按回车)。然后,感谢创建的Comboboxes,针对每种情况,指定它是水果,蔬菜还是其他。每个Combobox的目的是再次创建一个适合执行值的Widget。问题是当执行Bind
时,循环适用于整个序列并复制前部小部件。可以通过在第48行添加指令来解决问题,例如:
if 'Selection'.get() == self.Combo_Food_liste[i]:
但我寻找'选择'意味着对象我无法达到它!或者如果你有更好的想法,我会很高兴知道它!
谢谢!!
from ttk import *
from Tkinter import *
class Postproc:
def __init__(self, parent, title):
self.variable_Title_Frame = []
self.variable_Title =[]
self.Combo_Food_Select=[]
self.Combo_Food_stock=[]
self.Combo_Food_liste=[]
self.Combo_Food_Pos_Select={}
self.Combo_Food_Pos_stock={}
self.Combo_Food_Pos_liste={}
self.Combo_Food_Pos_Entry={}
self.parent = parent
self.parent.title(title)
self.parent.protocol("WM_DELETE_WINDOW", self.Closes)
Frame1 = Frame(self.parent,borderwidth=.5,relief=GROOVE)
Frame1.pack(side=LEFT,padx=1,pady=1)
### Define the number of packs then press 'Enter'
self.variables_Title_number=IntVar()
self.Field_Ouputs = Entry(Frame1, textvariable=self.variables_Title_number, bg ='bisque', fg='maroon')
self.Field_Ouputs.pack(side=LEFT,padx=1,pady=1)
self.Field_Ouputs.bind("<Return>", self.Launch_Outputs)
def Closes(self, event=None):
self.parent.destroy()
def Launch_Outputs(self, event=None):
self.Nombr = self.variables_Title_number.get()
self.Create_Outputs(self.Nombr)
def Create_Outputs(self, Nombr):
#### Define for each pack the kind of food
for i in range (0,Nombr):
self.variable_Title_Frame.append(Frame(MainWin,borderwidth=.5,relief=GROOVE))
self.variable_Title_Frame[i].pack(side= LEFT,expand=YES)
Label(self.variable_Title_Frame[i],text=i).pack(padx=1,pady=1)
self.Combo_Food_Select.append(StringVar())
self.Combo_Food_stock.append(('Fruit', 'Vegetable', 'Other'))
self.Combo_Food_liste.append(Combobox(self.variable_Title_Frame[i], textvariable = self.Combo_Food_Select[i], \
values = self.Combo_Food_stock[i], state = 'readonly'))
self.Combo_Food_liste[i].bind("<<ComboboxSelected>>", self.Position_Magnitude)
self.Combo_Food_liste[i].pack()
def Position_Magnitude(self, index):
##### Define for each kind the variety
for i in range (0, self.Nombr):
if self.Combo_Food_liste[i].get() == 'Fruit':
self.Combo_Food_Pos_Select[i]=(StringVar())
self.Combo_Food_Pos_stock[i]=(['Apple', 'Orange'])
self.Combo_Food_Pos_liste[i]=(Combobox(self.variable_Title_Frame[i], textvariable = self.Combo_Food_Pos_Select[i], \
values = self.Combo_Food_Pos_stock[i], state = 'readonly'))
self.Combo_Food_Pos_liste[i].pack(side=BOTTOM)
if self.Combo_Food_liste[i].get() == 'Vegetable':
self.Combo_Food_Pos_Select[i]=(StringVar())
self.Combo_Food_Pos_stock[i]=(['Tomatoe', 'Pepper', 'Peas'])
self.Combo_Food_Pos_liste[i]=(Combobox(self.variable_Title_Frame[i], textvariable = self.Combo_Food_Pos_Select[i], \
values = self.Combo_Food_Pos_stock[i], state = 'readonly'))
self.Combo_Food_Pos_liste[i].pack(side=BOTTOM)
if self.Combo_Food_liste[i].get() == 'Other':
self.Combo_Food_Pos_Select[i]=(StringVar())
self.Combo_Food_Pos_Entry[i]=Entry(self.variable_Title_Frame[i], textvariable=self.Combo_Food_Pos_Select[i], bg ='bisque', fg='maroon')
self.Combo_Food_Pos_Select[i].set("Specify")
self.Combo_Food_Pos_Entry[i].pack(side=BOTTOM)
if self.Combo_Food_liste[i].get() == "":
next
if __name__ == '__main__':
MainWin = Tk()
app = Postproc(MainWin, "Main Window")
MainWin.mainloop()
答案 0 :(得分:0)
您可以使用lambda
功能将i
发送至Position_Magnitude()
self.Combo_Food_liste[i].bind("<<ComboboxSelected>>", lambda event, index=i:self.Position_Magnitude(event,index))
在Position_Magnitude
中,您可以使用event.widget
代替self.Combo_Food_liste[i]
from ttk import *
from Tkinter import *
class Postproc:
def __init__(self, parent, title):
self.variable_Title_Frame = []
self.variable_Title =[]
self.Combo_Food_Select=[]
self.Combo_Food_stock=[]
self.Combo_Food_liste=[]
self.Combo_Food_Pos_Select={}
self.Combo_Food_Pos_stock={}
self.Combo_Food_Pos_liste={}
self.Combo_Food_Pos_Entry={}
self.parent = parent
self.parent.title(title)
self.parent.protocol("WM_DELETE_WINDOW", self.Closes)
Frame1 = Frame(self.parent,borderwidth=.5,relief=GROOVE)
Frame1.pack(side=LEFT,padx=1,pady=1)
### Define the number of packs then press 'Enter'
self.variables_Title_number=IntVar()
self.Field_Ouputs = Entry(Frame1, textvariable=self.variables_Title_number, bg ='bisque', fg='maroon')
self.Field_Ouputs.pack(side=LEFT,padx=1,pady=1)
self.Field_Ouputs.bind("<Return>", self.Launch_Outputs)
def Closes(self, event=None):
self.parent.destroy()
def Launch_Outputs(self, event=None):
self.Nombr = self.variables_Title_number.get()
self.Create_Outputs(self.Nombr)
def Create_Outputs(self, Nombr):
#### Define for each pack the kind of food
for i in range(Nombr):
self.variable_Title_Frame.append(Frame(MainWin,borderwidth=.5,relief=GROOVE))
self.variable_Title_Frame[i].pack(side= LEFT,expand=YES)
Label(self.variable_Title_Frame[i],text=i).pack(padx=1,pady=1)
self.Combo_Food_Select.append(StringVar())
self.Combo_Food_stock.append(('Fruit', 'Vegetable', 'Other'))
self.Combo_Food_liste.append(Combobox(self.variable_Title_Frame[i], textvariable = self.Combo_Food_Select[i], \
values = self.Combo_Food_stock[i], state = 'readonly'))
self.Combo_Food_liste[i].bind("<<ComboboxSelected>>", lambda event, index=i:self.Position_Magnitude(event,index))
self.Combo_Food_liste[i].pack()
def Position_Magnitude(self, event, index):
#print event.widget, index
if event.widget.get() == 'Fruit':
self.Combo_Food_Pos_Select[index]=(StringVar())
self.Combo_Food_Pos_stock[index]=(['Apple', 'Orange'])
self.Combo_Food_Pos_liste[index]=(Combobox(self.variable_Title_Frame[index], textvariable = self.Combo_Food_Pos_Select[index], \
values = self.Combo_Food_Pos_stock[index], state = 'readonly'))
self.Combo_Food_Pos_liste[index].pack(side=BOTTOM)
if event.widget.get() == 'Vegetable':
self.Combo_Food_Pos_Select[index]=(StringVar())
self.Combo_Food_Pos_stock[index]=(['Tomatoe', 'Pepper', 'Peas'])
self.Combo_Food_Pos_liste[index]=(Combobox(self.variable_Title_Frame[index], textvariable = self.Combo_Food_Pos_Select[index], \
values = self.Combo_Food_Pos_stock[index], state = 'readonly'))
self.Combo_Food_Pos_liste[index].pack(side=BOTTOM)
if event.widget.get() == 'Other':
self.Combo_Food_Pos_Select[index]=(StringVar())
self.Combo_Food_Pos_Entry[index]=Entry(self.variable_Title_Frame[index], textvariable=self.Combo_Food_Pos_Select[index], bg ='bisque', fg='maroon')
self.Combo_Food_Pos_Select[index].set("Specify")
self.Combo_Food_Pos_Entry[index].pack(side=BOTTOM)
if event.widget.get() == "":
next
if __name__ == '__main__':
MainWin = Tk()
app = Postproc(MainWin, "Main Window")
MainWin.mainloop()
修改强>
<顺便说一下:如果你在顶部组合框中选择新东西,你必须删除旧的组合框/条目def Position_Magnitude(self, event, index):
#print event.widget, index
try:
if self.Combo_Food_Pos_liste[index]:
self.Combo_Food_Pos_liste[index].pack_forget()
except:
pass
try:
if self.Combo_Food_Pos_Entry[index]:
self.Combo_Food_Pos_Entry[index].pack_forget()
except:
pass
if event.widget.get() == 'Fruit':
self.Combo_Food_Pos_Select[index]=(StringVar())
self.Combo_Food_Pos_stock[index]=(['Apple', 'Orange'])
self.Combo_Food_Pos_liste[index]=(Combobox(self.variable_Title_Frame[index], textvariable = self.Combo_Food_Pos_Select[index], \
values = self.Combo_Food_Pos_stock[index], state = 'readonly'))
self.Combo_Food_Pos_liste[index].pack(side=BOTTOM)
if event.widget.get() == 'Vegetable':
self.Combo_Food_Pos_Select[index]=(StringVar())
self.Combo_Food_Pos_stock[index]=(['Tomatoe', 'Pepper', 'Peas'])
self.Combo_Food_Pos_liste[index]=(Combobox(self.variable_Title_Frame[index], textvariable = self.Combo_Food_Pos_Select[index], \
values = self.Combo_Food_Pos_stock[index], state = 'readonly'))
self.Combo_Food_Pos_liste[index].pack(side=BOTTOM)
if event.widget.get() == 'Other':
self.Combo_Food_Pos_Select[index]=(StringVar())
self.Combo_Food_Pos_Entry[index]=Entry(self.variable_Title_Frame[index], textvariable=self.Combo_Food_Pos_Select[index], bg ='bisque', fg='maroon')
self.Combo_Food_Pos_Select[index].set("Specify")
self.Combo_Food_Pos_Entry[index].pack(side=BOTTOM)
if event.widget.get() == "":
next