简体   繁体   中英

Perfectly resize the Text widget with respect to the Tk() to show only the scroll bar

I am trying to recreate Notepad. This is my code:

#Importing everything from tkinter
from tkinter import *

#The root
app = Tk()
app.geometry("500x400")

#The text entry field and the scroll bar
y_scroll = Scrollbar(app)
y_scroll.pack(side = RIGHT, fill = Y)

text_area = Text(app, yscrollbar = y_scroll)
text_area.place(x = 0, y = 0, rel_width = 0.9, rel_height = 0.9)

#The app's mainloop
app.mainloop()

But it does not resize perfectly, and shows some blank area. How can I fix it and show only the scroll bar and the textarea? If I define the width, then the Text() widget does not resize when the app is resized. Is there any way to carry this out?

Don't use place. It's a bad habit to get into, as it forces you to do a lot more work in order to get a responsive UI. In this specific instance, pack is the best solution.

yscroll.pack(side="right", fill="y")
text_area.pack(side="left", fill="both", expand=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM