Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

python - ScrolledText doesn't resize

It's the first time I'm using Tkinter and I'm trying to realize a simple GUI, with a ScrolledText on the top and a text field with a button on the bottom, so that it looks like what is shown in the first image below.

The thing that I don't understand is the following: When I tried to resize the window vertically, the text field and the button disappear, leaving just the scrollable text, as shown in the second image below.

But what I would expect is also the scrollable text to resize, leaving everything visible.


Preview


Preview


I've tried to use a fixed height for the scrollable text and I think all the weights of the columns/rows are OK, but I still can't find the issue. (The green and red backgrounds are there for being certain where the frames were.)

Here's the code I used:

import tkinter as tk
from tkinter import ttk
import tkinter.scrolledtext as scrolledtext

top = tk.Tk()

#   GRID CONFIG
top.grid_rowconfigure((0,1), weight=1)
top.grid_columnconfigure(0, weight=1)

#   FRAMES
UpFrame = tk.Frame(top, bg='red')
DownFrame = tk.Frame(top, bg='green')

UpFrame.grid(column=0, row=0, sticky='nsew')
DownFrame.grid(column=0, row=1, sticky='sew')

UpFrame.grid_columnconfigure(0, weight=1)
UpFrame.grid_rowconfigure(0, weight=1)

DownFrame.grid_columnconfigure((0,1), weight=1)

#   SCROLLABLE FRAME
scrollable_frame = scrolledtext.ScrolledText(UpFrame,
                                             wrap = tk.WORD,
                                             font = ("Times New Roman", 15))
scrollable_frame.grid(column=0, row=0, sticky='nsew')

#   TEXT
my_msg = tk.StringVar()
my_msg.set("")

#   TEXT FIELD AND SEND BUTTON
entry_field = tk.Entry(DownFrame, textvariable=my_msg)
entry_field.grid(row=0, column=0, sticky='nsew')
send_button = tk.Button(DownFrame, text="Send")
send_button.grid(row=0, column=1, sticky='nsew')

tk.mainloop()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem in your case is that you've given a weigth of 1 to row 1 of the root window. When you shrink the window to be smaller than what will fit, grid will remove space from each row equally.

If you only set the weight to one for row zero, the problem goes away.

top.grid_rowconfigure(0, weight=1)

This behavior is documented in the official documentation for grid:

For masters whose size is larger than the requested layout, the additional space is apportioned according to the row and column weights. If all of the weights are zero, the layout is placed within its master according to the anchor value. For masters whose size is smaller than the requested layout, space is taken away from columns and rows according to their weights.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...