1

我想使用 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
4

1 回答 1

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
于 2014-01-25T00:17:24.853 回答