Scrollbar not stretching to fit the Text widget - python

I was able to get the Scrollbar to work with a Text widget, but for some reason it isn't stretching to fit the text box.
Does anyone know of any way to change the height of the scrollbar widget or something to that effect?
txt = Text(frame, height=15, width=55)
scr = Scrollbar(frame)
scr.config(command=txt.yview)
txt.config(yscrollcommand=scr.set)
txt.pack(side=LEFT)

In your question you're using pack. pack has options to tell it to grow or shrink in either or both the x and y axis. Vertical scrollbars should normally grow/shrink in the y axis, and horizontal ones in the x axis. Text widgets should usually fill in both directions.
For doing a text widget and scrollbar in a frame you would typically do something like this:
scr.pack(side="right", fill="y", expand=False)
text.pack(side="left", fill="both", expand=True)
The above says the following things:
scrollbar is on the right (side="right")
scrollbar should stretch to fill any extra space in the y axis (fill="y")
the text widget is on the left (side="left")
the text widget should stretch to fill any extra space in the x and y axis (fill="both")
the text widget will expand to take up all remaining space in the containing frame (expand=True)
For more information see http://effbot.org/tkinterbook/pack.htm

Here is an example:
from Tkinter import *
root = Tk()
text = Text(root)
text.grid()
scrl = Scrollbar(root, command=text.yview)
text.config(yscrollcommand=scrl.set)
scrl.grid(row=0, column=1, sticky='ns')
root.mainloop()
this makes a text box and the sticky='ns' makes the scrollbar go all the way up and down the window

Easy solution to use a textbox with an integrated scrollbar:
Python 3:
#Python 3
import tkinter
import tkinter.scrolledtext
tk = tkinter.Tk()
text = tkinter.scrolledtext.ScrolledText(tk)
text.pack()
tk.mainloop()
To read the textbox:
string = text.get("1.0","end") # reads from the beginning to the end
Of course you can shorten the imports if you want.
In Python 2 you import ScrolledText instead.

Related

How do I stop text from moving when I resize the screen in tkinter

I am creating a registration form, and I have coded labels to show next to the text box for someone's username and password. This is the code I am using to place the text boxes and labels:
usernamebx.place(relx=0.5, rely=0.5, width=225, height=25,
anchor= CENTER)
userbx_label.place(relx=0.1, rely=0.5, anchor=CENTER)
passwbx.place(relx=0.5, rely=0.6, width=225, height=25, anchor = CENTER)
passwbx_label.place(relx=0.1, rely=0.6, anchor=CENTER)
The code for usernamebx and passwbx means that the text boxes don't move when I resize the tkinter window. However, I have done the same with the labels for each but it doesn't work. Any help?
The code for usernamebx and passwbx means that the text boxes don't move when I resize the tkinter window.
Actually, they do move! If you put a widget at relx 0.5 in a window that is 200 pixels wide, that means the center of the widget will be 100 pixels from the left edge of the window. When you grow the window to 400 pixels wide, the center of the widget now will be 200 pixels from the left edge. It moved 100 pixels. You don't see it because it's symmetrical so it stays in the center.
The same happens with a widget that is at 0.1. on a 200 pixel wide window it's going to be 20 pixels from the left edge. When you make the window 400 pixels widget it's going to be 40 pixels from the edge.
This is the nature of relative coordinates -- they will always change when the window is resized.
It's hard to see what your actual requirement is, though I'm guessing you want the username label+entry and password label+entry to be co-aligned in the center of the window.
If that's the case, one simple solution is to put those widgets in a frame. Use grid internally since it appears that you are in fact creating a grid. Then, you can place the frame in the window as a separate step.
Here's an example of the technique. For illustrative purposes the frame has a visible border, but that's not strictly necessary. You can remove the border to make it blend in with the background.
This example uses place to put the frame in the center, though you can also use pack.
import tkinter as tk
root = tk.Tk()
root.geometry("400x400")
inner_frame = tk.Frame(root, bd=2, relief="groove")
usernamebx = tk.Entry(inner_frame)
userbx_label = tk.Label(inner_frame, text="Username:")
passwbx = tk.Entry(inner_frame)
passwbx_label = tk.Label(inner_frame, text="Password:")
inner_frame.grid_columnconfigure(0, weight=1)
userbx_label.grid(row=0, column=0, sticky="e")
usernamebx.grid(row=0,column=1, sticky="ew")
passwbx_label.grid(row=1, column=0, sticky="e")
passwbx.grid(row=1, column=1, sticky="ew")
inner_frame.place(relx=.5, rely=.5, anchor="center")
root.mainloop()
If you want to use pack rather than place, have the packer expand the allocated space to be the whole window, and the frame will automatically be centered. In this case the window will shrink to fit the frame plus the padding.
inner_frame.pack(side="top", expand=True, padx=10, pady=10)

Tkinter widget displacement with pack()

