Can you change a widget's parent in python tkinter?

懵懂的女人 提交于 2019-12-04 02:01:09

An old post by Fredrik Lundh (the Tkinter author) suggests that it isn't possible to change a widget's parent.

You cannot move a widget or group of widgets to a new parent but you can simulate that with some simple routines. I don't work in python but you should be able to convert the following code to tkinter from the tcl. By simulate I mean you copy the widget and any chidren recursively to a new parent. Tk provides the introspection needed to exactly copy the layout, bindings, and look of the widget to be moved/copied including all subwidgets. The routines that follow will allow you to move or copy both single or complex widgets to a new parent.

proc getWidgetType { w } {
    set class [winfo class $w ]
    if { [ string index $class 0 ] eq "T" &&
     [ string match "\[A-Z\]" [string index $class 1 ] ] } {
        set class [string range [string tolower $class ] 1 end ]
        set class "ttk::$class"
    } else {
        set class [string tolower $class ]
    }
    return $class
}
proc getConfigOptions { w } {
    set configure [ $w configure ]
    set options {}
    foreach f $configure {
        if { [llength $f ] < 3 } { continue; } 
        set name    [ lindex $f 0 ]
        set default [ lindex $f end-1 ]
        set value   [ lindex $f end ]
        if { $default ne $value } {
            lappend options $name $value 
        }
    }
    return $options
}
proc copyWidget { w  newparent { level 0 } } {
    set type [ getWidgetType $w ]
    set name [ string trimright $newparent.[lindex [split $w "." ] end ] "." ]  
    set retval [ $type $name {*}[ getConfigOptions $w ] ]
    foreach b [ bind $w ] {
        puts "bind $retval $b [subst { [bind $w $b ] } ] " 
        catch { bind $retval $b  [subst { [bind $w $b ] } ] } 
    } 
    if { $level > 0 } {
        if { [ catch { pack info $w } err ] == 0 } {
            array set temp [ pack info $w ]
            array unset temp -in
            catch { pack $name {*}[array get temp ] } 
        } elseif { [ catch { grid info $w } err ] == 0 } {
            array set temp [ grid info $w ]
            array unset temp -in
            catch { grid $name {*}[array get temp ] } 
        }
    }
    incr level 
    if { [ pack slaves $w ] ne "" } { 
        foreach f [ pack slaves $w ] {
            copyWidget $f $name $level
        }
    } else {
        foreach f [winfo children $w ] {
            copyWidget $f $name $level
        }
    }
    return $retval
}
proc moveWidget { w newparent } {
    set retval [ copyWidget $w $newparent ]
    destroy $w
    return $retval
}

# assume we have already created a toplevel with complex layout named
# .input with subframe .input.frame.tframe that we want to transfer to
# a new toplevel .x .  There is a cancel button we want to transfer
# also at .input.frame.bframe.icancel and we will grid it into
# .x.tframe .

toplevel .x
set form [ moveWidget .input.frame.tframe .x ]
set cancel [ moveWidget .input.frame.bframe.icancel .x.tframe ]
grid $cancel -row 2 -column 2 -sticky new 
pack $form -anchor center -expand 1 -fill both -side top
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!