1

Im trying to use a vertical scrollbar for my text box but am coming across some problems:

  1. I cant get the scroll bar to be directly touching the right side of the text box (so they are connected)
  2. It seems the scroll bar wont affect my text box

I looked through some solutions but none seemed to work. Heres my code:

from tkinter import *

writtenQ = Tk()
writtenQ.title("Written Response Question")
writtenQ.resizable(0,0)

header = LabelFrame(writtenQ, bg="white")
content = LabelFrame(writtenQ, bg="white")

header.columnconfigure(0, weight=1) # Forces column to expand to fill all available space
homeButton=Button(content,width=50,height=50)
try:
    homeIcon=PhotoImage(file="yes.png")
    homeButton.config(image=homeIcon)
    homeButton.image = homeIcon
except TclError:
    print("Home")
homeButton.grid(row=1, sticky="w", padx=15, pady=2)

#the image of the question will be put here
titleHeader = Label(content, text="Question Image here",pady=15, padx=20, bg="white", font=("Ariel",20, "bold"), anchor="w", relief="solid", borderwidth=1)
titleHeader.grid(row=2, column=0, columnspan=3, padx=15, pady=5, ipadx=370, ipady=150)

#this will allow the user to input their written response
answerInput = Text(content, width = 60, borderwidth=5, font=("HelvLight", 18)) 
answerInput.grid(row=3, column=0, ipady = 10, sticky="w", padx=(20,0), pady=20)
answerScrollBar= Scrollbar(content, command=answerInput.yview, orient="vertical")
answerScrollBar.grid(row=3, column=1, sticky="w")

submitButton = Button(content, borderwidth=1, font=("Ariel", 22), text="Submit", bg="#12a8e3", fg="black", activebackground="#12a8e3", relief="solid")
submitButton.grid(row=3, column=2, ipady=50, ipadx=70, sticky="nw", pady=20)

header.grid(row=0, sticky='NSEW')
content.grid(row=1, sticky='NSEW')

  • Set `sticky="nsw"` and `pady=20` for the scrollbar. However, better put the `Text` and `Scrollbar` together inside a `Frame`. – acw1668 Jun 26 '20 at 09:56

2 Answers2

2

Configuring a scrollbar requires a two-way connection: the scrollbar needs to call the yview or xview method of the widget, and the widget needs to call the set method of the scrollbar.

Usually, this is done in three steps like in the following example:

answerInput = Text(...)
answerScrollBar= Scrollbar(..., command=answerInput.yview)
answerInput.configure(yscrollcommand=answerScrollBar.set)

You are forgetting the final step.


Unrelated to an actual functioning scrollbar, you're going to want to be able to see the scrollbar. You need to use sticky="ns" for the scrollbar so that it stretches in the Y direction. Otherwise it will only be a couple dozen pixels tall.

answerScrollBar.grid(row=3, column=1, sticky="ns")
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • thanks this fixed it. Do you know how to set the size of the text box? It seems to expand infinitely and i tried putting it into a fixed sized frame but it would still expand vertically – alvindraper Jun 27 '20 at 16:30
1

Have you tried the solution here?

Let's say that the text widget is called text. Your code could be (excluding the setup of the window):

import tkinter
import tkinter.ttk as ttk
scrollb = ttk.Scrollbar(self, command=text.yview)
scrollb.grid(row=0, column=1, sticky='nsew')
text['yscrollcommand'] = scrollb.set

I have picked out what I think will be ueful for you from Honest Abe's answer. Hope it helped. Remember to set up your window before using the code...

jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
Thomas
  • 1,214
  • 4
  • 18
  • 45