首页 > 解决方案 > 不确定如何从 tk 条目或 ruby​​ 中的文本小部件获取文本

问题描述

我想要完成的是从 TkEntry 小部件中检索条目字符串并使用该字符串将 TkLabel 文本变量更改为:

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

标签: rubytexttk

解决方案


对我来说,以下代码(从您的示例中调整)永久使用 TkEntry 中的值更新 TkLabel:

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

推荐阅读