Devin Maharjan
Devin Maharjan

Reputation: 109

How do we add a scroll bar when the entry boxes are more than the window height in tkinter?

First, when the number of rectangles is entered less than 24 it works fine. When I put the number of rectangles more than 24, the entry boxes exceed the window height and I cannot see it. I want to use a scroll bar at the right side when the number of entry boxes exceeds the window height. The documentation implies that only the List, Textbox, Canvas and Entry widgets support the scrollbar interface. So, how can I do for gridboxes?

Note : The code is verifiable.

from tkinter import *

temp_recta = []

def save_content(top):
    for j in range(0, int(rectangles.get()) * 2):
        temp_recta.append(rectangle_values[j].get())

    root = Toplevel()

    l1 = Label(root, text="Size of Sheet[Height]:")
    l1.grid(row=0, column=0)

    l2 = Label(root, text="Size of Sheet[Width]:")
    l2.grid(row=1, column=0)

    height = StringVar()
    e1 = Entry(root, textvariable=height)
    e1.grid(row=0, column=1)

    width = StringVar()
    e2 = Entry(root, textvariable=width)
    e2.grid(row=1, column=1)

    b5 = Button(root, text="Proceed", width=12, command=lambda: save_sheetsize(e1, e2, root))
    b5.grid(row=2, column=0)
    b6 = Button(root, text="Back", width=12, command=lambda: (root.destroy(), top.deiconify()))
    b6.grid(row=2, column=1)

    root.title("Sheet Size")

def save_sheetsize(e1, e2, root):
    global x
    global y
    x = float(e2.get())
    y = float(e1.get())
    root.destroy()
    window.destroy()

rectangle_values = []
x = 0
y = 0
k = 0

def open_window():
    window.withdraw()
    global k
    top = Toplevel()
    top.title("Rectangles")

    for i in range(0, int(rectangles.get()) * 2):
        if (i % 2) == 0:
            l4 = Label(top, text="Size of rectangle:")
            l4.grid(row=i, column=0)

        rectangle_values.append(StringVar())
        en = Entry(top, textvariable=rectangle_values[i])
        en.grid(row=i, column=1)


    b3 = Button(top, text="Save", width=12, command=lambda: (top.withdraw(), save_content(top)))
    b3.grid(row=int(rectangles.get()) * 2 + 1, column=0)
    b4 = Button(top, text="Back", width=12, command=lambda: (top.destroy(), window.deiconify()))
    b4.grid(row=int(rectangles.get()) * 2 + 1, column=1)
    k = int(rectangles.get())

window = Tk()

l3 = Label(window, text="Number of Rectangles:")
l3.grid(row=0, column=0)

rectangles = StringVar()
e3 = Entry(window, textvariable=rectangles)
e3.grid(row=0, column=1)

b1 = Button(window, text='Submit', width=12, command=open_window)
b1.grid(row=3, column=1)

window.title("Rectangle Configuration")
window.mainloop()

Upvotes: 1

Views: 379

Answers (1)

Kumar Saptam
Kumar Saptam

Reputation: 336

Here is a simple class to do this. Add your text widget and it will give you a scrollbar when your window is full of text.

import tkinter
class Scrollbar:
    def __init__(self,text):
        self.frame = text.master
        self.text = text
        self.text.configure(wrap='none')
        self.for_x_view()
        self.for_y_view()

    def for_x_view(self):
        # scroll Bar x For width
        scroll_x=tkinter.Scrollbar(self.frame, orient='horizontal',command=self.text.xview)
        scroll_x.config(command=self.text.xview)
        self.text.configure(xscrollcommand=scroll_x.set)
        scroll_x.pack(side='bottom', fill='x', anchor='w')
        return

    def for_y_view(self):
        # Scroll Bar y For Height
        scroll_y = tkinter.Scrollbar(self.frame)
        scroll_y.config(command=self.text.yview)
        self.text.configure(yscrollcommand=scroll_y.set)
        scroll_y.pack(side='right', fill='y')       
        return


if __name__ == '__main__':
    root = tkinter.Tk()
    pad = tkinter.Text(root,wrap='none')
    Scrollbar(pad)
    pad.pack()
    root.mainloop()

Upvotes: 0

Related Questions