如何在tkinter窗口的滚动条上使用.grid?

问题描述

我有一个tkinter窗口,其中包含几个使用网格的图像。我试图将滚动条网格化到窗口的右侧,但不知道使用什么选项,因为在网格内部无法使用“ side = right”,“ fill = Y”。有什么建议吗?

解决方法

网格面向行/列。因此,每个小部件都需要在几何管理器集中设置其行和列。这是一个演示,可以回答提出的问题。

# create a widget and scrollbar
import tkinter as tk
root = tk.Tk()
text = tk.Text(root)
vs = tk.Scrollbar(root,command=text.yview)
text.configure(yscrollcommand=vs.set)

# set the row,column and expansion for each widget
text.grid(row=0,column=0,sticky='news')
vs.grid(row=0,column=1,sticky='news')

# now make the grid manager expand to fill the available space in root.
# making 0,0 expand to use all the spare space and 0,1 be fixed
root.grid_rowconfigure(0,weight=1)
root.grid_columnconfigure(0,weight=1)

root.mainloop()

如果要在标签窗口小部件上管理一组图像,则将它们放在框架中以分离出这些图像的几何形状管理。您可能要考虑是否应该 而是将它们放到画布中,该画布具有很好的滚动支持。