The scrollbar does not stretch to fit the Text widget

I managed to get Scrollbar to work with the Text widget, but for some reason it does not stretch to fit the text field.

Does anyone know how to change the height of a scroll widget or something like that?

 txt = Text(frame, height=15, width=55) scr = Scrollbar(frame) scr.config(command=txt.yview) txt.config(yscrollcommand=scr.set) txt.pack(side=LEFT) 
+6
source share
3 answers

In your question you are using pack . pack has options to say that it grows or contracts in either the one or both x and y axes. Vertical scrollbars should usually increase / decrease along the y axis, and horizontal scroll bars should be along the x axis. Text widgets usually need to fill in both directions.

To create a text widget and scrollbar in a frame, you usually do something like this:

 scr.pack(side="right", fill="y", expand=False) text.pack(side="left", fill="both", expand=True) 

The following is said above:

  • the scroll bar is on the right ( side="right" )
  • the scroll bar must stretch to fill any extra space along the y axis ( fill="y" )
  • the text widget is on the left ( side="left" )
  • the text widget must be stretched to fill any extra space along the x and y axis ( fill="both" )
  • the text widget will expand to take up all the remaining space in the containing frame ( expand=True )

For more information see http://effbot.org/tkinterbook/pack.htm

+8
source

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 the text box, and sticky='ns' makes the scroll bar go up and down the window

+4
source

A simple solution for using a text box with a built-in scrollbar:

Python 3 :

 #Python 3 import tkinter import tkinter.scrolledtext tk = tkinter.Tk() text = tkinter.scrolledtext.ScrolledText(tk) text.pack() tk.mainloop() 

To read a text field:

 string = text.get("1.0","end") # reads from the beginning to the end 

Of course, you can cut back on imports if you want.

In Python 2 , import ScrolledText instead of you.

+4
source

Source: https://habr.com/ru/post/945113/


All Articles