1

If I call text_area.pack() before scrollbar.pack() (i.e switch them), the scrollbar doesn't show. Why is that? If I am going to create a larger program, I would have absolutely no chance to find out where the problem is.

from tkinter import * import tkinter.filedialog root = Tk() root.geometry("200x100") frame = Frame(root,width=150, height=90) frame.pack() scrollbar = Scrollbar(frame) text_area = Text(frame, width=200, height=50,yscrollcommand=scrollbar.set) scrollbar.config(command=text_area.yview) scrollbar.pack(side="right", fill="y") text_area.pack() root.mainloop() 
1
  • As Bryan pointed out, when you call root.geometry("200x100") you're overruling any size requirements inside that. That is also why you can see the scrollbar when the window is expanded, provided that you have large enough environment. Comment it out to see what happens. Commented Nov 13, 2017 at 13:32

3 Answers 3

2

The reason the scrollbar doesn't show is because there's simply no room for it. You're specifying a window size of 200x100 pixels and an inner frame size of 150x90 pixels, but you are trying to put a much larger text widget in that space. You're specifying a size of 200x50 characters (roughly 1400x750, depending on the fonts you're using) which is much too wide for the available space.

The way pack works is that it looks at the available space, puts the widget in that space, and subtracts the spaced needed for that widget from the space available for the next widget. Because you put the text widget first, and it requested more than the available space, it used up all of the available space. Then, when you call pack on the scrollbar, there's simply nowhere to put it.

When you reverse the order, the scrollbar takes up only a fraction of the available space, so there's room to fit the text widget in.

The best solution is to change the order in which you call pack. In general, it's best to call pack so that the last widget you call pack on is the "hero" -- the one that takes up all remaining space and grows or shrinks as the window grows and shrinks. Usually that's a text widget or a canvas widget, or a frame that itself contains many widgets.

The key to success with tkinter is to not try to force a tkinter widget or window to be a particular size in pixels (except, perhaps, for the canvas). Instead, either let a widget use it's default size (particularly with buttons and scrollbars), or pick a sensible default (number of rows and/or columns). Tkinter will then compute the right size to fit everything in based on the font, the screen resolution, and other factors.

Sign up to request clarification or add additional context in comments.

Comments

1

The other 2 answers are really good - I thought I would add an example. Personally, I don't like to use .pack - I like to place things instead like this: self.set_label_logpage.place(x=175, y=100)

Example code:

faultlogframe_logs = tk.Label(self, textvariable=logging_screen_label, font=Roboto_Normal_Font, height=180, width=400, background='white', foreground='black') faultlogframe_logs.place(x=605, y=600) self.scrollbar = Scrollbar(self, orient=VERTICAL, elementborderwidth=4, width=32) self.scrollbar.pack(pady=60,padx=0, ipady=4, ipadx=4, side=RIGHT, fill=Y) self.scrollbar_y = Scrollbar(self, orient=HORIZONTAL, width=12, takefocus=1) self.scrollbar_y.pack(expand=TRUE, ipady=9, ipadx=9, padx=0, pady=0, side=BOTTOM, fill=X, anchor=S) self.set_label_logpage = tk.Listbox(self, yscrollcommand=self.scrollbar.set, xscrollcommand=self.scrollbar_y.set) self.set_label_logpage.config(font=Roboto_Normal_Font, background='white', foreground='black', height=16, width=55) #textvariable=self.label_to_display_log self.set_label_logpage.place(x=175, y=100) self.scrollbar_y.config(command=self.set_label_logpage.xview) self.scrollbar.config(command=self.set_label_logpage.yview) 

Comments

0

When you switch them, scrollbar.pack() simply is unaware that it needs to go top, instead it goes next place from top to bottom, and right. You can see that when you expand window size.

You can resolve the issue by replacing:

text_area.pack() 

with:

text_area.pack(side="left") 

When you want to design more complex geometry structures I'd suggest you use grid instead of pack.

1 Comment

I did replace text_area.pack() with text_are.pack(side="left"), switched them and the scrollbar still doesn´t show up. Even if I try to resize the window etc. Here is the code: from tkinter import * import tkinter.filedialog root = Tk() root.geometry("200x100") frame = Frame(root,width=150, height=90) frame.pack() scrollbar = Scrollbar(frame) text_area = Text(frame, width=200, height=50,yscrollcommand=scrollbar.set) scrollbar.config(command=text_area.yview) text_area.pack(side="left") scrollbar.pack(side="right", fill="y") root.mainloop()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.