| Previous | Next
User CustomizationContents:Using the Command Line
When writing Perl/Tk applications, we can change the look of the application dramatically by using different options when creating widgets. We can give our users a similar type of control using the resource database or command-line options. The user has the ability to change the appearance of almost anything, from the width of widget edges and the fonts used to display text to the cursor shown as the mouse passes over a widget. A change can be global to every application that honors the option database or so specific that it changes a single option of a single widget. In the first part of this chapter, we'll cover the ways users can change these visual aspects from the command line. Later in the chapter, we'll give you more ideas on how to utilize a resource database. The term resource database originated in the X Window System. Essentially, the resource database provides user-specified values for widget configuration options. When creating a new widget, Tk queries the resource database for all options that the developer did not explicitly specify on the widget creation command. If an option/value pair (sometimes called a resource) isn't found in the resource database, Tk uses the option's default value provided by the widget's class. Actually, a resource is a bit more complicated. Although the resource identifies a single option/value pair, it may apply to more than a single widget. Using class names and the wildcard character ( So, an option database resource is really a pattern/value pair. Resources have this general syntax:
where Tk doesn't use the classical Unix resource database, but its own implementation, called the option database. Except for minor details, using the option database is identical to the resource database; thus, we'll use the two terms interchangeably. Perl/Tk handles the option database slightly differently than Tcl/Tk. Prior to MainWindow creation, Perl/Tk calls the special command-line processing subroutine When determining the value to assign to an option, Perl/Tk uses this order of precedence:
Using the Command LineYou need to leave a way open for customization in your application. Mainly, this means you can take the easy way out and not hardcode many option values. If you hardcode too many configuration options, users cannot easily tailor your application using one of the methods listed in this chapter. For example, if you want to allow your users to change the font of all the widgets in the application, don't use the -
Often the easiest thing for a user to do is specify a command-line option when running the application. The options can be changed quickly and allow users to have immediate and direct control over their applications. There are several command-line options that customize the way your application looks. When you run your application, the command line looks something like this:[41]
# On MS Windows systems: > The supported options are standard X options that any user familiar with X Windows will recognize. None of these options are required, but they might be the simplest ways to do what you want (for example, changing the background color of everything in the application). When specifying a command-line list of options and values, do not use a comma separator. If any of the options have bad values, you'll get an immediate error, and the application won't run. Customizing using only command-line options won't necessarily produce the most elegant-looking applications, but for something simple, such as changing colors or fonts, it's the quickest way to go. Also, remember that an option can't be changed by the user if you have set it inside the application. Table 16-1 lists the options that Table 16-1. Command-line options recognized by Tk::CmdLine::SetArguments
Colors, Fonts, and TitlesChanging all colors in an application is simplified by using the - Here's an example that changes your application to white on black: % If you are fond of a particular color for the text, use just the % To change the font of all widgets in the application, use the % For more information on fonts, take a look at "Fonts". Normally the title of a running Perl/Tk application is either its filename (excluding any extension), the value associated with the % Initial MainWindow PlacementWhen you run a Perl/Tk application, the MainWindow immediately appears on the screen. If you would rather the window manager not display the MainWindow, specify the % In Windows, you'll see an entry on the Taskbar. For a Unix system, the icon is placed however your window manager normally does these things (perhaps in an icon bay, as an icon on the desktop, or not shown at all). This is a great way to start an application that has a lot of tasks to do (similar to a batch job), but you still want to be able to see the output in a nice format later. The program can even be written to deiconify itself if something urgent happens while it's running. With the % The complete syntax for
where Choosing a DisplayOn Unix systems, you'll have to specify the full display name (take a look at the Option Database LookupsWhen using the option database, a resource pattern often contains class and application names. You can change these values for your application via the command-line options Further, you can pre-populate the option database by using the % Synchronizing Window MessagesNormally your Perl/Tk application runs asynchronously, which means X messages are buffered. To turn off this buffering, use the |