2
我正在嘗試製作一個小程序,將一些數據變爲可用的形式。我想要做的一件事是能夠選擇一些文件並對它們執行操作,所以我認爲我會使用Tk中的列表框對象來做到這一點。我希望能夠打開一個文件並查看它的文件名顯示在列表框中。據我所知,這正是在列表框中使用listvariable的目的。然而,當我運行我的代碼時,列表框永遠不會更新(儘管已經在列表變量變量中的項目顯示正常)。瞭解紅寶石中的Tk列表框
所以這裏是接近MWE的。我做錯了什麼,我誤解了什麼基本概念?
require 'tk'
require 'tkextlib/tile'
$path_list = []
$populate_list = TkVariable.new($path_list)
def get_file
file = Tk.getOpenFile
file = open(file) unless file.empty?
path = File.basename(file, ".out")
if $path_list.include?(path)
Tk.messageBox(
'type' => "ok",
'icon' => "warning",
'title' => " - Minimum Working Example - ",
'message' => "This file has already been added! Nothing was added to the list"
)
else
$path_list.push(path)
end
end
root = TkRoot.new {title "- Minimum Working Example -"}
frame = Tk::Tile::Frame.new(root) {padding "3 3 12 12"}.grid(:sticky => 'nsew') # 'north south east west'
TkGrid.columnconfigure root, 0, :weight => 1; TkGrid.rowconfigure root, 0, :weight => 1
$file_listbox = Tk::Listbox.new(frame) {
listvariable $populate_list}.grid(:column => 1, :row => 0, :rowspan => 6)
Tk::Tile::Button.new(frame) {
width 15; text 'Open file...'; command {get_file}}.grid(:column => 0, :row => 1)
Tk.mainloop
我可能需要以其他順序寫它嗎?
不,它不應該。當您創建TkVariable時,您傳遞的值只是初始化值,並不指向其他變量。所以他傳遞一個空數組並初始化。 –
這真是太棒了。看到它不起作用是令人沮喪的,但這是有道理的。我對這一切都很陌生,所以我想我應該習慣於調試怪異的東西..謝謝 –