内部不能使用几何管理器包 [英] Cannot use geometry manager pack inside

查看:479
本文介绍了内部不能使用几何管理器包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用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:不能使用几何管理呃打包里面.56155888已经有线上管理的从属
vertscroll.pack(side =right,fill =y,expand = False)



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

解决方案

Per 文档,请勿混合 pack 和<$在同一个主窗口中:c $ c> grid :


警告:切勿将网格和包混合在同一个窗口中主窗口。 Tkinter
将乐于花费你一生的余生,试图谈判双方经理都满意的
解决方案。不要等待,杀死应用程序的
,然后再看看你的代码。一个常见的错误
是为一些小部件使用错误的父项。


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






 将Tkinter导入为tk 
导入ttk

类应用程序(对象):
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()


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)

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)

Any ideas how to fix this?

解决方案

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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