簡體   English   中英

如何使用滾動條小部件 tkinter 向下滾動文本框

[英]How to scrolldown a textbox using scrollbar widget tkinter

應用程序:構建一個筆記應用程序(作為 Python 中 GUI 開發的介紹),其中包括滾動條功能以滾動文本框

問題:我實際上似乎無法向下滾動文本框。 我似乎沒有得到灰色的矩形,它可以讓我控制滾動條並向上/向下滾動文本框

#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()


這是問題的圖像問題的圖像

如果有人能解釋 scrollbar.set 和 yview 的概念,以及為什么滾動條工作都需要它們,我也將不勝感激。 教程和視頻似乎沒有解釋這個概念,但只是實現它

在第 145 行。你缺少sticky

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

輸出:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM