如何使用 Ruby/Tk 验证 TkEntry 的内容?
How do I validate a TkEntry's contents using Ruby/Tk?
我想要一个只接受数值的 TkEntry,但我不知道该怎么做。
:validatecommand
仅在初始化 TkEntry 时调用一次,我不知道如何正确传递参数!
这是一个说明我的问题的示例文件:
require 'tk'
# validation method
def isNumerical?
return false # i plan to actually do something here once i can get a variable in
end
def initWindow
@root = TkRoot.new {
title "Bluebird"
minsize 400, 300
setgrid
}
entry = TkEntry.new(
@root,
:width=>5,
:textvariable=>TkVariable.new("2241"),
:validate=>"key",
:validatecommand=>proc{isNumerical?}
).grid(:column=> 0, :row=> 0)
end
initWindow
Tk.mainloop
The :validatecommand only gets called on once, when the TkEntry is initialized[.]
不是这样!
I can't figure out how to properly pass an argument in[.]
我对你的程序进行了微调,以便将参数传递给它的验证方法,并打印输入字符串:
require 'tk'
# validation method
def isNumerical?(s)
puts s
# return false # i plan to actually do something here once i can get a variable in
return true
end
def initWindow
@root = TkRoot.new {
title "Bluebird"
minsize 400, 300
setgrid
}
entry = TkEntry.new(
@root,
:width=>5,
:textvariable=>TkVariable.new("2241"),
:validate=>"key",
:validatecommand=>[proc{|s| isNumerical?(s)}, "%P"]
).grid(:column=> 0, :row=> 0)
end
initWindow
Tk.mainloop
顺便说一句,我想你会发现这些有用:
我想要一个只接受数值的 TkEntry,但我不知道该怎么做。
:validatecommand
仅在初始化 TkEntry 时调用一次,我不知道如何正确传递参数!
这是一个说明我的问题的示例文件:
require 'tk'
# validation method
def isNumerical?
return false # i plan to actually do something here once i can get a variable in
end
def initWindow
@root = TkRoot.new {
title "Bluebird"
minsize 400, 300
setgrid
}
entry = TkEntry.new(
@root,
:width=>5,
:textvariable=>TkVariable.new("2241"),
:validate=>"key",
:validatecommand=>proc{isNumerical?}
).grid(:column=> 0, :row=> 0)
end
initWindow
Tk.mainloop
The :validatecommand only gets called on once, when the TkEntry is initialized[.]
不是这样!
I can't figure out how to properly pass an argument in[.]
我对你的程序进行了微调,以便将参数传递给它的验证方法,并打印输入字符串:
require 'tk'
# validation method
def isNumerical?(s)
puts s
# return false # i plan to actually do something here once i can get a variable in
return true
end
def initWindow
@root = TkRoot.new {
title "Bluebird"
minsize 400, 300
setgrid
}
entry = TkEntry.new(
@root,
:width=>5,
:textvariable=>TkVariable.new("2241"),
:validate=>"key",
:validatecommand=>[proc{|s| isNumerical?(s)}, "%P"]
).grid(:column=> 0, :row=> 0)
end
initWindow
Tk.mainloop
顺便说一句,我想你会发现这些有用: