Ruby 简明教程
Ruby - Tk Guide
Introduction
Ruby 的标准图形用户界面 (GUI) 为 Tk。Tk 起初是 John Ousterhout 开发的 Tcl 脚本语言的 GUI。
The standard graphical user interface (GUI) for Ruby is Tk. Tk started out as the GUI for the Tcl scripting language developed by John Ousterhout.
Tk 拥有独特的识别标记,它是唯一跨平台的 GUI。Tk 可以运行在 Windows、Mac 和 Linux 上,并在每个操作系统上都提供原生观感。
Tk has the unique distinction of being the only cross-platform GUI. Tk runs on Windows, Mac, and Linux and provides a native look-and-feel on each operating system.
基于 Tk 的应用程序的基本组件被称为窗口小部件。该组件有时也称为窗口,因为在 Tk 中,“窗口”和“窗口小部件”经常可以互换使用。
The basic component of a Tk-based application is called a widget. A component is also sometimes called a window, since, in Tk, "window" and "widget" are often used interchangeably.
Tk 应用程序遵循一个窗口小部件层次结构,其中可以在另一个窗口小部件内放置任意数量的窗口小部件,并且这些窗口小部件又可以在另一个窗口小部件内放置,依此类推。Tk 程序中的主窗口小部件被称为根窗口小部件,可以通过制作 TkRoot 类的一个新实例来创建它。
Tk applications follow a widget hierarchy where any number of widgets may be placed within another widget, and those widgets within another widget, ad infinitum. The main widget in a Tk program is referred to as the root widget and can be created by making a new instance of the TkRoot class.
-
Most Tk-based applications follow the same cycle: create the widgets, place them in the interface, and finally, bind the events associated with each widget to a method.
-
There are three geometry managers; place, grid and pack that are responsible for controlling the size and location of each of the widgets in the interface.
Installation
Ruby Tk 绑定与 Ruby 一起分发,但 Tk 是一个单独的安装程序。Windows 用户可以从 ActiveState’s ActiveTcl 下载一个单次点击的 Tk 安装程序。
The Ruby Tk bindings are distributed with Ruby but Tk is a separate installation. Windows users can download a single click Tk installation from ActiveState’s ActiveTcl.
Mac 和 Linux 用户可能不需要安装它,因为存在很大几率 Tk 已经与操作系统一同安装了,但如果没有安装,你可以从 Tcl Developer Xchange 下载预构建软件包或获取源代码。
Mac and Linux users may not need to install it because there is a great chance that its already installed along with OS but if not, you can download prebuilt packages or get the source from the Tcl Developer Xchange.
Simple Tk Application
Ruby/Tk 程序有一个典型的结构,即创建主窗口或 root 窗口(TkRoot 的一个实例),向其中添加窗口小部件以构建用户界面,然后通过调用 Tk.mainloop 启动主事件循环。
A typical structure for Ruby/Tk programs is to create the main or root window (an instance of TkRoot), add widgets to it to build up the user interface, and then start the main event loop by calling * Tk.mainloop*.
面向 Ruby/Tk 的传统 Hello, World! 示例看起来像这样 −
The traditional Hello, World! example for Ruby/Tk looks something like this −
require 'tk'
root = TkRoot.new { title "Hello, World!" }
TkLabel.new(root) do
text 'Hello, World!'
pack { padx 15 ; pady 15; side 'left' }
end
Tk.mainloop
在这里,在加载 tk 扩展模块之后,我们使用 TkRoot.new 创建了一个根级别框架。然后我们制作了一个 TkLabel 窗口小部件作为根框架的一个子级,同时为该标签设置几个选项。最后,我们打包根框架并进入主 GUI 事件循环。
Here, after loading the tk extension module, we create a root-level frame using TkRoot.new. We then make a TkLabel widget as a child of the root frame, setting several options for the label. Finally, we pack the root frame and enter the main GUI event loop.
如果你运行这个脚本,将会产生以下结果 −
If you would run this script, it would produce the following result −
Ruby/Tk Widget Classes
有一系列 Ruby/Tk 类,它们可用于使用 Ruby/Tk 创建所需 GUI。
There is a list of various Ruby/Tk classes, which can be used to create a desired GUI using Ruby/Tk.
-
TkFrame Creates and manipulates frame widgets.
-
TkButton Creates and manipulates button widgets.
-
TkLabel Creates and manipulates label widgets.
-
TkEntry Creates and manipulates entry widgets.
-
TkCheckButton Creates and manipulates checkbutton widgets.
-
TkRadioButton Creates and manipulates radiobutton widgets.
-
TkListbox Creates and manipulates listbox widgets.
-
TkComboBox Creates and manipulates listbox widgets.
-
TkMenu Creates and manipulates menu widgets.
-
TkMenubutton Creates and manipulates menubutton widgets.
-
Tk.messageBox Creates and manipulates a message dialog.
-
TkScrollbar Creates and manipulates scrollbar widgets.
-
TkCanvas Creates and manipulates canvas widgets.
-
TkScale Creates and manipulates scale widgets.
-
TkText Creates and manipulates text widgets.
-
TkToplevel Creates and manipulates toplevel widgets.
-
TkSpinbox Creates and manipulates Spinbox widgets.
-
TkProgressBar Creates and manipulates Progress Bar widgets.
-
Dialog Box Creates and manipulates Dialog Box widgets.
-
Tk::Tile::Notebook Display several windows in limited space with notebook metaphor.
-
Tk::Tile::Paned Displays a number of subwindows, stacked either vertically or horizontally.
-
Tk::Tile::Separator Displays a horizontal or vertical separator bar.
-
Ruby/Tk Font, Colors and Images Understanding Ruby/Tk Fonts, Colors and Images
Standard Configuration Options
所有控件都具有若干不同的配置选项,这些选项通常用于控制它们的显示方式或行为方式。有哪些可用选项当然取决于控件类。
All widgets have a number of different configuration options, which generally control how they are displayed or how they behave. The options that are available depend upon the widget class of course.
以下列出了所有标准配置选项,这些选项可适用于任何 Ruby/Tk 控件。
Here is a list of all the standard configuration options, which could be applicable to any Ruby/Tk widget.
Ruby/Tk Geometry Management
几何管理处理根据要求对不同控件进行定位。Tk 中的几何管理依赖于主控件和从控件的概念。
Geometry Management deals with positioning different widgets as per requirement. Geometry management in Tk relies on the concept of master and slave widgets.
主控件是一个控件,通常是顶级窗口或框架,它将包含其它控件,即从控件。你可以认为几何管理器控制了主控件,并决定在其中显示什么。
A master is a widget, typically a top-level window or a frame, which will contain other widgets, which are called slaves. You can think of a geometry manager as taking control of the master widget, and deciding what will be displayed within.
几何管理器将询问每个从控件的自然大小,或理想的显示大小。然后,它获取该信息,并将其与程序在要求几何管理器管理该特定从控件时提供的任何参数结合在一起。
The geometry manager will ask each slave widget for its natural size, or how large it would ideally like to be displayed. It then takes that information and combines it with any parameters provided by the program when it asks the geometry manager to manage that particular slave widget.
有三个几何管理器,即 place、grid 和 pack,它们负责控制接口中每个控件的大小和位置。
There are three geometry managers place, grid and pack that are responsible for controlling the size and location of each of the widgets in the interface.
Ruby/Tk Event Handling
Ruby/Tk 支持事件循环,它会从操作系统接收事件。这些事件包括按钮按下、击键、鼠标移动、窗口大小调整等。
Ruby/Tk supports event loop, which receives events from the operating system. These are things like button presses, keystrokes, mouse movement, window resizing, and so on.
Ruby/Tk 可用于管理此事件循环。它会弄清楚该事件适用于哪个控件(用户是否单击了此按钮?如果按下了键,哪个文本框具有焦点?),并相应地分派事件。单个控件知道如何响应事件,因此例如,当鼠标移动到按钮上时,按钮可能会变色,而在鼠标离开时恢复原色。
Ruby/Tk takes care of managing this event loop for you. It will figure out what widget the event applies to (did the user click on this button? if a key was pressed, which textbox had the focus?), and dispatch it accordingly. Individual widgets know how to respond to events, so for example a button might change color when the mouse moves over it, and revert back when the mouse leaves.
在更高的层面上,Ruby/Tk 会在你的程序中调用回调,以表明控件发生了重大事件。对于这两种情况,你可以提供一个代码块或一个 Ruby Proc 对象,以指定应用程序如何响应该事件或回调。
At a higher level, Ruby/Tk invokes callbacks in your program to indicate that something significant happened to a widget For either case, you can provide a code block or a Ruby Proc object that specifies how the application responds to the event or callback.
让我们了解如何使用 bind 方法将基本窗口系统事件与处理它们的 Ruby 过程关联起来。bind 的最简单形式使其输入一个指示事件名称的字符串和一个 Tk 用于处理该事件的代码块。
Let’s take a look at how to use the bind method to associate basic window system events with the Ruby procedures that handle them. The simplest form of bind takes as its inputs a string indicating the event name and a code block that Tk uses to handle the event.
例如,要捕获控件上的第一个鼠标按钮的 ButtonRelease 事件,你可以编写 −
For example, to catch the ButtonRelease event for the first mouse button on some widget, you’d write −
someWidget.bind('ButtonRelease-1') {
....code block to handle this event...
}
事件名称可以包括其他修饰符和详细信息。修饰符是一个字符串,如 Shift、Control 或 Alt,指示按下了其中一个修饰键。
An event name can include additional modifiers and details. A modifier is a string like Shift, Control or Alt, indicating that one of the modifier keys was pressed.
因此,例如,要捕获在用户按住 Ctrl 键并单击鼠标右键时生成的事件。
So, for example, to catch the event that’s generated when the user holds down the Ctrl key and clicks the right mouse button.
someWidget.bind('Control-ButtonPress-3', proc { puts "Ouch!" })
当用户激活它们时,许多 Ruby/Tk 控件都会触发回调,并且你可以使用 command 回调指定在这种情况下调用某个代码块或过程。如前所述,你可以在创建控件时指定 command 回调过程 −
Many Ruby/Tk widgets can trigger callbacks when the user activates them, and you can use the command callback to specify that a certain code block or procedure is invoked when that happens. As seen earlier, you can specify the command callback procedure when you create the widget −
helpButton = TkButton.new(buttonFrame) {
text "Help"
command proc { showHelp }
}
或者,你可以在稍后使用控件的 command 方法分配它 −
Or you can assign it later, using the widget’s command method −
helpButton.command proc { showHelp }
由于 command 方法接受过程或代码块,因此你还可以编写上一段代码示例,如下所示 −
Since the command method accepts either procedures or code blocks, you could also write the previous code example as −
helpButton = TkButton.new(buttonFrame) {
text "Help"
command { showHelp }
}
The configure Method
配置方法可用于设置和检索任何小组件配置值。例如,如需更改按钮的宽度,你可以随时按如下方式调用配置方法:
The configure method can be used to set and retrieve any widget configuration values. For example, to change the width of a button you can call configure method any time as follows −
require "tk"
button = TkButton.new {
text 'Hello World!'
pack
}
button.configure('activebackground', 'blue')
Tk.mainloop
如需获取当前小组件的值,只需按如下方式提供该值,而无需提供实际值:
To get the value for a current widget, just supply it without a value as follows −
color = button.configure('activebackground')
你还可以不提供任何选项来调用配置,这样便会提供所有选项及其值的列表。
You can also call configure without any options at all, which will give you a listing of all options and their values.