gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:58:42 26 4
gpt4 key购买 nike

所以我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com