How can I disable horizontal scrolling in a Tkinter listbox? (Python 3)

心已入冬 提交于 2021-02-10 16:11:18

问题


Say in Tkinter you have a listbox of a certain size within a window. Then let's say you add a string to that listbox that is larger than that size. If you highlight this element and drag to the side the listbox will automatically "scroll" itself so that you can see the full element. Is there anyway to disable this short of running a thread that repeatedly attempts to set the scroll to 0?

import tkinter
root = tkinter.Tk()
listbox = tkinter.Listbox(root)
listbox.insert("end", "Minimal, Complete, and Verifiable example")
listbox.pack()
root.mainloop()
quit()

回答1:


The auto-scrolling is triggered by the mouse leaving the listbox while the button is pressed. Perhaps the simplest solution is to prevent that behavior by creating your own binding that returns "break":

listbox.bind("<B1-Leave>", lambda event: "break")

Note: this will also prevent the vertical auto-scrolling. If you want to keep the vertical auto-scrolling than you'll have to write a more complex function that will only return "break" if the mouse is to the left or right of the listbox.



来源:https://stackoverflow.com/questions/47565987/how-can-i-disable-horizontal-scrolling-in-a-tkinter-listbox-python-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!