前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python scroll listbox 例子

python scroll listbox 例子

作者头像
用户5760343
发布2022-05-13 10:54:31
8040
发布2022-05-13 10:54:31
举报
文章被收录于专栏:sktj

image.png

"a simple customizable scrolled listbox component"

from tkinter import *

class ScrolledList(Frame): def init(self, options, parent=None): Frame.init(self, parent) self.pack(expand=YES, fill=BOTH) # make me expandable self.makeWidgets(options)

代码语言:javascript
复制
def handleList(self, event):
    index = self.listbox.curselection()                # on list double-click
    label = self.listbox.get(index)                    # fetch selection text
    self.runCommand(label)                             # and call action here
                                                       # or get(ACTIVE)
def makeWidgets(self, options):
    sbar = Scrollbar(self)
    list = Listbox(self, relief=SUNKEN)
    sbar.config(command=list.yview)                    # xlink sbar and list
    list.config(yscrollcommand=sbar.set)               # move one moves other
    sbar.pack(side=RIGHT, fill=Y)                      # pack first=clip last
    list.pack(side=LEFT, expand=YES, fill=BOTH)        # list clipped first
    pos = 0
    for label in options:                              # add to listbox
        list.insert(pos, label)                        # or insert(END,label)
        pos += 1                                       # or enumerate(options)
   #list.config(selectmode=SINGLE, setgrid=1)          # select,resize modes
    list.bind('<Double-1>', self.handleList)           # set event handler
    self.listbox = list

def runCommand(self, selection):                       # redefine me lower
    print('You selected:', selection)

if name == 'main': options = (('Lumberjack-%s' % x) for x in range(20)) # or map/lambda, [...] ScrolledList(options).mainloop()

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档