python - 无法在内部使用几何管理器包

标签 python tkinter geometry scrollbar

所以我正在使用 tkinter 库制作一个 rss 阅读器,并且在我的一种方法中我创建了一个文本小部件。它显示正常,直到我尝试向它添加滚动条。

这是我在滚动条之前的代码:

   def create_text(self, root):
        self.textbox = Text(root, height = 10, width = 79, wrap = 'word')
        self.textbox.grid(column = 0, row = 0)

下面是我的代码:

def create_text(self, root):
        self.textbox = Text(root, height = 10, width = 79, wrap = 'word')
        vertscroll = ttk.Scrollbar(root)
        vertscroll.config(command=self.textbox.yview)
        vertscroll.pack(side="right", fill="y", expand=False)
        self.textbox.config(yscrllcommand=vertscroll.set)
        self.textbox.pack(side="left", fill="both", expand=True)
        self.textbox.grid(column = 0, row = 0)

这给了我错误

_tkinter.TclError: cannot use geometry manager pack inside .56155888 which already has slaves managed by grid on the line vertscroll.pack(side="right", fill="y", expand=False)

有什么办法解决这个问题吗?

最佳答案

根据 the docs ,不要在同一个主窗口中混合使用 packgrid:

Warning: Never mix grid and pack in the same master window. Tkinter will happily spend the rest of your lifetime trying to negotiate a solution that both managers are happy with. Instead of waiting, kill the application, and take another look at your code. A common mistake is to use the wrong parent for some of the widgets.

因此,如果您在文本框上调用grid,请不要在滚动条上调用pack


import Tkinter as tk
import ttk

class App(object):
    def __init__(self, master, **kwargs):
        self.master = master
        self.create_text()

    def create_text(self):
        self.textbox = tk.Text(self.master, height = 10, width = 79, wrap = 'word')
        vertscroll = ttk.Scrollbar(self.master)
        vertscroll.config(command=self.textbox.yview)
        self.textbox.config(yscrollcommand=vertscroll.set)
        self.textbox.grid(column=0, row=0)
        vertscroll.grid(column=1, row=0, sticky='NS')

root = tk.Tk()
app = App(root)
root.mainloop()

关于python - 无法在内部使用几何管理器包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23584325/

相关文章:

python - Tkinter:按钮打开另一个窗口(并关闭当前窗口)

python - Tk 窗口不显示任何内容

opengl - OpenGL立方体贴图中的面约定

python - virtualenv 没有匹配的分布

python - 多线程Python退出

python - 通过 shapefile 切割 NetCDF 文件

python - qPython 中的多个 KDB+ 股票行情订阅

python - 带有两个 slider 的 tkinter 刻度?

algorithm - Geometry:计算几何,计算所有的 friend 点对

python - 将圆柱体拟合到分散的 3D XYZ 点数据