Class: Gui

Inherits:
Object
  • Object
show all
Defined in:
lib/gui.rb

Overview

_

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.button(tx2, m, x, y) ⇒ Object

method to draw a button with text (tx2), triggering method (m) and coordinates x,y



35
36
37
38
39
40
# File 'lib/gui.rb', line 35

def self.button(tx2, m,  x, y)
   TkButton.new(root){
   text tx2
   command(m)
   grid('row' => x, 'column' => y, 'padx' => 10, 'pady' => 15)}
end

.entry(var, x, y) ⇒ Object

method to draw an entry field sending value to variable (var)[ use $var1 to $var3]



48
49
50
51
52
# File 'lib/gui.rb', line 48

def self.entry(var, x, y)
  TkEntry.new(root){
    textvariable var
   grid('row' => x, 'column' => y, 'padx' => 10, 'pady' => 15)}
end

.help_winObject

Start by building a Toplevel Help window which is used by a menu for a Help guide



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/gui.rb', line 6

def self.help_win
$win = TkToplevel.new{title "Help Window"}
$scrollbar = TkScrollbar.new($win){command proc{|*args| $textwin.yview(*args)}
}.pack('side' => 'right', 'fill' => 'y')
$textwin =TkText.new($win){
  borderwidth 5
  wrap 'word'
  font TkFont.new('times 16 bold')
  background  "yellow"
  pack('side' => 'right')
}
$textwin.insert(1.0, "#{$guide}")
$textwin.yscrollcommand(proc{|first, last| $scrollbar.set(first, last)})
end

.insert_text(content) ⇒ Object

method to insert result of the method into the textfield widget



68
69
70
71
# File 'lib/gui.rb', line 68

def self.insert_text(content)
$textfield.delete(1.0, 'end')
$textfield.insert('end', content)
end

.label(tx1, x, y) ⇒ Object

method to draw a label with text (tx1) and coordinates x,y in a grid



28
29
30
31
32
# File 'lib/gui.rb', line 28

def self.label(tx1, x, y)
  TkLabel.new(root){
    text tx1
    grid('row' => x, 'column' => y, 'padx' => 10, 'pady' => 15)}
end

menu to display and the root window menubar



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/gui.rb', line 84

def self.menu
TkOption.add '*tearOff', 0
menu = TkMenu.new(root)
menu.add('command',
              'label'   => "Help",
              'command' => proc{help_win},
              'underline' => 3)
menu_bar = TkMenu.new
menu_bar.add('cascade',
             'menu' => menu,
             'label' => "Help")
root.menu(menu_bar)
end

.rootObject

method to build the root main window



22
23
24
25
# File 'lib/gui.rb', line 22

def self.root
  TkRoot.new{
    title "Title"}
end

.showObject

The final and essential step, to run the mainloop for GUI to display



99
100
101
# File 'lib/gui.rb', line 99

def self.show
Tk.mainloop
end

.textfield(w, h, bg, fg, x, y, z) ⇒ Object

and foreground color (fg), (content) and coordinates x, y, with vertical scrollbar



56
57
58
59
60
61
62
63
64
65
# File 'lib/gui.rb', line 56

def self.textfield(w, h, bg, fg, x, y, z)
  $textfield = TkText.new(root){
    width w
    height h
    background bg
    foreground  fg
    font TkFont.new('times 16 bold')
    grid('row' => x, 'column' => y,'columnspan' => z, 'padx' => 10, 'pady' => 15)
  }
end

Instance Method Details

#scrollObject

__ The Scrollbar to use with textfield widget



75
76
77
78
79
80
81
# File 'lib/gui.rb', line 75

def scroll
$scroll = TkScrollbar.new{command proc{|*args|
    $textfield.yview(*args)
  $textfield.yscrollcommand(proc {|first, last| $scroll.set(first, last)})
    pack('side' => 'right', 'fill' => 'y')}
  }
end