如何滚动非活动的Tkinter ListBox?(How to scroll an inactive Tkinter ListBox?)

编程入门 行业动态 更新时间:2024-10-28 02:36:17
如何滚动非活动的Tkinter ListBox?(How to scroll an inactive Tkinter ListBox?)

我正在用Python编写Tkinter GUI。 它有一个用于在其下面搜索结果ListBox的条目。 ListBox还有一个滚动条。 如何使用鼠标和箭头键滚动才能在ListBox中工作,而无需将焦点从搜索字段中移开? IE我希望用户能够键入搜索,滚动并继续键入,而无需在窗口小部件之间来回切换。 谢谢

I'm writing a Tkinter GUI in Python. It has an Entry for searching with a results ListBox below it. The ListBox also has a Scrollbar. How can I get scrolling with the mouse and arrow keys to work in the ListBox without switching focus away from the search field? IE I want the user to be able to type a search, scroll around, and keep typing without having to tab back and forth between widgets. Thanks

最满意答案

将绑定添加到调用列表框yview的条目小部件和/或在用户按下或按下或使用向上/向下yview时see命令。

例如,您可以为箭头键执行以下操作:

class App(Tkinter.Tk): def __init__(self): Tkinter.Tk.__init__(self) self.entry = Tkinter.Entry() self.listbox = Tkinter.Listbox() 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.entry.bind("<Down>", self.OnEntryDown) self.entry.bind("<Up>", self.OnEntryUp) def OnEntryDown(self, event): self.listbox.yview_scroll(1,"units") def OnEntryUp(self, event): self.listbox.yview_scroll(-1,"units")

Add bindings to the entry widget that call the listbox yview and/or see commands when the user presses up and down or uses the up/down scrollwheel.

For example, you can do something like this for the arrow keys:

class App(Tkinter.Tk): def __init__(self): Tkinter.Tk.__init__(self) self.entry = Tkinter.Entry() self.listbox = Tkinter.Listbox() 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.entry.bind("<Down>", self.OnEntryDown) self.entry.bind("<Up>", self.OnEntryUp) def OnEntryDown(self, event): self.listbox.yview_scroll(1,"units") def OnEntryUp(self, event): self.listbox.yview_scroll(-1,"units")

更多推荐

ListBox,搜索,search,电脑培训,计算机培训,IT培训"/> <meta name="description

本文发布于:2023-07-09 15:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1087171.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ListBox   Tkinter   inactive   scroll

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!