将文件名从按钮重新定位到条目并将文件名用作全局变量

Reposition file name from button to entry and use file names as global variables

目前:

执行前(图1)

执行后(图2)

我很差 tcl/tk。但是现在我正在学习 tcl/tk 为了我的工作。我想让 GUI 像图形一样。图 1 是在单击按钮之前。如果我单击右侧按钮和 select 文件,文件名将出现在按钮文本中。我想将这些名称定位在中间侧入口区域,并想将这些文件名用作全局变量。这是我的代码。谁能帮帮我?

#-------------------------------------------------------------------------
namespace eval ::dyna::partupdate {

    variable p_mainWin ".partupdate";

    variable str_curDir [file dirname [info script]];
    variable str_basefile "";
    variable str_appendfile "";
    variable str_spotweldfile "";
    variable str_outputfile "";
}

set types {
        {"LS-DYNA Files"     {.key .k .dyn .d } }
        {"IGES Files"     {.iges .igs } }
        {"MCF Files"     {.mcf } }
        {"All files"            *}
}

##############################################################################
##### Procedure Name: ::dyna::partupdate::CreateGUI
##### Functionality: creates the main window for file selection
##############################################################################
proc ::dyna::partupdate::CreateGUI {} {
    variable p_mainWin;

    destroy $p_mainWin;
    set base [ toplevel $p_mainWin];
    wm title $base {Update FE Model};
    wm resizable $base 1 1;

    set frm_main [frame $base.frm_main];
    pack $frm_main -side top -anchor nw -padx 0 -pady 0;

    set frm_fileselect [frame $frm_main.frm_fileselect -bd 0 -relief groove];
    set frm_create [frame $frm_main.frm_create -bd 0 -relief groove]
#    pack $frm_create -side right -anchor ne -padx 0 -pady 4;

    grid $frm_fileselect -row 0 -padx 1 -pady 4 -sticky nw;
    grid $frm_create     -row 3 -padx 1 -pady 4 -sticky ne; 

#    set frm_create [frame $frm_main.frm_create -bd 0 -relief groove]
#    pack $frm_create -side right -anchor ne -padx 0 -pady 4;

    # UI for base file
    set lbl_basefile [label $frm_fileselect.lbl_basefileName -text "Base File"];
    set ent_basefile [entry $frm_fileselect.ent_basefileName -width 30 \
                       -textvariable "::dyna::partupdate::str_basefile" -state disabled];
    set btn_basefile [button $frm_fileselect.btn_basefile -text " ... " \
                       -command "::dyna::partupdate::onSelect $frm_fileselect.btn_basefile" ]

    grid $lbl_basefile -row 0 -col 0 -padx 5 -pady 7 -sticky nw;
    grid $ent_basefile -row 0 -col 1 -padx 5 -pady 7 -sticky nw;
    grid $btn_basefile -row 0 -col 2 -padx 0 -pady 6 -sticky nw;

    # UI for appended file
    set lbl_appendfile [label $frm_fileselect.lbl_appendfileName -text "Appended File"];
    set ent_appendfile [entry $frm_fileselect.ent_appendfileName -width 30 \
                       -textvariable "::dyna::partupdate::str_appendfile" -state disabled];
    set btn_appendfile [button $frm_fileselect.btn_appendfile -text " ... " \
                       -command "::dyna::partupdate::onSelect $frm_fileselect.btn_appendfile" ]

    grid $lbl_appendfile -row 1 -col 0 -padx 5 -pady 2 -sticky nw;
    grid $ent_appendfile -row 1 -col 1 -padx 5 -pady 2 -sticky nw;
    grid $btn_appendfile -row 1 -col 2 -padx 0 -pady 6 -sticky nw;

    # UI for spotweld file
    set lbl_spotweldfile [label $frm_fileselect.lbl_spotweldfileName -text "Spotweld File"];
    set ent_spotweldfile [entry $frm_fileselect.ent_spotweldfileName -width 30 \
                       -textvariable "::dyna::partupdate::str_spotweldfile" -state disabled];
    set btn_spotweldfile [button $frm_fileselect.btn_spotweldfile -text " ... " \
                       -command "::dyna::partupdate::onSelect $frm_fileselect.btn_spotweldfile" ]

    grid $lbl_spotweldfile -row 2 -col 0 -padx 5 -pady 2 -sticky nw;
    grid $ent_spotweldfile -row 2 -col 1 -padx 5 -pady 2 -sticky nw;
    grid $btn_spotweldfile -row 2 -col 2 -padx 0 -pady 6 -sticky nw;

    # UI for output file
    set lbl_outputfile [label $frm_fileselect.lbl_outputfileName -text "Output File"];
    set ent_outputfile [entry $frm_fileselect.ent_outputfileName -width 30 \
                       -textvariable "::dyna::partupdate::str_outputfile" -state disabled];
    set btn_outputfile [button $frm_fileselect.btn_outputfile -text " ... " \
                       -command "::dyna::partupdate::saveAs $frm_fileselect.btn_outputfile" ]

    grid $lbl_outputfile -row 3 -col 0 -padx 5 -pady 2 -sticky nw;
    grid $ent_outputfile -row 3 -col 1 -padx 5 -pady 2 -sticky nw;
    grid $btn_outputfile -row 3 -col 2 -padx 0 -pady 6 -sticky nw;

    set btn_update [hwt::CanvasButton $frm_create.btn_update \
    [::hwt::DluWidth 40] [::hwt::DluHeight 14] \
    -command "::dyna::partupdate::uPDATE" \
    -text "Update" ]

    set btn_close [hwt::CanvasButton $frm_create.btn_close \
    [::hwt::DluWidth 40] [::hwt::DluHeight 14] \
    -command "::dyna::partupdate::CloseWindow" \
    -text "Close" ]

    pack $btn_update -anchor ne -padx 5 -pady 5 -side left;
    pack $btn_close -anchor ne -padx 5 -pady 5 -side left;
}

