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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23584325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:12:09  来源:igfitidea点击:

Cannot use geometry manager pack inside

pythontkintergeometryscrollbar

提问by user3623888

So I'm making an rss reader using the tkinter library, and in one of my methods I create a text widget. It displays fine until I try to add scrollbars to it.

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

Here is my code before the scrollbars:

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

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

Here is my code after:

这是我之后的代码:

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)

This gives me the error

这给了我错误

_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)

_tkinter.TclError:无法在 .56155888 中使用几何管理器包,该包已经在 vertscroll.pack(side="right", fill="y", expand=False) 行上由网格管理从属

Any ideas how to fix this?

任何想法如何解决这一问题?

采纳答案by unutbu

Per the docs, don't mix packand gridin the same master window:

根据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.

警告:切勿在同一个主窗口中混合网格和包。Tkinter 会很高兴地用你的余生来协商一个双方经理都满意的解决方案。与其等待,不如终止应用程序,然后再查看您的代码。一个常见的错误是为某些小部件使用了错误的父级。

Thus, if you call gridon the textbox, do not call packon the scrollbar.

因此,如果您调用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()

回答by ThePerson

The reason of the code is simple, you CANNOT use pack and grid inside the same class or for the same frame. Thus, use only one.

代码的原因很简单,你不能在同一个类或同一个框架中使用包和网格。因此,只使用一个。