Ruby TK GUI开发 - 从数组创建标签

时间:2013-07-06 18:12:39

标签: ruby user-interface tk

我想使用Ruby TK GUI开发开发待办事项列表。我正在尝试通过我的任务数组来执行循环。我找不到显示未知数量标签的示例或方法。非常感谢任何帮助,以及建设性的批评。

这并没有显示它的工作方式,但凭借我之前对Ruby的了解,这是我能想到创建它的唯一方法。

require 'tk'
require 'tkextlib/tile'

root = TkRoot.new {title "Josh's ToDo List"}
content = Tk::Tile::Frame.new(root) {padding "3 3 12 12"}.grid(:sticky => 'nsew')
TkGrid.columnconfigure root, 0, :weight => 1; TkGrid.rowconfigure root, 0, :weight => 1

@todo_list = ['Go to the Store','Get Gas']

$date = TkVariable.new; $todo = TkVariable.new; $display = TkVariable.new
f = Tk::Tile::Label.new(content) {text 'To Do:'}.grid( :column => 1, :row => 1, :sticky => 'we'); 
Tk::Tile::Entry.new(content) {width 7; textvariable $todo}.grid( :column => 2, :row => 1, :sticky => 'we' )
@todo_list.each {|task| Tk::Tile::Label.new(content) {textvariable task}.grid( :column => 2, :row => 2, :sticky => 'we');}
Tk.mainloop

1 个答案:

答案 0 :(得分:0)

实际上非常简单,你使用'textvariable',它必须是$ todo_list.each块中的预定义全局变量(如$ todo,$ date和$ display),只需将'textvariable'更改为'text “

 @todo_list.each {|task| Tk::Tile::Label.new(content) {text task}.grid( :column => 2, :row => 2, :sticky => 'we');}
Tk.mainloop