【问题标题】:How to use .grid with a scrollbar in a tkinter window?如何在 tkinter 窗口中使用带有滚动条的 .grid?
【发布时间】:2020-10-16 05:49:17
【问题描述】:

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

【问题讨论】:

  • 你可以使用place(relx=1, y=0, relheight=1, anchor='ne')
  • 或者将这些图像放在一个框架中,然后在框架和滚动条上使用pack()
  • grid 的所有选项都已记录在案。

标签: python-3.x tkinter scrollbar


【解决方案1】:

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

# 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()

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 2013-01-21
    • 2010-10-23
    • 1970-01-01
    • 2010-12-24
    相关资源
    最近更新 更多