<small id='9YTeh'></small><noframes id='9YTeh'>

      1. <legend id='9YTeh'><style id='9YTeh'><dir id='9YTeh'><q id='9YTeh'></q></dir></style></legend>
          <bdo id='9YTeh'></bdo><ul id='9YTeh'></ul>
        <tfoot id='9YTeh'></tfoot>

        <i id='9YTeh'><tr id='9YTeh'><dt id='9YTeh'><q id='9YTeh'><span id='9YTeh'><b id='9YTeh'><form id='9YTeh'><ins id='9YTeh'></ins><ul id='9YTeh'></ul><sub id='9YTeh'></sub></form><legend id='9YTeh'></legend><bdo id='9YTeh'><pre id='9YTeh'><center id='9YTeh'></center></pre></bdo></b><th id='9YTeh'></th></span></q></dt></tr></i><div id='9YTeh'><tfoot id='9YTeh'></tfoot><dl id='9YTeh'><fieldset id='9YTeh'></fieldset></dl></div>

        Tkinter 在循环中使用 lambda 分配按钮命令

        Tkinter assign button command in loop with lambda(Tkinter 在循环中使用 lambda 分配按钮命令)
          <tbody id='awfn2'></tbody>
          <bdo id='awfn2'></bdo><ul id='awfn2'></ul>
          <legend id='awfn2'><style id='awfn2'><dir id='awfn2'><q id='awfn2'></q></dir></style></legend>

          1. <tfoot id='awfn2'></tfoot>

            <small id='awfn2'></small><noframes id='awfn2'>

          2. <i id='awfn2'><tr id='awfn2'><dt id='awfn2'><q id='awfn2'><span id='awfn2'><b id='awfn2'><form id='awfn2'><ins id='awfn2'></ins><ul id='awfn2'></ul><sub id='awfn2'></sub></form><legend id='awfn2'></legend><bdo id='awfn2'><pre id='awfn2'><center id='awfn2'></center></pre></bdo></b><th id='awfn2'></th></span></q></dt></tr></i><div id='awfn2'><tfoot id='awfn2'></tfoot><dl id='awfn2'><fieldset id='awfn2'></fieldset></dl></div>
                • 本文介绍了Tkinter 在循环中使用 lambda 分配按钮命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I'm trying to create a few Buttons (with a for loop) like so:

                  def a(self, name):
                      print(name)
                  
                  users = {"Test": "127.0.0.0", "Test2": "128.0.0.0"}
                  row = 1
                  for name in users:
                      user_button = Tkinter.Button(self.root, text=name,
                                                   command=lambda: self.a(name))
                      user_button.grid(row=row, column=0)
                      row += 1
                  

                  and for the buttons to each get their own parameter (Test getting "Test" and Test2 getting "Test2"), but when I press the buttons they both print "Test2" which means they are using the same function with the same parameter.

                  How can I solve this?

                  解决方案

                  The problem is your lamba in the for loop. Your lambda is using the name variable, but the name variable gets reassigned each time through the for loop. So in the end, all of the buttons get the last value that name was assigned to in the for loop. To avoid this you can use default keyword parameters in your lamba expression like so:

                  user_button = Tkinter.Button(self.root,
                                               text=name,
                                               command=lambda name=name: self.a(name))
                  

                  This binds the current value of the name variable to the lamba's name keyword argument each time through the loop, producing the desired effect.

                  这篇关于Tkinter 在循环中使用 lambda 分配按钮命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                  <i id='mobW9'><tr id='mobW9'><dt id='mobW9'><q id='mobW9'><span id='mobW9'><b id='mobW9'><form id='mobW9'><ins id='mobW9'></ins><ul id='mobW9'></ul><sub id='mobW9'></sub></form><legend id='mobW9'></legend><bdo id='mobW9'><pre id='mobW9'><center id='mobW9'></center></pre></bdo></b><th id='mobW9'></th></span></q></dt></tr></i><div id='mobW9'><tfoot id='mobW9'></tfoot><dl id='mobW9'><fieldset id='mobW9'></fieldset></dl></div>
                      <tbody id='mobW9'></tbody>

                  1. <tfoot id='mobW9'></tfoot>
                      <bdo id='mobW9'></bdo><ul id='mobW9'></ul>

                          <small id='mobW9'></small><noframes id='mobW9'>

                          <legend id='mobW9'><style id='mobW9'><dir id='mobW9'><q id='mobW9'></q></dir></style></legend>