Tkinter का उपयोग text file को render करके canvas पर load करने के लिए भी किया जा सकता है। इसके अलावा, text files का उपयोग अन्य उद्देश्यों के लिए किया जा सकता है जैसे data manipulate करना, data grab करना, और अन्य उपयोगों के लिए data render करना।
मान लीजिए हमें tkinter canvas file में एक text read करना है जिसमें 10,000 से अधिक lines of queries हैं। Text file load करने के बाद canvas में किसी particular query को search करने में लंबा समय लगेगा। ऐसी बड़ी text files को handle करने के लिए, हम इसमें Y scrollbar जोड़कर file की responsiveness speed up कर सकते हैं। हम Scrollbar Widget का उपयोग करके side controller widget बनाएंगे।
पहले, हम "open" method का उपयोग करके file open और read करेंगे, फिर tkinter frame के Y-axis में scrollbar जोड़ेंगे। Frame में scrollbar जोड़ने के लिए, हम Scrollbar widget का उपयोग करके इसका instance बना सकते हैं। यह window instance को parameter के रूप में लेता है और Scrollbar के अन्य property (side, axis) define करता है।
उदाहरण
# Importing the tkinter library in the notebook
from tkinter import *
# Create an instance of the tkinter frame
win = Tk()
win.geometry("700x300")
# Create instance of Scrollbar object and define the property of the scrollbar
scrollbar = Scrollbar(win)
scrollbar.pack(side=RIGHT, fill=Y)
listbox = Listbox(win, height=300, width=100)
listbox.pack()
# Open and read the file using open method
file = open('file.txt', 'r').readlines()
for i in file:
listbox.insert(END, i)
# Define the property of the widget
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
# display the canvas until the END button is not pressed.
mainloop()
आउटपुट
उपरोक्त code snippet चलाने से canvas खुलेगा जिसके side में scrollbar होगा।
