2015-01-14 2 views
0

Я создал два виджета управления деревьями и виджет прокрутки. Теперь я пытаюсь создать функциональность, где у пользователя будет выбор использования полосы прокрутки в том смысле, что будут параметры радиообмена, которые решат, какой виджет будет прокручиваться с помощью полосы прокрутки. У меня есть идея, как прокрутить два виджета вместе с here. Но не знаете, как создать переключаемую полосу прокрутки. Мой код ниже:Прокрутка двух виджетов с использованием одной полосы прокрутки в TCL/TK.

package require Tk 
package require treectrl 

namespace eval ::at::GUI { 
    variable Priv; 
    variable OptionsRB; 

    set Priv(treePrimary) ""; 
    set Priv(treeSecondary) ""; 
    set Priv(treeScrollbar) ""; 

    if { ![info exists OptionsRB] } { 
    set OptionsRB(scrollTree) "LeftTree"; 
    } 
} 

proc ::at::GUI::DrawGUI {} { 
    variable Priv; 

    set frm_treeFrame [ttk::labelframe .treeFrame -text "Tree Area"];  
    set Priv(treePrimary) [treectrl $frm_treeFrame.treePrimary] 
    set Priv(treeSecondary) [treectrl $frm_treeFrame.treeSecondary]  
    set Priv(treeScrollbar) [ttk::scrollbar $frm_treeFrame.sb_treeScroll -command "::at::GUI::yview"] 

    grid $Priv(treePrimary) $Priv(treeSecondary) $Priv(treeScrollbar) -sticky news; 
    grid columnconfigure $frm_treeFrame 0 -weight 1; 
    grid columnconfigure $frm_treeFrame 1 -weight 1; 
    grid rowconfigure $frm_treeFrame 0 -weight 1; 

    set frm_ST [ttk::labelframe .scrollTreeOptions -text "Scroll Option"]; 
    set rb_leftTree [ttk::radiobutton $frm_ST.rb1 -text "Left Tree" -variable [namespace current]::OptionsRB(scrollTree) -value LeftTree -command [namespace current]::configScroll ] 
    set rb_rightTree [ttk::radiobutton $frm_ST.rb2 -text "Right Tree" -variable [namespace current]::OptionsRB(scrollTree) -value RightTree -command [namespace current]::configScroll ] 
    set rb_bothTree [ttk::radiobutton $frm_ST.rb3 -text "Both Tree" -variable [namespace current]::OptionsRB(scrollTree) -value BothTree -command [namespace current]::configScroll ] 

    grid $rb_leftTree -row 1 -padx {30 5} -pady 5 -sticky w 
    grid $rb_rightTree -row 2 -padx {30 5} -pady 5 -sticky w 
    grid $rb_bothTree -row 3 -padx {30 5} -pady 5 -sticky w 

    # Grid all the frames in main window. 
    grid $frm_treeFrame -sticky news; 
    grid $frm_ST -sticky news; 

    ############################## 
    ::at::GUI::CreateLemes $Priv(treePrimary) 
    ::at::GUI::CreateLemes $Priv(treeSecondary) 

    foreach tree [list $Priv(treePrimary) $Priv(treeSecondary)] { 
    for {set i 0} {$i < 20} {incr i} { 
     set parent [expr {int(rand()*$i)}] 
     $tree item create -tag item$i -button auto 
     $tree item lastchild $parent item$i 
     $tree item text item$i name item$i 
    } 
    } 
    return; 
} 

proc ::at::GUI::CreateLemes {T} { 
    $T element create rect rect -fill [list blue selected] 
    $T element create name text 

    set S [$T style create nameStyle] 
    $T style elements $S {rect name}; 
    $T style layout $S rect -detach yes -iexpand xy; 
    $T style layout $S name -detach no -iexpand xy -expand e; 

    $T column create -tag name -itemstyle $S -text Items 
    $T configure -treecolumn first; 
} 

