473,382 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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?

Nov 7 '05 #1
2 11022
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("myTextFile.txt") # get a file handle
myText= myFile.read() # read the file to variable
myFile.close() # close file handle

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

myTextWidget.pack(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

Nov 7 '05 #2
On Sunday 06 November 2005 16:25, wanwan wrote:
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?


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.config(background='white')
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.delete(1.0, END)
self.text.insert(INSERT, astr)
def set_from_file(self):
afilename = tkFileDialog.askopenfilename()
if afilename:
try:
afile = open(afilename)
self.set(afile.read())
afile.close()
except Exception, e:
tkMessageBox.showwarning("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",
activeforeground='blue',
command=atext.set_from_file)
open_button.pack()
tk.mainloop()

if __name__ == "__main__":
main()


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

http://www.jamesstroud.com/
Nov 7 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: T. Kaufmann | last post by:
Hi there, I have some lines of code in a Tkinter programm - I want to list every single file of a zip-archive in a Text widget: fp = os.popen("%s %s" % ('unzip -vl ', 'anyarchiv.zip', 'r')...
7
by: Jane Austine | last post by:
As you add more items, say text lines, in Text widget, it gets too slow and almost impractical to use on. Take idle for example. If the text gets bigger(e.g. print...
7
by: Beps | last post by:
hello, I have got a data list. In my Text widget each data is between the previous and the next; so they are whole in a only block. Now: |-------------------- |BushBerlusconiBlair|...
2
by: Tonino | last post by:
Hi, I have a small Tkinter app that gets data from a socket connection to a "server". The app has a Text() widget to display the info that it gets from the socket connection. I have the...
4
by: Lou G | last post by:
I'm trying to show a number of Checkbuttons (each with associated text based on a list of names) inside a y-scrollable Text widget like so: Bob Carol Ted Alice etc. etc.
4
by: sullivanz.pku | last post by:
Hi all I am using the standard python GUI Tkinter as my program's main interface. Although I know wxPython has some widget to support rich text widget, but I do not have time to shift to wx----...
3
by: mariox19 | last post by:
Are Tkinter widgets running on their own thread? If I try to make a simple application that will print the letters A to Z to a Tkinter Text widget, and I space the printing of each letter by 1...
0
by: Alex Bryan | last post by:
Hey guys. I am having trouble understanding the get() method from the Tkinter Text() widget. It isn't like the entry.get() one I am used to. I know you have to put tags in, or at least I read. I...
0
by: John McMoangle | last post by:
Hey guys. I am having trouble understanding the get() method from The arguments to the get method of the Text widget are strings in the form of "line.position". So, text.get('1.5', '2.7')...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.