0

I am creating a form with 40 labels and entries. The problem is that I can enter till 20 after that the window size reaches the maximum and I cannot see entries below it. How do I integrate a scrolling option in Tkinter main window? I know that scrollbar cannot be integrated into main window and only to widgets. I want something so that I can either scroll using mouse or arrow keys or anything to see below content. Below is my code:

from Tkinter import *

root = Tk()
root.title('test')
root.geometry("400x400")

for i in range(40):
    Label(root, text="Field {} ".format(i)).grid(row=i,column=0)
    value=Entry(root).grid(row=i,column=1)

root.mainloop()

Output image

Ashish soni
  • 89
  • 1
  • 10

1 Answers1

0

ListBox

Scrollbars are almost always used in conjunction with Listbox, Canvas or Text widget. To connect a vertical scrollbar to one of these widgets you have to do two things:

  1. Set the widget’s yscrollcommand callbacks to the set method of the scrollbar.
  2. Set the scrollbar’s command to the yview method of the widget.

Example

from tkinter import *

master = Tk()

scrollbar = Scrollbar(master)
scrollbar.pack(side=RIGHT, fill=Y)

listbox = Listbox(master, yscrollcommand=scrollbar.set)

for i in range(40):
    
    listbox.insert(END, Label(master, text=f"Field {i} "))
    listbox.insert(END, Entry(master))

listbox.pack(side=LEFT, fill=BOTH)

scrollbar.config(command=listbox.yview)

mainloop()

  • `tkinter` widgets cannot be added to `Listbox` widget, only their names are inserted. Use `Text` widget instead. – acw1668 Sep 19 '20 at 14:04