The Tk Text Widget
All script writers need to deal with textual data at one time or another. One of the most powerful tools for manipulating text in the free software world is the text widget found in the Tk graphical user interface (GUI) toolkit. This widget is available to script writers working with Tcl, Perl/Tk and Tkinter in Python, and it boasts features and functionality that can solve almost any text-related requirement a script writer is likely to encounter.
Key features include multi-line text display and editing, comprehensive text formatting, embedded images, embedded widgets and a unique, almost boundless mechanism for endowing dynamic behaviour on areas of text. This article discusses the features of the Tk text widget that make it a rich and dynamic text manipulation utility. Examples are presented in Tcl/Tk code, but users of other languages shouldn't have much trouble translating the concepts and examples to their favoured environments. Before we get started, however, we need to take a quick look at a couple of concepts employed by the widget.
Each character stored in a text widget can be addressed by an index. An index most often is defined by two numbers, the line number and the character position on that line. As an example, the index 10.45 refers to the character on line 10, position 45.
The text widget has built-in functionality for index arithmetic; it supports expressions that allow you to find a location, say, 10 lines and 15 characters from a given point. It also handles special indices containing a pixel location (specified as @x,y) or containing certain words. For example, 1.end means the character at the end of the first line, insert means the character at the input cursor and current means the character under the mouse pointer.
The feature that provides the Tk text widget with much of its power is known as tagging. Any area of the text, defined as starting at one index and ending at another, can be tagged with a logical tag name. Each tag can be assigned certain attributes and properties, and all the areas of text given that tag immediately take on the assigned properties. A good part of this article discusses the properties that can be controlled and the uses to which this tagging feature can be put.
The Tk text widget provides all the facilities required for rich text editing. This includes flexible font handling, adjustable line spacing and margins, word wrap, colour support, cut, copy and paste, an undo stack, plus the usual array of bold, underline, italic and other formatting options. Figure 1 is a screenshot of one of the demonstration scripts supplied with Tk. It shows some of the formatting options available. Figure 1 here
All formatting is implemented using the tags mechanism. The script writer defines a tag with the formatting required, then either inserts the required text with that tag or applies that tag to an area of text already in the widget. For example, for an individual text widget named .t, a title tag might be defined with a large, underlined font and centre justification:
.t tag configure title -font "helvetica 14" \ -justify "center" \ -underline on
To insert some title text into the widget, this code can be used:
.t insert 1.0 "The Legend of Black Cave\n" title
This inserts the text at line 1, position 0, with the tag title applied. Alternatively, the tag can be applied to text already in the widget by using code like:
.t tag add title 1.0 1.end
This adds the title tag to the text between the given indices, in this case between the start and end of line 1. In a more realistic situation those indices wouldn't be hard coded. They are more likely to be calculated from the location of the selection, for example, or the results of a search. Notice that the command used to assign a tag to an area of text is add. This is because an area of text can have several tags assigned to it. For example, our title text also might need to be highlighted as the result of a search or to indicate that its status has changed to urgent.
Figure 2 is a screenshot of a small script (Listing 1) that demonstrates formatting by way of tag manipulation. It also demonstrates the text widget's useful ability to save marks, or named locations, anywhere in the text. Figure 2 here
In addition to text formatting, the Tk text widget also supports embedding images and other GUI control widgets. When images are inserted into the text, they float, so editing of the text around them causes them to move in accordance with the formatting rules configured for that area of text. The same image can be inserted into the widget multiple times, and if an image is modified dynamically elsewhere in the script, its representation in the text widget is updated immediately.
Other Tk GUI widgets can be inserted into the text widget. Simple buttons and dropdown lists, as might be found amongst the text of a web page, are only the beginning. It is possible to embed anything--a drawing canvas, a table or even another text widget. If you have a complicated set of widgets that need to be presented inside some text, the text widget has a mechanism whereby it creates only those widgets when necessary--when the appropriate area is scrolled into view, for example.
Practical Task Scheduling Deployment
July 20, 2016 12:00 pm CDT
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.Register Now!
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- Tech Tip: Really Simple HTTP Server with Python
- SUSE LLC's SUSE Manager
- My +1 Sword of Productivity
- Non-Linux FOSS: Caffeine!
- Managing Linux Using Puppet
- Doing for User Space What We Did for Kernel Space
- Rogue Wave Software's Zend Server
- Parsing an RSS News Feed with a Bash Script
- Google's SwiftShader Released
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide