当前:
执行前(图1)
执行后(图2)
我的tcl / tk很差。但是如今,我正在为工作学习tcl / tk。我想制作图形用户界面。图1是单击按钮之前。如果单击右侧按钮并选择文件,则文件名将显示在按钮文本中。我想将这些名称放置在中间侧输入区域中,并希望将这些文件名称用作全局变量。这是我的代码。有人可以帮我吗?
#-------------------------------------------------------------------------
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;
答案 0 :(得分:0)
对于第一个,此行:
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
}
执行$frm_fileselect.btn_basefile
时,上面的第一行将更新作为按钮的窗口小部件onSelect
。您首先需要更改第一行以修改输入框,如下所示:
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" ]
对于最后一部分,您实际上并不需要它们具有全局性。您可以使用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
。