2013-07-06 35 views
1

我想使用Ruby TK GUI開發開發一個待辦事項列表。我正試圖通過我的一系列任務來運行循環。我無法找到示例或方法來顯示未知數量的標籤。非常感謝任何幫助以及建設性的批評。

這不會顯示任何工作方式,但是憑藉我對Ruby的先前知識,這是我可以考慮創建的唯一方法。Ruby TK GUI開發 - 從數組創建標籤

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

它漂亮其實很簡單,你使用「textvariable」,這已經是一個predifined全局變量(如$待辦事項,$日期和$顯示)在$ todo_list.each塊,只是改變「textvariable 'to'text'

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