Python GUI Tk : Text
Text with ScrollBar
import Tkinter root = Tkinter.Tk() s = Tkinter.Scrollbar(root) T = Tkinter.Text(root) T.focus_set() s.pack(side=Tkinter.RIGHT, fill=Tkinter.Y) T.pack(side=Tkinter.LEFT, fill=Tkinter.Y) s.config(command=T.yview) T.config(yscrollcommand=s.set) for i in range(40): T.insert(Tkinter.END, "This is line %d\n" % i) Tkinter.mainloop()
Draw text
from Tkinter import * canvas = Canvas(width=300, height=300, bg='white') canvas.pack(expand=YES, fill=BOTH) canvas.create_text(100, 280, text='Ham') mainloop()
Draw text with font
from Tkinter import * root = Tk() #root.option_readfile('optionDB') root.title('Canvas') canvas = Canvas(root, width =400, height=400) canvas.create_text(350,150, text='text', fill='yellow', font=('verdana', 36)) canvas.pack() root.mainloop()
Scrolled Text
from Tkinter import * class ScrolledText(Frame): def __init__(self, parent=None, text='', file=None): Frame.__init__(self, parent) self.pack(expand=YES, fill=BOTH) self.makewidgets() self.settext(text, file) def makewidgets(self): sbar = Scrollbar(self) text = Text(self, relief=SUNKEN) sbar.config(command=text.yview) text.config(yscrollcommand=sbar.set) sbar.pack(side=RIGHT, fill=Y) text.pack(side=LEFT, expand=YES, fill=BOTH) self.text = text def settext(self, text='', file=None): if file: text = open(file, 'r').read() self.text.delete('1.0', END) self.text.insert('1.0', text) self.text.mark_set(INSERT, '1.0') self.text.focus() def gettext(self): return self.text.get('1.0', END+'-1c') if __name__ == '__main__': root = Tk() try: st = ScrolledText(file=sys.argv[1]) except IndexError: st = ScrolledText(text='Words\ngo here') def show(event): print repr(st.gettext()) root.bind('<Key-Escape>', show) root.mainloop()
Change color for tags
from Tkinter import * root = Tk() def hello(event): print 'Hi' text = Text() text.config(font=('courier', 15, 'normal')) text.config(width=20, height=12) text.pack(expand=YES, fill=BOTH) text.insert(END, 'Lin 1\n\nLin 2\n\nLin 3.\n\n') text.tag_add('demo', '1.5', '1.7') text.tag_add('demo', '3.0', '3.3') text.tag_add('demo', '5.3', '5.7') text.tag_config('demo', background='purple') text.tag_config('demo', foreground='white') text.tag_config('demo', font=('times', 16, 'underline')) text.tag_bind('demo', '<Double-1>', hello) root.mainloop()
Change font for Text
from Tkinter import * root = Tk() def hello(event): print 'HI' text = Text() text.config(font=('courier', 15, 'normal')) text.config(width=20, height=12) text.pack(expand=YES, fill=BOTH) text.insert(END, 'Lin 1\n\nLin 2\n\nLin 3.\n\n') text.tag_add('demo', '1.5', '1.7') text.tag_add('demo', '3.0', '3.3') text.tag_add('demo', '5.3', '5.7') text.tag_config('demo', font=('times', 16, 'underline')) text.tag_bind('demo', '<Double-1>', hello) root.mainloop()
Add double click action to a Text
from Tkinter import * root = Tk() def hello(event): print 'Got tag event' text = Text() text.config(font=('courier', 15, 'normal')) text.config(width=20, height=12) text.pack(expand=YES, fill=BOTH) text.insert(END, 'Lin 1\n\nLin 2\n\nLin 3.\n\n') text.tag_add('demo', '1.5', '1.7') text.tag_add('demo', '3.0', '3.3') text.tag_add('demo', '5.3', '5.7') text.tag_bind('demo', '<Double-1>', hello) root.mainloop()
Insert String to a Text
from Tkinter import * root = Tk() def hello(event): print 'Got tag event' text = Text() text.config(font=('courier', 15, 'normal')) text.config(width=20, height=12) text.pack(expand=YES, fill=BOTH) text.insert(END, 'Lin 1\n\nLin 2\n\nLin 3.\n\n') root.mainloop()
Text properties: expand and fill
from Tkinter import * root = Tk() def hello(event): print 'Got tag event' text = Text() text.config(font=('courier', 15, 'normal')) text.config(width=20, height=12) text.pack(expand=YES, fill=BOTH) text.insert(END, 'Lin 1\n\nLin 2\n\nLin 3.\n\n') root.mainloop()