SkaterCoderGamerDon
SkaterCoderGamerDon

Reputation: 21

Uncertain on how to get text from the tk entry or text widgets in ruby

What I am trying to accomplish is to retrieve the entry string from the TkEntry widget and use that string to change the TkLabel textvariable to:

require 'tk'

class Gui

    def procc
        label['textvariable'] = $user_input
    end

    root = TkRoot.new {title "Gui Test"}
    root['geometry'] = '500x500'

    label = TkLabel.new(root) do
        textvariable
        pack("padx" => "15", "pady" => "15")
    end

    label['textvariable'] = "Label"

    button = TkButton.new(root) do
        text "Button"
        pack("padx" => "15", "pady" => "15")
        command (proc {procc})
    end
    check_button = TkCheckButton.new(root) do
        text "Check Mate"
        pack("padx" => "15", "pady" => "15")
    end



    $user_input = TkVariable.new

    e = Tk::Tile::Entry.new(root) {textvariable = $user_input}
    e.pack()

end

Gui.new
Tk.mainloop

Upvotes: 1

Views: 126

Answers (1)

MarkDBlackwell
MarkDBlackwell

Reputation: 2122

For me, the following code (tweaked from your example) perpetually updates the TkLabel with the value in the TkEntry:

require 'tk'

class Gui
    root = TkRoot.new {title "Gui Test"}
    root['geometry'] = '500x500'

    $user_input = TkVariable.new

    label = TkLabel.new(root) do
        textvariable $user_input
        pack("padx" => "15", "pady" => "15")
    end

    $user_input.value = "Label"

    button = TkButton.new(root) do
        text "Button"
        pack("padx" => "15", "pady" => "15")
        command (proc {})
    end
    check_button = TkCheckButton.new(root) do
        text "Check Mate"
        pack("padx" => "15", "pady" => "15")
    end

    e = Tk::Tile::Entry.new(root) {textvariable $user_input}
    e.pack()
end

Gui.new
Tk.mainloop

Upvotes: 1

Related Questions