proc ::dyna::partupdate::onSelect { label } {
    global types   
    set file [tk_getOpenFile -filetypes $types -parent .]
    $label configure -text $file
}

proc ::dyna::partupdate::saveAs { label } {
    global types   
    set file [tk_getSaveFile -filetypes $types -parent . -initialdir .]
    $label configure -text $file
}

proc ::dyna::partupdate::uPDATE { args } {
    variable str_basefile;
    variable str_appendfile;
    variable str_spotweldfile;
    variable str_outputfile;

    set executable C:\CENSor\CENSor.exe

    # base file name should not be empty
    if {$str_basefile == ""} {
        tk_messageBox -message "Please select an base file." -title "aaa" \
            -type ok -icon info;
        return;
    }

    # append file name should not be empty
    if {$str_appendfile == ""} {
        tk_messageBox -message "Please select an appended file." -title "aaa" \
            -type ok -icon info;
        return;
    }

    # execution case 
    if {$str_spotweldfile == ""} {
          exec $executable -e 0 $str_outputfile $str_basefile $str_appendfile
    }
    else {
          exec $executable -e 0 $str_outputfile $str_basefile $str_appendfile $str_spotweldfile
    }
}

proc ::dyna::partupdate::CloseWindow { args } {
    variable p_mainWin;

    set windowPath [lindex $args 0];
    if {[winfo exists $p_mainWin]} {
        destroy $p_mainWin
    }
}

# ----- bring up the main gui --------
::dyna::partupdate::CreateGUI;

对于第一个,这一行:

set btn_basefile [button $frm_fileselect.btn_basefile -text " ... " \
                   -command "::dyna::partupdate::onSelect $frm_fileselect.btn_basefile" ]

将执行此过程:

proc ::dyna::partupdate::onSelect { label } {
    global types   
    set file [tk_getOpenFile -filetypes $types -parent .]
    $label configure -text $file
}

当执行 onSelect 时,上面的第一行将更新作为按钮的小部件 $frm_fileselect.btn_basefile。您首先需要更改第一行以修改输入框,如下所示:

set btn_basefile [button $frm_fileselect.btn_basefile -text " ... " \
                   -command "::dyna::partupdate::onSelect $frm_fileselect.ent_basefile" ]

然后更改proc,使其能够编辑条目文本(条目没有-text选项,您必须插入。通常最好删除所有内容以防万一)

proc ::dyna::partupdate::onSelect { label } {
    global types   
    set file [tk_getOpenFile -filetypes $types -parent .]
    $label configure -state normal   ;# makes the entry normal to allow editing
    $label delete 0 end              ;# delete everything
    $label insert end $file          ;# insert the text
    $label configure -state disabled ;# disable the entry again
}

然后你将不得不类似地修改剩余的 3 个按钮:

set btn_appendfile [button $frm_fileselect.btn_appendfile -text " ... " \
                   -command "::dyna::partupdate::onSelect $frm_fileselect.ent_appendfile" ]

set btn_spotweldfile [button $frm_fileselect.btn_spotweldfile -text " ... " \
                   -command "::dyna::partupdate::onSelect $frm_fileselect.ent_spotweldfile" ]

set btn_outputfile [button $frm_fileselect.btn_outputfile -text " ... " \
                   -command "::dyna::partupdate::saveAs $frm_fileselect.ent_outputfile" ]

manual for entry widget


至于最后一部分,您真的不需要它们是全局的。可以使用widget get,例如:

set filename [.partupdate.frm_main.frm_fileselect.ent_basefile get]

会给你 Base File 条目中的文本(当然,如果你在变量中有小部件路径,它会更容易)

set filename [.partupdate.frm_main.frm_fileselect.ent_spotweldfile get]

会给你一个Spotweld File,等等。


旁注:我可能会将 onSelect 中的变量从 label 重命名为 entry