将参数作为参数传递时克服后期绑定

时间:2019-08-29 00:23:57

标签: python tkinter lambda

我正在使用lambda来允许我使用范围内的全部值,并且在这种情况下,创建了一系列按钮,这些按钮的文本变量与其范围内的相应值相同。然后,我想创建特定于该迭代的x值的if / then语句,但问题是当x作为参数传递给函数时出现,该问题又回到被视为范围内的最后一个值,即问题后期绑定回报。

State_Full = ACTIVE
State_Null = DISABLED
Normal = NORMAL

i= 1
x=1

for x in range(1,6):
   Address_Hex = hex(x).lstrip("0x")
      btn = tk.Button(, height=1, width=10, relief=tk.RAISED, 
      disabledforeground` = "Red",state =State_Full,text= Address_Hex 
      ,command=lambda i=i,x=x: Write_Hex(x))

  btn.pack(padx=10, pady=2, side=tk.TOP)

  btn_1 = tk.Button(scframe.interior, height=1, width=10, relief=tk.RAISED,
                 text= "Opcode_1",disabledforeground = "Red", fg = "Blue" 
                 ,command=lambda i=i,x=x: Write_Opcode_1())
  btn_1.pack(padx=10, pady=2, side=tk.TOP)

  def Write_Hex(x):
      Address_Hex = hex(x).lstrip("0x")
      T.insert(tk.END, Address_Hex)
      State_1 = btn_1.cget("state")
      if  x <= 1 :
         btn_1['state'] = State_Null
      if x == 2:
         btn['state'] = State_Null
#^^^^^^The problem is above here, when x is 1 or 2 all of the buttons should 
turn off but only button 5 the end of the range turns off/disables^^^^^^

我希望该范围创建的所有按钮都将被禁用并变成红色,但是在此只有5个按钮被禁用。

0 个答案:

没有答案