Reputation: 7
I'm having some issues with a ScrolledText widget in tkinter.
The Scrollbar is not showing up.
Here is my code:
from tkinter import *
import os
from tkinter.scrolledtext import ScrolledText
#window variables
win = Tk()
win.title('TextS')
win_width = 600
win_height = 400
win.geometry('{}x{}'.format(win_width,win_height))
#widgets
title = Label(win, text="TextS", font=("Helvetica", 36, "bold"), bg="lightgray")
title.grid(row=0, column=0)
st = ScrolledText(win, width=400, height=300)
st.grid(column=1, pady=10, padx=10)
#main loop
while True:
win.update()
Some Screen Shots:
With .grid() what I want to use: screenshot
With .pack() which I do not want to use: screenshot (Has scroll bar)
Sorry if this is a noobie mistake, Thank you!
Have a great day! :)
Upvotes: 0
Views: 835
Reputation: 4545
Using same code.
set geometry
to 350x300
In line 20 set ScrolledText
to width=40
, height=10
Set column
to 0 not 1
You're set to go.
Code:
from tkinter import *
import os
from tkinter.scrolledtext import ScrolledText
#window variables
win = Tk()
win.title('TextS')
win_width = 600
win_height = 400
win.geometry('350x300')
#widgets
title = Label(win, text="TextS", font=("Helvetica", 36, "bold"), bg="lightgray")
title.grid(row=0, column=0)
st = ScrolledText(win, width=40, height=10)
st.grid(column=0, pady=10, padx=10)
#main loop
while True:
win.update()
win.mainloop()
Screenshot output:
Upvotes: 0
Reputation: 142641
ScrolledText
uses width
and height
in chars, not in pixels - so you create very big widget and scroller doesn't fit in window. You have to use smaller values.
grid
doesn't use free space when you resize (all of them use value weight=0
. You have to use bigger weight
to inform which row and column has to use this free space when you resize.
win.grid_columnconfigure(1, weight=1) # column 1
win.grid_rowconfigure(1, weight=1) # row 1
Now it will resize cells in row 1 and column 1 but it doesn't resize widgets in cells. Widgets needs grid(..., sticky='nsew')
to resize in all directions.
import tkinter as tk # PEP8: `import *` is NOT preferred
from tkinter.scrolledtext import ScrolledText
# window variables # PEP8: one space after `#`
win = tk.Tk()
win.grid_columnconfigure(1, weight=1)
win.grid_rowconfigure(1, weight=1)
title = tk.Label(win, text="TextS", font=("Helvetica", 36, "bold"), bg="lightgray")
title.grid(row=0, column=1)
st = ScrolledText(win, width=100, height=25)
st.grid(column=1, pady=10, padx=10, sticky='nsew')
# main loop
win.mainloop()
PEP 8 -- Style Guide for Python Code
Upvotes: 0