Home > other >  How to scrolldown a textbox using scrollbar widget tkinter
How to scrolldown a textbox using scrollbar widget tkinter

Time:06-27

Application: Building a notes application (as an intro to GUI development in Python) which includes feature of a scrollbar to scroll through a textbox

Problem: I can't actually seem to scroll down through the textbox. I don't seem to get the grayed rectangle which lets me control the scrollbar and scroll up/down through the textbox

#importing necessary packages
from tkinter import *
from tkinter import font
from tkinter import ttk


#set up main window
root = Tk()

root.title("Notes")
root.geometry("400x650")



#functions

#functions to change all widget button's backgrounds when user hovers over it and leaves it
def enter_button(e):
    e.widget.config(background = "#D4D4D4")
    
#SystemButtonFace is default colour
def leave_button(e):
    e.widget.config(background = "SystemButtonFace")


#clear text in text-box

def clear():
    
    #delete all text from text_box
    text_box.delete(1.0,END)
    
def bold_it():
    
    #create font
    
    try: 
        
        
        bold_font = font.Font(text_box, text_box.cget("font"))
        bold_font.configure(weight = "bold")
    
        #creating tag called "bold" which bolds textll upon condition
        text_box.tag_configure("bold", font = bold_font)
    
        #creating a bold tag which highlights first character
        bold_tag = text_box.tag_names("sel.first")
    
        #condition for checking to see if tag is applied or not
        #in the first highlighted character
        #if tag is applied, remove the bold from first-highlighted text
        #- last highlighted text
        #"bold" needs to be matched in the tag
        if "bold" in bold_tag:
        
            text_box.tag_remove("bold","sel.first","sel.last")
        
        else:
        
            text_box.tag_add("bold","sel.first", "sel.last")
        
    except TclError: 
        
        pass

def italics_it():
    
    
    try:
    
        #create a font
        italics_font = font.Font(text_box, text_box.cget("font"))
        italics_font.configure(slant = "italic")
    
        #create a tag called "italic"
        text_box.tag_configure("italics", font = italics_font)
    
        italics_tag = text_box.tag_names("sel.first")
    
        #condition to see whether tag has been applies or not
        if "italics" in italics_tag:
        
            text_box.tag_remove("italics", "sel.first","sel.last")
        
        else: 
        
            text_box.tag_add("italics", "sel.first", "sel.last")
    
    except TclError: 
        
        pass



#frames
top_frame = LabelFrame(root, padx = 30, pady = 10)

button_frame = LabelFrame(root, padx = 30, pady = 10)

text_frame = LabelFrame(root, padx = 10, pady = 10)

bottom_frame = LabelFrame(root, borderwidth = 0, highlightthickness = 5)




top_frame.grid(row = 0 , column = 0)

button_frame.grid(row = 1, column = 0, pady = 10)

text_frame.grid(row = 2, column = 0, pady = 1)

bottom_frame.grid(row = 3, column = 0, pady = 3)


#labels, textboxes, buttons

#top_frame content
Notes_label = Label(top_frame, text = "Notes", fg = "black", font = 1, padx = 141)
Notes_label.grid(row = 0 , column = 0)

save_button = Button(top_frame, text = "save")
#padx increases distance between buttons


#button_frame content

#bold button
#the ideal is that if u press ctrl   b, the bold_button is pressed by itself
#rn, it's gonna be a highlight technique
bold_button = Button(button_frame, text = "B", padx = 4, pady = 2, command = bold_it)
bold_button.grid(row = 0, column = 0)

#italicsize button
italics_button = Button(button_frame, text = "I", padx = 4, pady = 2, command = italics_it)
italics_button.grid(row = 0, column = 2, padx = 15)

#text_box frame button
text_box = Text(text_frame, width = 45, height = 27)
text_box.grid(row = 0, column = 0)

#text_box frame content
main_scrollbar = ttk.Scrollbar(text_frame, orient = "vertical", command = text_box.yview)

main_scrollbar.grid(row = 0, column = 1)

text_box["yscrollcommand"] = main_scrollbar.set







clear_button = Button(bottom_frame, text = "clear", padx = 2, pady = 2, command = clear)
clear_button.grid(row = 0, column = 0, padx = 15, pady = 10)

save_button = Button(bottom_frame, text = "save note", padx = 2, pady = 2)
save_button.grid(row = 0, column =1, padx = 15, pady = 10)





#binding all buttons for changing colours when user hovers over it and leaves it
bold_button.bind("<Enter>", enter_button)
bold_button.bind("<Leave>", leave_button)
italics_button.bind("<Enter>", enter_button)
italics_button.bind("<Leave>", leave_button)
clear_button.bind("<Enter>", enter_button)
clear_button.bind("<Leave>", leave_button)
save_button.bind("<Enter>", enter_button)
save_button.bind("<Leave>", leave_button)


















# main program loop
root.mainloop()


here's an image of the problem enter image description here

CodePudding user response:

Maybe try adding brackets after the set

text_box["yscrollcommand"] = main_scrollbar.set

so it should look like

text_box["yscrollcommand"] = main_scrollbar.set()

  • Related