如何在Tkinter列表框中禁用水平滚动?(Python 3) [英] How can I disable horizontal scrolling in a Tkinter listbox? (Python 3)

查看:62
本文介绍了如何在Tkinter列表框中禁用水平滚动?(Python 3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说在Tkinter中,窗口中有一个特定大小的列表框.然后假设您向该列表框添加了一个大于该大小的字符串.如果突出显示此元素并拖动到侧面,列表框将自动滚动"自身,以便您可以看到完整的元素.无论如何,是否有必要通过运行反复尝试将滚动条设置为0的线程来禁用它?

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()

推荐答案

在按下按钮时,鼠标离开列表框会触发自动滚动.也许最简单的解决方案是通过创建自己的返回"break"的绑定来防止该行为:

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")

注意:这也将阻止垂直自动滚动.如果要保持垂直自动滚动,则必须编写一个更复杂的函数,如果鼠标位于列表框的左侧或右侧,该函数将仅返回"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.

这篇关于如何在Tkinter列表框中禁用水平滚动?(Python 3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