简体   繁体   English

使用网格布局的Python Tkinter滚动条

[英]Python Tkinter Scrollbar using grid layout

i am tryingto make a simple gui that has two listboxes one list box is inside a scrollbar . 我试图做一个简单的GUI,它具有两个列表框,一个列表框位于滚动条中。 i am able to add all these to the window but i cant see the other listbox just the scrollbar. 我能够将所有这些添加到窗口,但我看不到其他列表框,只是滚动条。 i am using GRID layout 我正在使用GRID布局

here is my code : 这是我的代码:

from Tkinter import *
import glob, os

master = Tk()

master.resizable(width=FALSE, height=FALSE)
master.geometry('{}x{}'.format(400, 500))

listbox = Listbox(master)
listbox.grid(row=0,column=0)


listbox.insert(END, "a list entry")

for item in ["one", "two", "three", "four"]:
    listbox.insert(END, item)


data=Listbox(master,bg="grey")
scrollbar = Scrollbar(data, orient=VERTICAL)
data.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=data.yview)
for file in glob.glob("*.*"):
        print(file)
        data.insert(END,file)
        #data.grid(row=0,column=2)

data.grid(row=1, column=2, rowspan=4,
                   columnspan=2, sticky=N+E+S+W)
data.columnconfigure(3, weight=1)
scrollbar.grid(column=2, sticky=N+S)


master.mainloop()

The problem is that you are putting your scrollbar inside the listbox, which is the wrong way to do scrollbars. 问题是您将滚动条放在列表框中,这是滚动条的错误方法。 The problem is, when you use grid to place the scrollbar in the listbox, the listbox will shrink to fit the scrollbar. 问题是,当您使用网格将滚动条放置在列表框中时,列表框将缩小以适合滚动条。 And since the scrollbar doesn't have a defined type, the listbox shrinks to a very small size. 由于滚动条没有定义的类型,因此列表框会缩小到非常小的尺寸。

The solution is to make the scrollbar the child of a frame, not the child of a listbox. 解决方案是使滚动条成为框架的子项,而不是列表框的子项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM