Search code examples
python-3.xtkintertkinter-texttkinter-scrolledtext

Adding a resizable scroll-bar in Tkinter GUI


I am new to Tkinter. I am trying to add a scroll-bar in the GUI that I created using Tkinter. I added the scroll-bar using the following program. But, the problem is that, when I resize my window using mouse cursor, the size of the scrollbar also changes. I want the size of the scroll-bar to remain same in proportion to the size of the screen. I am not sure how to do that.

Also, there is no restriction that I have to use pack() I can use any other geometry manager.

the code:

import tkinter as tk


# Execute a multi-thread programming
class GUI():
    def __init__(self):

        # Define the default size of the window
        self.HEIGHT = 750
        self.WIDTH = 700
        self.play_pause = False

        # Define the GUI window.

        self.root = tk.Tk()
        self.root.title("GUI window")
        self.root.geometry(newGeometry=f"{self.WIDTH}x{self.HEIGHT}")
        self.root.minsize(self.WIDTH, self.HEIGHT)

        self.frame3 = tk.LabelFrame(self.root, text="Output window", height=450, width=700)
        self.frame3.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)

        self.output_text = tk.Text(self.frame3, height=42, fg="white", bg="black")
        self.output_text.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

        # Create a scroll bar for the Text widget
        v = tk.Scrollbar(self.frame3, orient='vertical', command=self.output_text.yview)
        v.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)


if __name__ == '__main__':
    app = GUI()
    app.root.mainloop()

The output that I am getting.

enter image description here

What happens when I resize

enter image description here


Solution

  • You need to turn off the expand option. Either accept the default, or explicitly set it to False. When set to True it tells pack to allocate extra space from the window to that widget, which you don't want.