Cannot use geometry manager pack inside
Asked Answered
J

3

46

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?

Johnnie answered 10/5, 2014 at 17:40 Comment(0)
V
72

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()
Ventilator answered 10/5, 2014 at 18:8 Comment(3)
Thanks! Shouldn't the sticky be NSE though?Johnnie
You could use NSE, but in this case I don't think it matters.Ventilator
Link to the docs as of June 2024: dafarry.github.io/tkinterbook/grid.htmStroh
I
9

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.

Incurve answered 22/9, 2017 at 19:28 Comment(4)
Why "only one throughout the code"? Each has a purpose, and strengths and weaknesses. Most complex GUIs will be easier to write when you use both.Clayson
Its possible to have "both throughout the code" . When you make a complex GUI, you use many classes (inhereting from the main class i.e. App). Each of these classes have their own sets of widgets. Thus, you can use pack in some classes and grid in some by dividing the components.Incurve
Yes, I know that. What I was asking is why you recommend to only use one throughout the code.Clayson
You can make different classes, for different pages. Then, you can use pack for some pages and, grid for some other.Incurve
A
2

You can't use pack and grid inside the same class or the same frame.use only one

Apotropaic answered 2/7, 2020 at 19:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.