0

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! :)

Chiz
  • 7
  • 2
  • If you want the text area to be below the label, you need to call `.grid()` with the same column number for both widgets. – jasonharper Jul 24 '22 at 19:19
  • 2
    It looks like you're forcing the window to a specific size, and then putting in widgets that are too big to fit so the scrollbar becomes invisible. Have you tried either removing the call to `win.geometry`, making the geometry bigger, or making the default size of the scrolledtext smaller? There seems to be plenty of things to try. – Bryan Oakley Jul 24 '22 at 19:25
  • @BryanOakley Thank you! I removed the win.geometry and that worked. But when I resize the window it changes how could I set it to auto resize? – Chiz Jul 24 '22 at 19:29
  • it needs `win.grid_columnconfigure(1, weight=1)`, `win.grid_rowconfigure(1, weight=1)` - and this row and column will use all new space when you resize window. And ` `st.grid(..., sticky='nsew')` to resize `ScrolledText` to this new size of row and column. – furas Jul 25 '22 at 09:08

2 Answers2

0

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

furas
  • 134,197
  • 12
  • 106
  • 148
0

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:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19