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

      2. <legend id='37415'><style id='37415'><dir id='37415'><q id='37415'></q></dir></style></legend>

      3. <small id='37415'></small><noframes id='37415'>

        <tfoot id='37415'></tfoot>
          <bdo id='37415'></bdo><ul id='37415'></ul>
      4. 如何避免 tkinter &lt;&lt;ListboxSelect&gt;&gt;和

        How to avoid tkinter lt;lt;ListboxSelectgt;gt; and .curselection() detecting events/selection outside of Listbox?(如何避免 tkinter lt;lt;ListboxSelectgt;gt;和 .curselection() 检测列表框之外的事件/选择?)
      5. <i id='VqSiU'><tr id='VqSiU'><dt id='VqSiU'><q id='VqSiU'><span id='VqSiU'><b id='VqSiU'><form id='VqSiU'><ins id='VqSiU'></ins><ul id='VqSiU'></ul><sub id='VqSiU'></sub></form><legend id='VqSiU'></legend><bdo id='VqSiU'><pre id='VqSiU'><center id='VqSiU'></center></pre></bdo></b><th id='VqSiU'></th></span></q></dt></tr></i><div id='VqSiU'><tfoot id='VqSiU'></tfoot><dl id='VqSiU'><fieldset id='VqSiU'></fieldset></dl></div>
        <tfoot id='VqSiU'></tfoot>
          • <legend id='VqSiU'><style id='VqSiU'><dir id='VqSiU'><q id='VqSiU'></q></dir></style></legend>

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

                <bdo id='VqSiU'></bdo><ul id='VqSiU'></ul>
                  <tbody id='VqSiU'></tbody>

                • 本文介绍了如何避免 tkinter &lt;&lt;ListboxSelect&gt;&gt;和 .curselection() 检测列表框之外的事件/选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用以下 tkinter 小部件:EntryListbox.我希望 Entry 小部件在列表框中显示选定的项目 列表框配置为允许 selectmode=tk.SINGLE.选择由 tkinter 内置的虚拟事件 <<ListboxSelect>> 触发.我的测试脚本如下所示.

                  I am using the following tkinter widgets: Entry and Listbox. I want the Entry widget to display a selected item in the Listbox The Listbox is configured to allow selectmode=tk.SINGLE. Selection is triggered by tkinter built-in virtual event <<ListboxSelect>>. My test script is shown below.

                  条目 listbox 选择时被正确更新.但是,在发生 Listbox 选择后,我遇到了以下问题:

                  Entry is correctly updated when a Listbox selection is made. However, I am encountering the following issues after a Listbox selection has occurred:

                  1. 当鼠标指针在 Entry 中,并且发生 Left-Button Double Click 导致选择 Entry 中的项目时,抛出 2 个异常.
                  2. 如果鼠标指针放在除 tkinter GUI 之外的任何窗口上,例如显示测试脚本的 IDLE 或终端或 ....等,以及当我执行 Left-Button双击 然后在该窗口中选择一个单词,或者当我故意按下 鼠标左键 来选择一个段落时,会出现与 1. 中提到的相同的异常.
                  1. When the mousing pointer is in Entry, and a Left-Button Double Click takes place which leads to selecting the items in Entry, 2 exceptions is thrown up.
                  2. If the mouse pointer is place over any windows other than the tkinter GUI, example over IDLE where the test script is shown or over a terminal or ....etc., and when I do a Left-Button Double Click which then selects a word in that window or when I purposely depress the Left Mouse Button to select a paragraph, the same exceptions mentioned in 1. appears.

                  错误如下所示.

                  注意:当 Listbox 中没有选择时,不会出现上述问题.

                  Note: The above mentioned issues do not occur when there is no selection made in the Listbox.

                  如何避免这两个问题?我怀疑这些问题与 <<ListboxSelect>>widget.curselection() 允许在 Listbox 之外触发但不知道如何进一步调查.

                  How do I avoid these two issues? I suspect these issues are related to <<ListboxSelect>> and widget.curselection() is allowed to be triggered outside of Listbox the but don't know how to investigate this further.

                  我想要发生什么?

                  1. 不应注册鼠标指针在其他窗口或不在Listbox上的活动.
                  2. Entry 中双击不应影响Listbox 中的选择.
                  1. The activities of the mouse pointer when in other windows or not over Listbox should not be registered.
                  2. Double clicking in Entry should not affect the selection in the Listbox.

                  谢谢.

                  测试脚本:

                  import tkinter as tk
                  
                  class App(tk.Tk):
                      def __init__(self):
                          tk.Tk.__init__(self)
                          self.entrytext = tk.StringVar()
                          self.entry = tk.Entry(textvariable=self.entrytext)
                          self.listbox = tk.Listbox(selectmode=tk.SINGLE)
                          self.entry.pack(side="top", fill="x")
                          self.listbox.pack(side="top", fill="both", expand=True)
                          for i in range(100):
                              self.listbox.insert("end", "item %s" % i)
                          self.listbox.bind("<<ListboxSelect>>", self.ListboxSelect)
                  
                  
                      def ListboxSelect(self, event):
                          widget = event.widget
                          try:
                              selection=widget.curselection()
                              print('
                  selection = ', selection)
                              selection_index = int(selection[0])
                              print('selection_index = ', selection_index)
                              selection_value = widget.get(selection[0])
                              print("selection_value = {} ".format(selection_value))
                              self.entrytext.set(selection_value)
                          except:
                              raise
                  
                  
                  if __name__=='__main__':
                      a = App()
                      a.grid()
                  

                  例外情况:

                  Exception in Tkinter callback
                  Traceback (most recent call last):
                    File "/usr/lib/python3.5/idlelib/run.py", line 119, in main
                      seq, request = rpc.request_queue.get(block=True, timeout=0.05)
                    File "/usr/lib/python3.5/queue.py", line 172, in get
                      raise Empty
                  queue.Empty
                  
                  During handling of the above exception, another exception occurred:
                  
                  Traceback (most recent call last):
                    File "/usr/lib/python3.5/tkinter/__init__.py", line 1553, in __call__
                      return self.func(*args)
                    File "~/entry_listbox_example.py", line 22, in ListboxSelect
                      selection_index = int(selection[0])
                  IndexError: tuple index out of range
                  

                  推荐答案

                  如何避免 tkinter <<ListboxSelect>>.curselection() 检测列表框外的事件/选择?

                  How to avoid tkinter <<ListboxSelect>> and .curselection() detecting events/selection outside of Listbox?

                  这完全取决于您的意思.<<ListboxSelect>> 事件被明确设计为在选择更改时触发,无论它如何更改.这可能意味着用户在列表框中选择新的内容,或者从列表框中删除选择时.

                  That depends on exactly what you mean. The <<ListboxSelect>> event was explicitly designed to fire whenever the selection changes, no matter how it changes. That could mean when the user selects something new in the listbox, or when the selection is removed from the listbox.

                  你得到的错误是因为你假设有一个选择,这可能是也可能不是真的.您需要检查选择,并且仅在选择某些内容时运行您的代码.

                  The errors you get are because you assume that there is a selection, which may or may not be true. You need to check for a selection and only run your code if something is selected.

                  另一个解决方案,或整体解决方案的一部分,可能是将列表框的 exportselection 选项设置为 False.当设置为 True - 默认值 - 任何其他小部件获得选择时,选择将被取消设置.当设置为 False 时,选择不会因为另一个小部件选择了部分或全部数据而改变.

                  Another solution, or part of the overall solution, might be to set the exportselection option of the listbox to False. When set to True -- the default -- the selection will be unset whenever any other widget gets the selection. When set to False, the selection won't change just because another widget gets some or all of its data selected.

                  这篇关于如何避免 tkinter &lt;&lt;ListboxSelect&gt;&gt;和 .curselection() 检测列表框之外的事件/选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  env: python: No such file or directory(env: python: 没有这样的文件或目录)
                  How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                  Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                  Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                  Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                  When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)
                    <bdo id='tIJ6Z'></bdo><ul id='tIJ6Z'></ul>

                      <tfoot id='tIJ6Z'></tfoot>

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