Scrolled Text : Text « GUI Tk « Python

Home
Python
1.2D
2.Application
3.Buildin Function
4.Class
5.Data Structure
6.Data Type
7.Database
8.Development
9.Dictionary
10.Event
11.Exception
12.File
13.Function
14.GUI Pmw
15.GUI Tk
16.Language Basics
17.List
18.Math
19.Network
20.String
21.System
22.Thread
23.Tuple
24.Utility
25.XML
Python » GUI Tk » TextScreenshots 
Scrolled Text
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()



           
         
  
Related examples in the same category
1.Draw textDraw text
2.Draw text with fontDraw text with font
3.Change color for tagsChange color for tags
4.Change font for TextChange font for Text
5.Add double click action to a TextAdd double click action to a Text
6.Insert String to a TextInsert String to a Text
7.Text properties: expand and fillText properties: expand and fill
8.Text with ScrollBarText with ScrollBar
9.Show a file in a text widget
10.Search string in TextSearch string in Text
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.