无法在内部使用几何管理器包 [英] Cannot use geometry manager pack inside
问题描述
所以我正在使用 tkinter 库制作一个 rss 阅读器,并在我的一种方法中创建了一个文本小部件.它显示正常,直到我尝试向其添加滚动条.
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.
这是我在滚动条之前的代码:
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)
这是我的代码:
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: 不能在 .56155888 内使用几何管理器包,它已经在线上由网格管理从属vertscroll.pack(side="right", fill="y", expand=False)
_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)
有什么想法可以解决这个问题吗?
Any ideas how to fix this?
推荐答案
Per 文档、不要在同一个主窗口中混用pack
和grid
:
Per the docs, don't mix pack
and grid
in the same master window:
警告:切勿在同一个主窗口中混合网格和包装.特金特将愉快地度过你的余生试图谈判一个两位经理都满意的解决方案.与其等待,不如杀死应用程序,再看看你的代码.一个常见的错误是为某些小部件使用了错误的父级.
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
.
Thus, if you call grid
on the textbox, do not call pack
on the scrollbar.
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()
这篇关于无法在内部使用几何管理器包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!