[python]text與scrollbar的搭配使用

紀錄如何將text與scrollbar搭配使用。

import Tkinter as tk

class Mainapplication(tk.Frame):
	def __init__(self,master=None):
		tk.Frame.__init__(self,master)
		self.pack()
		self.createwidget()
		self.openfile()
		
	def createwidget(self):
		self.scrollbar = tk.Scrollbar(self)
		self.scrollbar.pack(side=tk.RIGHT,fill=tk.Y)
		
		self.text = tk.Text(self,yscrollcommand=self.scrollbar.set)
		self.text.pack(side=tk.LEFT,fill=tk.BOTH)
		
		self.scrollbar.config(command=self.text.yview)
		
	def openfile(self):
		with open('1111.txt','r') as f:
			temp = f.read()
			
		self.text.insert(tk.END,temp)


if __name__ =='__main__':
	root = tk.Tk()
	app = Mainapplication(master=root)
	root.mainloop()

 

By Jsy