Project

General

Profile

Bug #8957 ยป test_control_variables.rb

rob (rob gow), 09/27/2013 03:50 AM

 
#!/bin/env ruby

# print button shows the value of control variable $phone
# the two set buttons will set the control variable to their adjacent radio
# At first clicking the radio button and then print shows the control variable
# is being updated.

# Clicking a set button changes the variable to it its adjacent radio, but this
# does not change the radio button selected.
# Also once the variable has been set in code, ruby ceases to update the variable,
# clicking radio buttons and then print results in no change. From here on it
# can only be changed in code.
#
require 'tk'
require 'tkextlib/tile'
root = TkRoot.new {title "contro variable test"}
content = Tk::Tile::Frame.new(root) {padding "4 4 4 4"}.grid( :sticky => 'ew')
TkGrid.columnconfigure root, 1, :weight => 1
TkGrid.columnconfigure root, 2, :weight => 1
TkGrid.rowconfigure root, 1, :weight => 1
TkGrid.rowconfigure root, 2, :weight => 1
TkGrid.rowconfigure root, 3, :weight => 1

$phone = TkVariable.new
home = Tk::Tile::RadioButton.new(root) {
text 'Home'; variable $phone; value 'home'}
.grid( :column => 1, :row => 1, :sticky => 'w')
office = Tk::Tile::RadioButton.new(root) {
text 'Office'; variable $phone; value 'office'}
.grid( :column => 1, :row => 2, :sticky => 'w')
mobile = Tk::Tile::RadioButton.new(root) {
text 'Mobile'; variable $phone; value 'mobile'}
.grid( :column => 1, :row => 3, :sticky => 'w')

abutton = Tk::Tile::Button.new(root) {text 'print'; command 'a_button'}
.grid( :column => 2, :row => 1, :sticky => 'ew')
bbutton = Tk::Tile::Button.new(root) {text '<- set'; command 'b_button'}
.grid( :column => 2, :row => 2, :sticky => 'ew')
cbutton = Tk::Tile::Button.new(root) {text '<- set' ; command 'c_button'}
.grid( :column => 2, :row => 3, :sticky => 'ew')

TkWinfo.children(root).each {|w| TkGrid.configure w, :padx => 5, :pady => 5}
home.focus
root.bind("Return") {home}

def a_button
puts ("control variable = #{$phone}")
end

def b_button
puts("setting contol variable to office")
$phone = 'office'
end

def c_button
puts("setting contol variable to mobile")
$phone = 'mobile'
end

Tk.mainloop
    (1-1/1)