-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_EventBindings_EventHandling_TkConcepts.rb
113 lines (94 loc) · 2.09 KB
/
04_EventBindings_EventHandling_TkConcepts.rb
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
# coding: utf-8
# Copyright (C) 2019 Mark D. Blackwell.
require 'tk'
require 'tkextlib/tile'
module ::EventBindings
module GraphicalHelper
def f_content
$f_content_private ||= begin
f = ::Tk::Tile::Frame.new root
f.grid sticky: :wnes
end
end
def root
$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 ::EventBindings
module GraphicalObjects
def l_label
@l_label_private ||= begin
l = ::Tk::Tile::Label.new f_content
l.grid
end
end
end
end
module ::EventBindings
module Graphical
extend GraphicalHelper
extend GraphicalObjects
extend self
def main
f_content.padding '3 3 3 3'
weights_column_and_row_set_up
l_label[:text] = 'Starting...'
label_bind
::Tk.mainloop
nil
end
private
def label_bind
label_bind_drag
label_bind_text_fixed
nil
end
def label_bind_drag
l_label.bind 'B3-Motion', lambda_drag, '%x %y'
nil
end
def label_bind_text_fixed
key_symbol = %w[ 1 Double-1 Enter Leave ]
text = %w[
Clicked\ left\ mouse\ button
Double\ clicked
Moved\ mouse\ inside
Moved\ mouse\ outside
]
key_symbol.zip(text).each do |k,t|
l_label.bind k do
l_label[:text] = t
end
end
nil
end
def lambda_drag
@lambda_drag_private ||= ::Kernel.lambda do |x,y|
l_label[:text] = "Right button drag to #{x}, #{y}"
nil
end
end
def weights_column_and_row_set_up
weights_column_and_row_default_set_up f_content, root
nil
end
end
end
::EventBindings::Graphical.main