• <i id='HpXtv'><tr id='HpXtv'><dt id='HpXtv'><q id='HpXtv'><span id='HpXtv'><b id='HpXtv'><form id='HpXtv'><ins id='HpXtv'></ins><ul id='HpXtv'></ul><sub id='HpXtv'></sub></form><legend id='HpXtv'></legend><bdo id='HpXtv'><pre id='HpXtv'><center id='HpXtv'></center></pre></bdo></b><th id='HpXtv'></th></span></q></dt></tr></i><div id='HpXtv'><tfoot id='HpXtv'></tfoot><dl id='HpXtv'><fieldset id='HpXtv'></fieldset></dl></div>
    <tfoot id='HpXtv'></tfoot>
      1. <legend id='HpXtv'><style id='HpXtv'><dir id='HpXtv'><q id='HpXtv'></q></dir></style></legend>

          <bdo id='HpXtv'></bdo><ul id='HpXtv'></ul>

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

      2. 更新时绑定到StringVar的Tkinter Label落后一键

        时间:2023-07-05
        <legend id='Arasq'><style id='Arasq'><dir id='Arasq'><q id='Arasq'></q></dir></style></legend>
          <i id='Arasq'><tr id='Arasq'><dt id='Arasq'><q id='Arasq'><span id='Arasq'><b id='Arasq'><form id='Arasq'><ins id='Arasq'></ins><ul id='Arasq'></ul><sub id='Arasq'></sub></form><legend id='Arasq'></legend><bdo id='Arasq'><pre id='Arasq'><center id='Arasq'></center></pre></bdo></b><th id='Arasq'></th></span></q></dt></tr></i><div id='Arasq'><tfoot id='Arasq'></tfoot><dl id='Arasq'><fieldset id='Arasq'></fieldset></dl></div>

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

          • <bdo id='Arasq'></bdo><ul id='Arasq'></ul>

            • <tfoot id='Arasq'></tfoot>

                  <tbody id='Arasq'></tbody>

                  本文介绍了更新时绑定到StringVar的Tkinter Label落后一键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在这里遇到的问题是,当我单击 Listbox 中的不同文件名时,Label 会在我的任何内容后面单击更改值m 当前点击.

                  The problem I'm running into here is that, when I click on the different file names in the Listbox, the Label changes value one click behind whatever I'm currently clicking on.

                  我在这里错过了什么?

                  import Tkinter as tk
                  
                  class TkTest: 
                  
                      def __init__(self, master):
                  
                          self.fraMain = tk.Frame(master)
                          self.fraMain.pack()
                  
                          # Set up a list box containing all the paths to choose from
                          self.lstPaths = tk.Listbox(self.fraMain)
                          paths = [
                              '/path/file1',
                              '/path/file2',
                              '/path/file3',
                          ]
                          for path in paths:
                              self.lstPaths.insert(tk.END, path)
                          self.lstPaths.bind('<Button-1>', self.update_label)
                          self.lstPaths.pack()
                  
                          self.currentpath = tk.StringVar()
                          self.lblCurrentPath = tk.Label(self.fraMain, textvariable=self.currentpath)
                          self.lblCurrentPath.pack()
                  
                      def update_label(self, event):
                          print self.lstPaths.get(tk.ACTIVE),
                          print self.lstPaths.curselection()
                          self.currentpath.set(self.lstPaths.get(tk.ACTIVE))
                  
                  root = tk.Tk()
                  app = TkTest(root)
                  root.mainloop()
                  

                  推荐答案

                  问题与 Tk 的基本设计有关.简短的版本是,特定小部件上的绑定在小部件的默认类绑定之前触发.它是在类绑定中更改列表框的选择.这正是您所观察到的——您看到的是当前点击之前的选择.

                  The problem has to do with the fundamental design of Tk. The short version is, bindings on specific widgets fire before the default class bindings for a widget. It is in the class bindings that the selection of a listbox is changed. This is exactly what you observe -- you are seeing the selection before the current click.

                  最好的解决方案是绑定到虚拟事件 <<ListboxSelect>> ,该事件在选择更改后触发.其他解决方案(Tk 独有的以及赋予它一些令人难以置信的功能和灵活性的原因)是修改应用绑定的顺序.这包括将小部件绑定标签移动到类绑定标签之后,或者在类绑定标签之后添加一个新的绑定标签并将其绑定到该类.

                  The best solution is to bind to the virtual event <<ListboxSelect>> which is fired after the selection has changed. Other solutions (unique to Tk and what gives it some of its incredible power and flexibility) is to modify the order that the bindings are applied. This involves either moving the widget bindtag after the class bindtag, or adding a new bindtag after the class bindtag and binding it to that.

                  由于绑定到 <<ListboxSelect>> 是更好的解决方案,我不会详细介绍如何修改绑定标签,尽管它很简单,而且我认为有据可查.

                  Since binding to <<ListboxSelect>> is the better solution I won't go into details on how to modify the bindtags, though it's straight-forward and I think fairly well documented.

                  这篇关于更新时绑定到StringVar的Tkinter Label落后一键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何在 Tkinter 列表框中插入时添加自动滚动? 下一篇:如何避免 tkinter &lt;&lt;ListboxSelect&gt;&gt;和

                  相关文章

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

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

                    • <bdo id='xMvC3'></bdo><ul id='xMvC3'></ul>

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

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