test_control_variables.rb

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

Download (2.17 KB)

 
1
#!/bin/env ruby
2

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

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

    
24
$phone = TkVariable.new
25
home = Tk::Tile::RadioButton.new(root) {
26
    text 'Home'; variable $phone; value 'home'}
27
        .grid( :column => 1, :row => 1, :sticky => 'w')
28
office = Tk::Tile::RadioButton.new(root) {
29
    text 'Office'; variable $phone; value 'office'}
30
        .grid( :column => 1, :row => 2, :sticky => 'w')
31
mobile = Tk::Tile::RadioButton.new(root) {
32
    text 'Mobile'; variable $phone; value 'mobile'}
33
        .grid( :column => 1, :row => 3, :sticky => 'w')
34

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

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

    
46
def a_button
47
    puts ("control variable = #{$phone}")
48
end
49

    
50
def b_button
51
    puts("setting contol variable to office")
52
    $phone = 'office'
53
end
54

    
55
def c_button
56
    puts("setting contol variable to mobile")
57
    $phone = 'mobile' 
58
end
59

    
60
Tk.mainloop