proc ::at::GUI::configScroll {args} { 
    variable Priv; 
    variable OptionsRB; 

    if {$OptionsRB(scrollTree) eq "LeftTree"} { 
    #$Priv(treeSecondary) -yscrollcommand ""; 
    $Priv(treePrimary) -yscrollcommand "::at::GUI::yset $Priv(treeScrollbar)"; 
    } elseif {$OptionsRB(scrollTree) eq "RightTree"} { 
    #$Priv(treePrimary) -yscrollcommand ""; 
    $Priv(treeSecondary) -yscrollcommand "::at::GUI::yset $Priv(treeScrollbar)"; 
    } elseif {$OptionsRB(scrollTree) eq "BothTree"} { 
    $Priv(treePrimary) -yscrollcommand "::at::GUI::yset $Priv(treeScrollbar)"; 
    $Priv(treeSecondary) -yscrollcommand "::at::GUI::yset $Priv(treeScrollbar)"; 
    } 
} 

proc ::at::GUI::yset {sb args} { 
    uplevel [linsert $args 0 $sb set] 
    ::at::GUI::yview moveto [lindex [$sb get] 0] 
} 

proc ::at::GUI::yview {args} { 
    variable Priv; 
    variable OptionsRB; 
    if {$OptionsRB(scrollTree) eq "LeftTree"} { 
    eval [linsert $args 0 $Priv(treePrimary) yview]; 
    } elseif {$OptionsRB(scrollTree) eq "RightTree"} { 
    eval [linsert $args 0 $Priv(treeSecondary) yview]; 
    } elseif {$OptionsRB(scrollTree) eq "BothTree"} { 
    eval [linsert $args 0 $Priv(treePrimary) yview]; 
    eval [linsert $args 0 $Priv(treeSecondary) yview]; 
    } 
} 

Дело в том, что всякий раз, когда я пытаюсь нажать на radiobuttion, я получаю сообщение об ошибке, говоря:

плохо команду «-yscrollcommand»: должен быть активировать, BBox, canvasx, canvasy, cget, collapse, column, compare, configure, contentbox, debug, depth, dragimage, element, expand, gradient, header, ident, index, item, marquee, notify, numcolumns, numitems, thephans, range , scan, see, selection, state, style, theme, toggle, xview, или yview bad запятая nd "-yscrollcommand": необходимо активировать, bbox, canvasx, canvasy, cget, collapse, column, compare, configure, contentbox, debug, depth, dragimage, element, expand, gradient, header, ident, index, item, marquee, notify, numcolumns, numitems, сироты, диапазон, scan, see, выбор, состояние, стиль, тема, toggle, xview или yview при выполнении "$ Priv (treeSecondary) -yscrollcommand" :: at :: GUI :: yset $ Priv (treeScrollbar) "" (процедура «:: at :: GUI :: configScroll» строка 10) , вызванный из «:: at :: GUI :: configScroll» , вызванный из «.scrollTreeOptions. rb2 invoke " , вызванный изнутри. .scrollTreeOptions.rb2 instate {нажата! disabled} {.scrollTreeOptions.rb2 state! нажата; .scrollTreeOptions.rb2 вызова} " (команда обязана событию)

Но забавная часть, даже если я получаю сообщение об ошибке, если я переместить полосу прокрутки, это переключение между виджетом и работает по желанию . Но ошибка что-то я не могу понять.

Это мой самый первый проект ТЗ, так что я не уверен, что мне не хватает какой-то важной информации. Любые комментарии ??

+0

Рекомендуется использовать 'list' или proc для создания ваших обратных вызовов, которые могут сделать их быстрее, например. '-yscrollcommand [list :: at :: GUI :: yset $ Priv (treeScrollbar)] – schlenk

ответ

1

сообщение об ошибке является В самом деле, команда, содержащаяся в переменной Priv(treeSecondary), не имеет подкоманды с именем -yscrollcommand. Я думаю, вы хотите использовать подкоманду configure, чтобы изменить значение параметра -yscrollcommand. Попробуйте что-то вроде:

$Priv(treeSecondary) configure -yscrollcommand "::at::GUI::yset $Priv(treeScrollbar)" 
+0

Спасибо, Энди, что работает. –

Смежные вопросы