Python Tkinter:更新滚动的列表框-滚动的滚动位置 [英] Python Tkinter: Update scrolled Listbox - wandering scroll position

查看:68
本文介绍了Python Tkinter:更新滚动的列表框-滚动的滚动位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一长串的字符串项目,这些列表会在可滚动的GUI窗口中定期更新.我喜欢使用免费的随附电池,但我曾经使用过Tkinter,但遇到了一些问题:

I wanted to display a long list of string items that is regularly updated in a scrollable GUI Window. Loving the use of free included batteries I used Tkinter, and ran into a few problems:

  • Tkinter列表框无法更新项目,您必须将其删除并重新插入.
  • 重新插入会使滚动条向上漂移
  • Tkinter初学者总数

我解决了这个问题,并将与大家分享解决方案作为对这个问题的答案.如果您知道更好的方法,请发布改进的答案.

I solved this, and will share the solution as an answer to this question. If you know a better way, please post an improved answer.

推荐答案

诀窍是保存列表框的yview,修改内容,然后移回该位置.因此,这是我使用Tkinter的解决方案:

The trick is, to save the yview of the listbox, modify the content, and then move back to that position. So, here is my solution using Tkinter:

from Tkinter import * # I do not like wild imports, but everyone seems to do it

root = Tk()

scrollbar = Scrollbar(root, orient=VERTICAL) # yes, do this first...
listb = Listbox(root, yscrollcommand=scrollbar.set) # ...because you need it here
scrollbar.config(command=listb.yview)
scrollbar.pack(side=RIGHT, fill=Y)
listb.pack(fill=BOTH, expand=YES) # convince the listbox to resize

item_list = range(400)

for item in item_list:
    listb.insert(item)

exponent = 0
def update_list():
    global exponent
    vw = listb.yview() # Save the current position(percentage) of the top left corner
    for i in item_list:
        listb.delete(i)
        listb.insert(i, i * 10**exponent)
    listb.yview_moveto(vw[0]) # go back to that position
    exponent += 1
    root.after(1000, update_list)

root.after(1000, update_list)
root.mainloop()

这篇关于Python Tkinter:更新滚动的列表框-滚动的滚动位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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