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?
You cannot use both pack and grid on widgets that have the same master. The first one will adjust the size of the widget. The other will see the change, and resize everything to fit it's own constraints. The first will see these changes and resize everything again to fit its constraints.
The pack() method is one of three geometry managers in Tkinter. The other geometry managers are grid() and place() . The pack geometry manager has many configurations. The following are the most commonly used options: fill , expand , side , ipadx , ipady , padx , and pady .
The pack() fill option is used to make a widget fill the entire frame. The pack() expand option is used to expand the widget if the user expands the frame. fill options: NONE (default), which will keep the widget's original size. X, fill horizontally.
pack is the easiest layout manager to use with Tkinter. However, pack() is limited in precision compared to place() and grid() which feature absolute positioning. For simple positioning of widgets vertically or horizontally in relation to each other, pack() is the layout manager of choice.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With