<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 分配按钮命令

        时间:2024-04-21
          <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 分配按钮命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:什么是真假?它与真假有什么不同? 下一篇:如何在 Python 的 SQL 语句中使用变量?

                  相关文章

                  <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>
                  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>