Wiki
Version 2 (modified by callisto, 12 years ago)

completed label and separator widgets

Gtk Static Widgets

Static widgets provide information to the user, either about the state of the program or to help understand the display.

Labels

A 'label' simply displays some text. The most straightforward construction is:

        label1 = Label("label")

Labels can display larger blocks of text. Set 'lineWrap' to true if the text should be wrapped and displayed on multiple lines. Markup is supported to change the style of the text. If your label text contains markup you need to set the 'useMarkup' attribute of the label to true.

The following image and program shows three different labels.

# @args -pkg:gtk-sharp-2.0 # remove initial '#'

use Gtk

class ExampleWindow inherits Window
    cue init
        base.init("Example of Labels")
        listen .deleteEvent, do(obj, e as DeleteEventArgs)
            Application.quit
        .createWidgets
        .setDefaultSize(300, 100)

    def createWidgets
        label1 = Label("label")
        label2 = Label("with _mnemonic")
        label3 = Label("Right now, if you want software contracts in your language, 
how can you get them? The answer is to use <tt>Eiffel</tt> or D. What if you want static and 
dynamic binding? Use Objective-C or Boo. What if you want expressiveness and quick coding? 
Use <b>Python, Ruby or Smalltalk</b>. What if you want runtime performance? Use C#, Java, C++, 
etc. <i>What if you want first class language support for unit tests? Use D.</i>")
        label3.lineWrap = true
        label3.useMarkup = true

        box = HBox(false, 20)
        box.borderWidth = 20
        box.packStart(label1, true, true, 0)
        box.packStart(label2, true, true, 0)
        box.packStart(label3, true, true, 0)
        .add(box)
    
class RunProgram
    def main
        Application.init
        window = ExampleWindow()
        window.showAll
        Application.run

Separators

Separators are widgets which display as lines, and are used to divide up parts of the interface. Separators are either vertical or horizontal.

  • HSeparator() : creates a horizontal line - use to divide up widgets arranged vertically
  • VSeparator() : creates a vertical line - use to divide up widgets arranged horizontally

Frame

A 'frame' is a container class which holds one child widget, and displays that child with a surrounding border and title. That child can be another container, so frames can contain many other widgets.

A frame is constructed with the text of the title to display. The child widget is then added before packing the frame to display:

        frame1 = Frame("Widgets label")
        frame1.add(box)

There are two display elements of the frame which can be changed.

  1. The position of the label can be changed with respect to the top line using two attributes:
  • frame.labelXAlign: 0.0 is left, 1.0 is right.
  • frame.labelYAlign: 0.0 is below the line, 0.5 on the line, and 1.0 above the line.
  1. The style of the line can be changed, using the 'shadowType' attribute, which has the following values:
  • ShadowType.None
  • ShadowType.In: bevelled inwards
  • ShadowType.Out: bevelled outwards
  • ShadowType.EtchedIn: (the default)
  • ShadowType.EtchedOut

The following image and program illustrates the use of frames to label some radiobuttons. The frame shown on the left uses the default alignment and style.

# @args -pkg:gtk-sharp-2.0 # remove initial '#'

use Gtk

class ExampleWindow inherits Window
    cue init
        base.init("Example of Frames")
        listen .deleteEvent, do(obj, e as DeleteEventArgs)
            Application.quit
        .createWidgets
        .setDefaultSize(300, 100)

    def createRadios as VBox
        radio1 = RadioButton("Cobra")
        radio2 = RadioButton(radio1, "Java")
        radio3 = RadioButton(radio2, "Python")
        box = VBox(false, 10)
        box.packStart(radio1, false, false, 0)
        box.packStart(radio2, false, false, 0)
        box.packStart(radio3, false, false, 0)
        return box

    def createWidgets
        frame1 = Frame("Select Language")
        frame1.add(.createRadios)

        frame2 = Frame("Select Language")
        frame2.labelXalign = 0.5 to float32
        frame2.labelYalign = 0 to float32
        frame2.shadowType = ShadowType.In
        frame2.add(.createRadios)

        box = HBox(true, 20)
        box.borderWidth = 20
        box.packStart(frame1, true, true, 0)
        box.packStart(frame2, true, true, 0)
        .add(box)
    
class RunProgram
    def main
        Application.init
        window = ExampleWindow()
        window.showAll
        Application.run

Attachments