I want to pack two buttons (left and right) and a label (in the middle) in a frame. I want the label to fill the remaining space on the frame to both sides, but the widgets get displaced vertically with this code. What's the best way to do this? The widgets don't necessarily have to be packed on a frame but I want them to align horizontally while the text size of the label can change, but the buttons need to stay in place on the far left and right side. enter image description here
import tkinter as tk
root = tk.Tk()
root.geometry('600x800')
root.configure(background='#141414')
frm = tk.Frame(root)
frm.place(x=0, y=0, width=300, height=30)
btn1 = tk.Button(frm, text='button1')
lbl = tk.Label(frm, text='Lalalalalala')
btn2 = tk.Button(frm, text='button2')
btn1.pack(side='left')
lbl.pack(fill='x')
btn2.pack(side='right')
tk.mainloop()
You can solve this problem a couple of ways. One solution is to pack the label to one side or the other rather than the top.
btn1.pack(side='left')
lbl.pack(side='left', fill='x', expand=True)
btn2.pack(side='right')
Another is to pack the buttons first, and then pack the label. With pack the order matters.
btn1.pack(side='left')
btn2.pack(side='right')
lbl.pack(fill='x', expand=True)
For an illustrated explanation of how pack works see this answer to the question Tkinter pack method confusion

Python -> Tkinker -> TopLevel - auto size but with minimum width

I have a TopLevel(elements with Tkinker libiary) that has dynamically generated and added new items. The window adapts perfectly to the elements in height and width and I do not want to set a constant. However, the width is always a bit too narrow, so the elements fit, but the window looks bad.
Is it possible to set a minimum width or add permanent margins on the sides?
(Python 3)
The simplest solution is to put all of your widgets in a frame, then the frame inside the toplevel with padding around the edges.
The following example creates several labels inside the frame, and the frame is placed in the root window with a margin of 100 pixels all around it.
This example puts everything in the root window, but the same technique works with a Toplevel or any other widget.
import tkinter as tk
root = tk.Tk()
frame = tk.Frame(root, bd=1, relief="raised")
frame.pack(padx=100, pady=100)
for i in range(20):
label = tk.Label(frame, text=f"Widget #{i+1}")
label.pack()
root.mainloop()

TK, understanding pack and expanding space

I have a window that is resizeable, where i want to have two listboxes with scroll bars that expand to fill all space available.
when i have only one listbox packed as fill=both, expand=1, side=left and one scrollbar packed as fill=Y, expand=0, side=right then it will expand only horizontally, even though it is set to fill both directions. when i resize the window, the listbox only fill the sides. the bottom of the window remain empty.
then i moved on to add another listbox. Now instead of packing the scroll bar on right, i packed everything to left, so they are stacked. the listboxes continue to have fill=both, expand=1. Now when i resize the window both list boxes only fill vertically! the horizontal space remains empty.
what is going on? why does it ignore the vertical space with one element packed left and another right? and why it refuses to fill horizontally when everything is stacked left?
the fact that once it fill the vertical or the horizontal space leads me to believe the parent frame is expanding fine... or should i investigate that more as well?
Without seeing your actual code it's impossible to know what you're doing wrong. Here's an example to prove that pack works as documented:
import Tkinter as tk
root = tk.Tk()
lb1 = tk.Listbox(root)
lb2 = tk.Listbox(root)
vsb1 = tk.Scrollbar(root, orient="vertical", command=lb1.yview)
vsb2 = tk.Scrollbar(root, orient="vertical", command=lb2.yview)
lb1.configure(yscrollcommand=vsb1.set)
lb2.configure(yscrollcommand=vsb2.set)
lb1.pack(side="left", fill="both", expand=True)
vsb1.pack(side="left", fill="y", expand=False)
lb2.pack(side="left", fill="both", expand=True)
vsb2.pack(side="left", fill="y", expand=False)
root.mainloop()
workaround i'm using (downvote if not the right tk way)
i created two frames, both side=LEFT, expand=1, fill=BOTH and then put each pair of listbox+scrollbar there. now everything expands/fills just fine.
previously the listboxes were in the yellow frame. The ones i just created are the the blue and green.
still not sure with the pack manager would not expand the listboxes when they had scrollbars without expansion between them...

Why is there vertical padding in a Label?

I have a problem understanding where vertical padding in a widget comes from. The following code
import Tkinter as tk
import tkFont
currentfont = 'Muli'
root = tk.Tk()
# a single frame in root - the prod version has several
timestr = tk.Frame(root)
# the green background is added to show the limits of the Label
timeh = tk.Label(timestr, background='green', text="21")
timem = tk.Label(timestr, background='green', text="45")
timeh.grid(row=0, column=0)
timem.grid(row=0, column=1, sticky=tk.N)
timem.configure(font=(currentfont, 170))
font = tkFont.Font(family=currentfont, size=250, weight="bold")
timeh.configure(font=font)
timestr.grid(row=0, column=0, columnspan=3)
tk.mainloop()
produces
Why is there vertical padding above and below the glyphs? Is there a way to make them vertically fit into the window, similarly to how they fit horizontally?
The padding is likely part of the font, not the widget. Fonts need space above and below the space for average sized characters to make room for ascenders and descenders. The larger the font, the more space that is needed . Without the space below, for example, where would the bottom part of a lowercase "g" or "y" go?

Categories