[python]示範如何使用tkinter的text widget

當中會使用

1.text的insert, get方法

2.如何從頭插入,從尾插入以及取得整個text當中的內容

import Tkinter as tk
class Mainapplication(tk.Frame):
	def __init__(self,master=None):
		tk.Frame.__init__(self,master)
		self.pack()
		self.createwidget()
		
	def createwidget(self):
		self.label = tk.Label(self,text="hello world")
		self.label.pack()
		self.text = tk.Text(self,height=5)
		self.text.pack()
		self.button = tk.Button(self,text="OK",command=self.addcontent)
		self.button.pack()
		self.button2 = tk.Button(self,text="OK2",command=self.addcontent2)
		self.button2.pack()
		self.button3 = tk.Button(self,text="OK2",command=self.addcontent3)
		self.button3.pack()
	
	#將文字插入最前面(insert)
	def addcontent(self):
		self.text.insert(1.0,"OK\n")
	
	#將文字插入最後
	def addcontent2(self):
		self.text.insert(tk.END,"NO")
	
	#取得整個text當中的內容(get)
	def addcontent3(self):
		temp = self.text.get(1.0,tk.END)
		print temp

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

 

By Jsy