text widget example?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wanwan

    #1

    text widget example?

    I need my GUI to open and display a text file. I'm thinking of using a
    text widget but it looks so complicated in the tkinter manual.

    question I have is:

    is there an example anyone can find on the internet?

  • jmdeschamps@gmail.com

    #2
    Re: text widget example?

    Well, in the 'give me a free example' type questions, I guess I'm a bit
    blue tonight, so here goes: ( no personal pun intended, just look up
    some recent post in this newsgroup...)

    # Start the example Python code to show some text file in a text widget
    from Tkinter import * # import Tkinter in __main__ namespace

    root = Tk() # initialize Tkinter

    myTextWidget= Text(root) # set up a text widget as a root (window)
    child

    myFile=file("my TextFile.txt") # get a file handle
    myText= myFile.read() # read the file to variable
    myFile.close() # close file handle

    myTextWidget.in sert(0.0,myText ) # insert the file's text into the text
    widget

    myTextWidget.pa ck(expand=1, fill=BOTH) # show the widget

    root.mainloop() #run the events mainloop
    # End the example here

    You need a text file *myTextFile.txt * in your current working directory
    for this to work.

    Read-up on the different objects and properties for detail.

    Good luck!

    BTW, now that this is posted, I guess one could say that it's an
    example on the Internet

    Comment

    • James Stroud

      #3
      Re: text widget example?

      On Sunday 06 November 2005 16:25, wanwan wrote:[color=blue]
      > I need my GUI to open and display a text file. I'm thinking of using a
      > text widget but it looks so complicated in the tkinter manual.
      >
      > question I have is:
      >
      > is there an example anyone can find on the internet?[/color]

      Couldn't find a good one, so here's a 5 minute version (your exercise is to
      put in a scroll bar). See:

      http://www.pythonware.com/library/tkinter/introduction/

      James

      from Tkinter import *
      import tkFileDialog
      import tkMessageBox

      class MyText(Frame):
      def __init__(self, parent=None, *args, **kwargs):
      Frame.__init__( self, parent)
      self.pack()
      self.text = Text(self, *args, **kwargs)
      self.text.confi g(background='w hite')
      self.text.pack( expand=YES, fill=BOTH)
      def get(self):
      return self.text.get(1 .0,"%s-1c" % END)
      def set(self, astr):
      self.text.delet e(1.0, END)
      self.text.inser t(INSERT, astr)
      def set_from_file(s elf):
      afilename = tkFileDialog.as kopenfilename()
      if afilename:
      try:
      afile = open(afilename)
      self.set(afile. read())
      afile.close()
      except Exception, e:
      tkMessageBox.sh owwarning("File Problem",
      "Couldn't read file '%s': %s" % (afilename, str(e)))

      def main():
      tk = Tk()
      tk.title('Text Reader App')
      atext = MyText(tk)
      atext.pack()
      open_button = Button(tk, text="Open File",
      activeforegroun d='blue',
      command=atext.s et_from_file)
      open_button.pac k()
      tk.mainloop()

      if __name__ == "__main__":
      main()




      --
      James Stroud
      UCLA-DOE Institute for Genomics and Proteomics
      Box 951570
      Los Angeles, CA 90095


      Comment

      Working...