<tfoot id='JgC9P'></tfoot>
      <legend id='JgC9P'><style id='JgC9P'><dir id='JgC9P'><q id='JgC9P'></q></dir></style></legend>

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

      2. <small id='JgC9P'></small><noframes id='JgC9P'>

        python tkinter 列表框事件绑定

        时间:2023-07-05

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

              • <legend id='xV1FL'><style id='xV1FL'><dir id='xV1FL'><q id='xV1FL'></q></dir></style></legend>
                • <bdo id='xV1FL'></bdo><ul id='xV1FL'></ul>
                • <small id='xV1FL'></small><noframes id='xV1FL'>

                    <tbody id='xV1FL'></tbody>
                  本文介绍了python tkinter 列表框事件绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我无法让事件绑定与 python/tkinter 一起使用.我只是尝试单击并打印位置,但每次执行此操作时,结果都是-1".

                  I'm having trouble getting an event binding to work with python/tkinter. I'm simply trying to click and have the location printed, but every time I do this, "-1" is the result.

                  这是我的代码

                  from Tkinter import *
                  import Tkinter
                  
                  class make_list(Tkinter.Listbox):
                  
                      def __init__(self,master, **kw):
                          frame = Frame(master)
                          frame.pack()
                          self.build_main_window(frame)
                  
                          kw['selectmode'] = Tkinter.SINGLE
                          Tkinter.Listbox.__init__(self, master, kw)
                          master.bind('<Button-1>', self.click_button)
                          master.curIndex = None
                  
                      #display the clicked location
                      def click_button(self, event):
                          self.curIndex = self.nearest(event.x)
                          print self.curIndex
                  
                      #display the window, calls the listbox
                      def build_main_window(self, frame):
                          self.build_listbox(frame)
                  
                      #listbox
                      def build_listbox(self, frame):
                          listbox = Listbox(frame)
                          for item in ["one", "two", "three", "four"]:
                              listbox.insert(END, item)    
                          listbox.insert(END, "a list entry")
                          listbox.pack()
                          return
                  
                  if __name__ == '__main__':
                      tk = Tkinter.Tk()
                      make_list(tk)
                      tk.mainloop()
                  

                  更新的代码 - 我摆脱了框架,但我似乎无法弄清楚为什么函数 click_button 中的第一个打印语句得到 -1

                  from Tkinter import *
                  import Tkinter
                  
                  class make_list(Tkinter.Listbox):
                  
                      #display the clicked location
                      def click_button(self, event):
                          ##this block works
                          w = event.widget
                          index = int(w.curselection()[0])
                          value = w.get(index)
                          print value
                          ##this doesn't
                          self.curIndex = self.nearest(event.y)
                          print self.curIndex
                          self.curIndex = event.widget.nearest(event.y)
                          print self.curIndex
                  
                      #display the window, calls the listbox
                      def build_main_window(self):
                          self.build_listbox()
                  
                      #listbox
                      def build_listbox(self):
                          listbox = Listbox()
                          listbox.bind('<<ListboxSelect>>', self.click_button)
                          for item in ["one", "two", "three", "four"]:
                              listbox.insert(END, item)    
                          listbox.insert(END, "a list entry")
                          listbox.pack()
                          return
                  
                  if __name__ == '__main__':
                      tk = Tkinter.Tk()
                      start = make_list(tk)
                      start.build_main_window()
                      start.mainloop()
                  

                  推荐答案

                  listbox 最近的项目是由 y 找到的,而不是 x.

                  listbox nearest item is found by y, not x.

                   self.nearest(event.x)     # wrong
                   self.nearest(event.y)     # right
                  

                  更新:我首先没有注意到真正的问题:

                  Update: I didn't notice the real problem first:

                      listbox = Listbox(frame)
                  

                  它不是你子类化的同一个列表框,它是另一个不相关的列表框.你的列表框( make_list)是空的,这就是为什么它总是返回-1表示最近的.

                  It's not the same listbox which you subclassed, it's another unrelated listbox. Your listbox (which is make_list) is empty, that's why it always returns -1 for nearest.

                  也许子类化一个框架是一个好主意(无论如何,比子类化列表框并在其中添加一个带有另一个列表框的框架更好).然后你必须在那个不为空的 real 列表框上绑定事件.

                  Perhaps subclassing a frame is a good idea (anyway, better than subclassing listbox and adding a frame with another listbox into it). Then you'll have to bind event on that real listbox which is not empty.

                  查看它修复后如何工作的快速方法是使用 event.widget 调用真实列表框的 nearest:

                  Quick way to see how it will work when fixed is to call nearest of a real listbox with event.widget:

                  self.curIndex = event.widget.nearest(event.y)
                  

                  这篇关于python tkinter 列表框事件绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何避免 tkinter &lt;&lt;ListboxSelect&gt;&gt;和 下一篇:Tkinter 列表框

                  相关文章

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

                    1. <legend id='ZqBxJ'><style id='ZqBxJ'><dir id='ZqBxJ'><q id='ZqBxJ'></q></dir></style></legend>

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