Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Ruby Programming
Previous Page Home Next Page

Setting/Getting Options Dynamically

In addition to setting a widget's options when it's created, you can reconfigure a widget while it's running. Every widget supports the configure method, which takes a Hash or a code block in the same manner as new. We can modify the first example to change the label text in response to a button press:

lbl = { justify 'center'
  text    'Hello, World!';
  pack('padx'=>5, 'pady'=>5, 'side' => 'top') } {
  text "Cancel"
  command proc { lbl.configure('text'=>"Goodbye, Cruel World!") }
  pack('side'=>'right', 'padx'=>10, 'pady'=>10)

Now when the Cancel button is pressed, the text in the label will change immediately from ``Hello, World!'' to ``Goodbye, Cruel World!''

You can also query widgets for particular option values using cget:

require 'tk'
b = {
  text     "OK"
  justify  'left'
  border   5
b.cget('text') "OK"
b.cget('justify') "left"
b.cget('border') 5

Ruby Programming
Previous Page Home Next Page

  Published under the terms of the Open Publication License Design by Interspire