如何将滚动条附加到列表框小部件 [英] How to attach my scrollbar to my listbox widget

查看:72
本文介绍了如何将滚动条附加到列表框小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想要成为的照片:

here is a picture of what i want to be:

滚动条

实际代码:

lb = Listbox(self.master, width=120, height=6)
scrollbar = Scrollbar(self.master, orient="vertical",command=lb.yview)
scrollbar.pack(side="right", fill="y")
lb.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=lb.yview)
lb.place(x=5,y=5)

谢谢!

推荐答案

您可以使用列表框和滚动条创建一个新框架:

You can create a new frame with listbox and scrollbar in it:

from tkinter import *

root = Tk()
root.geometry('500x300')

frame = Frame(root)
frame.place(x = 5, y = 5) # Position of where you would place your listbox

lb = Listbox(frame, width=70, height=6)
lb.pack(side = 'left',fill = 'y' )

scrollbar = Scrollbar(frame, orient="vertical",command=lb.yview)
scrollbar.pack(side="right", fill="y")

lb.config(yscrollcommand=scrollbar.set)

for i in range(10):
    lb.insert(END, 'test'+str(i))

root.mainloop()

,或者由于使用的是place(不建议使用),因此您可以简单地计算滚动条的位置.在这种情况下,grid将是最好的布局管理器.

or since you're using place (which is not recommended), you can simply calculate the position of the scrollbar. grid would be the best layout manager in this case.

这篇关于如何将滚动条附加到列表框小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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