-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_FeetToMeters_Code_AFirstRealExample.rb
More file actions
187 lines (158 loc) · 4.05 KB
/
03_FeetToMeters_Code_AFirstRealExample.rb
File metadata and controls
187 lines (158 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# coding: utf-8
# Copyright (C) 2019 Mark D. Blackwell.
require 'tk'
require 'tkextlib/tile'
module ::FeetToMeters
module GraphicalHelper
def f_content
# There only can be a single main content frame (for the main window) in the
# program, so use a global:
$f_content_private ||= begin
# In order to make the entire visible background match that of its
# contained widgets, use a frame:
f = ::Tk::Tile::Frame.new root
# Pad the overall frame. This supplements padx and pady of each grid box
# (see elsewhere):
f.padding '3 3 12 12' # Left, Top, Right, Bottom.
f.grid sticky: :wnes # West, North, East, South.
end
end
def root
# There only can be a single (Tk) root in the program, so use a global:
$root_private ||= begin
tell_tk_which_encoding_to_use
::TkRoot.new
end
end
def weights_column_and_row_default_set_up(*args)
first = 0
args.reverse_each do |e|
::TkGrid.columnconfigure e, first, weight: 1
::TkGrid. rowconfigure e, first, weight: 1
end
nil
end
private
def tell_tk_which_encoding_to_use
::Tk::Encoding.encoding = ''.encoding
nil
end
end
end
module ::FeetToMeters
module GraphicalObjects
def e_feet
@e_feet_private ||= begin
e = ::Tk::Tile::Entry.new f_content
e.textvariable v_feet
e.width 7
end
end
def v_feet
@v_feet_private ||= ::TkVariable.new
end
def v_meters
@v_meters_private ||= ::TkVariable.new
end
end
end
module ::FeetToMeters
module Graphical
extend GraphicalHelper
extend GraphicalObjects
extend self
def main
weights_column_and_row_set_up
root.title 'Feet to Meters'
return_bind
column_1_set_up
column_2_set_up
column_3_set_up
pad_grid_boxes # Keep below where we fill the grid.
lambda_calculate_and_focus.call
::Tk.mainloop
nil
end
private
def column_1_set_up
l = ::Tk::Tile::Label.new f_content
l.text 'is equivalent to'
l.grid column: 1, row: 2, sticky: :e
nil
end
def column_2_row_1_set_up
e_feet.grid column: 2, row: 1, sticky: :we
nil
end
def column_2_row_2_set_up
l = ::Tk::Tile::Label.new f_content
l.textvariable v_meters
l.grid column: 2, row: 2, sticky: :we
nil
end
def column_2_set_up
column_2_row_1_set_up
column_2_row_2_set_up
nil
end
def column_3_row_1_set_up
l = ::Tk::Tile::Label.new f_content
l.text 'feet'
l.grid column: 3, row: 1, sticky: :w
nil
end
def column_3_row_2_set_up
l = ::Tk::Tile::Label.new f_content
l.text 'meters'
l.grid column: 3, row: 2, sticky: :w
nil
end
def column_3_row_3_set_up
b = ::Tk::Tile::Button.new f_content
b.command lambda_calculate_and_focus
b.text 'Calculate'
b.grid column: 3, row: 3, sticky: :w
nil
end
def column_3_set_up
column_3_row_1_set_up
column_3_row_2_set_up
column_3_row_3_set_up
nil
end
def lambda_calculate_and_focus
@lambda_calculate_and_focus_private ||= ::Kernel.lambda do
e_feet.focus
decimal_digits = 4
meters_per_foot = 0.3048
begin
v_meters.value = (v_feet * meters_per_foot).round decimal_digits
rescue ::NoMethodError
v_meters.value = ''
end
nil
end
end
def pad_grid_boxes
::TkWinfo.children(f_content).each do |widget|
# Pad each grid box:
::TkGrid.configure widget, padx: 5, pady: 5
end
nil
end
def return_bind
# Backstops every other widget in the tree:
root.bind 'Return' do
lambda_calculate_and_focus.call
end
nil
end
def weights_column_and_row_set_up
weights_column_and_row_default_set_up f_content, root
::TkGrid.columnconfigure f_content, 4, weight: 1
::TkGrid. rowconfigure f_content, 4, weight: 1
nil
end
end
end
::FeetToMeters::Graphical.main