不确定如何从tk条目或ruby中的文本小部件中获取文本

问题描述 投票:0回答:1

我想完成的是从TkEntry小部件中检索输入字符串,并使用该字符串将TkLabel textvariable更改为:

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
ruby text tk
1个回答
0
投票
对我来说,以下代码(从您的示例中进行了调整)使用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

© www.soinside.com 2019 - 2024. All rights reserved.