Wednesday, April 29, 2020

How to create scroll bar on python tkinter and use Keys on keyboard to move up or down

import tkinter
from tkinter import *
import docx
root = Tk()
file=docx.Document("New Microsoft Word Belgesi.docx")
array=[]
for i in file.paragraphs:           
        line=i.text
        array.append(line)
       
mylabel=Label(root)
mylabel.grid()
text=Text(mylabel)
text.grid(row=0,column=1)
scrollbar =Scrollbar(mylabel,command=text.yview)
text.config(yscrollcommand=scrollbar.set)
scrollbar.grid(row=0,column=0,sticky=NSEW)
text.insert(END,array)

def key_pressed(event):
    print(event.keysym)
    if str(event.keysym)=="Down":
        text.yview_scroll(5, "units")
    elif str(event.keysym)=="Up":
        text.yview_scroll(-5, "units")
       
root.bind("<Key>",key_pressed)

root.mainloop()