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