Class: Teek::Widget

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

Overview

Thin wrapper around a Tk widget path. Holds a reference to the App and the widget's Tcl path string.

Instances are interchangeable with plain strings anywhere a widget path is expected thanks to #to_s returning the path.

Created via App#create_widget:

Examples:

btn = app.create_widget('ttk::button', text: 'Click')
btn.command(:configure, text: 'Updated')
app.command(:pack, btn, pady: 10)  # to_s makes this work
btn.destroy

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, path) ⇒ Widget

Returns a new instance of Widget.



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

def initialize(app, path)
  @app = app
  @path = path
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



20
21
22
# File 'lib/teek/widget.rb', line 20

def app
  @app
end

#pathObject (readonly)

Returns the value of attribute path.



20
21
22
# File 'lib/teek/widget.rb', line 20

def path
  @path
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



118
119
120
# File 'lib/teek/widget.rb', line 118

def ==(other)
  other.is_a?(Widget) ? @path == other.path : @path == other.to_s
end

#bind(event, *subs) { ... } ⇒ void

This method returns an undefined value.

Bind an event on this widget.

Parameters:

  • event (String)

    Tk event name

  • subs (Array<Symbol, String>)

    substitution codes

Yields:

  • called when the event fires

See Also:



93
94
95
# File 'lib/teek/widget.rb', line 93

def bind(event, *subs, &block)
  @app.bind(@path, event, *subs, &block)
end

#command(*args, **kwargs) ⇒ String

Invoke a widget subcommand. Prepends the widget path as the Tcl command.

Examples:

btn.command(:configure, text: 'New')  # => .ttkbutton1 configure -text {New}
btn.command(:invoke)                  # => .ttkbutton1 invoke

Parameters:

  • args

    positional arguments

  • kwargs

    keyword arguments mapped to -key value pairs; any Proc value (e.g. command:) is tracked and released if reconfigured or when this widget is destroyed

Returns:

  • (String)

    the Tcl result



43
44
45
# File 'lib/teek/widget.rb', line 43

def command(*args, **kwargs)
  @app.command(@path, *args, **kwargs)
end

#destroyvoid

This method returns an undefined value.

Destroy this widget and all its children.



49
50
51
# File 'lib/teek/widget.rb', line 49

def destroy
  @app.destroy(@path)
end

#exist?Boolean

Check if this widget still exists in the Tk interpreter.

Returns:

  • (Boolean)


55
56
57
# File 'lib/teek/widget.rb', line 55

def exist?
  @app.winfo.exists?(@path)
end

#grid(**kwargs) ⇒ self

Grid this widget.

Parameters:

  • kwargs

    options passed to the Tk grid command

Returns:

  • (self)


82
83
84
85
# File 'lib/teek/widget.rb', line 82

def grid(**kwargs)
  @app.command(:grid, @path, **kwargs)
  self
end

#hashObject



123
124
125
# File 'lib/teek/widget.rb', line 123

def hash
  @path.hash
end

#heightInteger

Returns current height in pixels.

Returns:

  • (Integer)

    current height in pixels

See Also:



67
68
69
# File 'lib/teek/widget.rb', line 67

def height
  @app.winfo.height(@path)
end

#inspectObject



114
115
116
# File 'lib/teek/widget.rb', line 114

def inspect
  "#<Teek::Widget #{@path}>"
end

#on_close { ... } ⇒ void

This method returns an undefined value.

Register a handler for this window's close button (WM_DELETE_WINDOW). Meant for toplevels; see App#on_close for the full behavior.

Yields:

  • called when the window's close button is pressed

See Also:



110
111
112
# File 'lib/teek/widget.rb', line 110

def on_close(&block)
  @app.on_close(window: @path, &block)
end

#pack(**kwargs) ⇒ self

Pack this widget.

Parameters:

  • kwargs

    options passed to the Tk pack command

Returns:

  • (self)


74
75
76
77
# File 'lib/teek/widget.rb', line 74

def pack(**kwargs)
  @app.command(:pack, @path, **kwargs)
  self
end

#to_sString

Returns the Tcl widget path.

Returns:

  • (String)

    the Tcl widget path



28
29
30
# File 'lib/teek/widget.rb', line 28

def to_s
  @path
end

#unbind(event) ⇒ void

This method returns an undefined value.

Remove an event binding from this widget.

Parameters:

  • event (String)

    Tk event name

See Also:



101
102
103
# File 'lib/teek/widget.rb', line 101

def unbind(event)
  @app.unbind(@path, event)
end

#widthInteger

Returns current width in pixels.

Returns:

  • (Integer)

    current width in pixels

See Also:



61
62
63
# File 'lib/teek/widget.rb', line 61

def width
  @app.winfo.width(@path)
end