ZetCode

Tcl/Tk 中的对话框

最后修改于 2023 年 10 月 18 日

在本 Tcl/Tk 教程中,我们将学习对话框。

对话框定义为两个人或更多人之间的对话。在计算机应用程序中,对话框是一个用于与应用程序“对话”的窗口。对话框用于输入数据、修改数据、更改应用程序设置等。对话框是用户与计算机程序之间重要的沟通手段。

消息框

消息框是方便的对话框,它向应用程序的用户提供消息。消息由文本和图像数据组成。Tk 中的消息框使用 tk_messageBox 命令创建。

message_boxes.tcl
#!/usr/bin/wish

# ZetCode Tcl/Tk tutorial
#
# In this program, we show various
# message boxes.
#
# Author: Jan Bodnar
# Website: www.zetcode.com


frame .fr
pack .fr 

ttk::button .fr.erButton -text Error -command onError
grid .fr.erButton 
ttk::button .fr.wButton -text Warning -command onWarn
grid .fr.wButton -row 1 -column 0 
ttk::button .fr.queButton -text Question -command onQuest
grid .fr.queButton -row 0 -column 1 -sticky we -columnspan 6
ttk::button .fr.infButton -text Information -command onInfo
grid .fr.infButton -row 1 -column 1


proc onError {} {
    tk_messageBox -type ok -icon error -title Error \
    -message "Could not open file"
}

proc onWarn {} {
    tk_messageBox -type ok -icon warning -title Warning \
    -message "Deprecated function call"
}

proc onQuest {} {
    tk_messageBox -type ok -icon question -title Question \
    -message "Are you sure to quit?"
}

proc onInfo {} {
    tk_messageBox -type ok -icon info -title Information \
    -message "Download completed"
}


wm title . "message boxes" 
wm geometry . 300x250+300+300

我们使用网格管理器来设置四个按钮的网格。每个按钮显示不同的消息框。

ttk::button .fr.erButton -text Error -command onError
grid .fr.erButton 

我们创建一个错误按钮,它调用 onError 过程。在方法内部,我们显示错误消息对话框。该按钮放置在网格的第一个单元格中。ttk 命名空间内的控件被主题化。buttonttk::button 在功能上是相同的按钮。区别在于我们可以在后者上应用主题。

proc onError {} {
    tk_messageBox -type ok -icon error -title Error \
    -message "Could not open file"
}

如果我们按下了错误按钮,我们将显示错误对话框。我们使用 tk_messageBox 命令创建消息框。-type 选项指定对话框中显示哪些按钮。在我们的例子中,它是一个 OK 按钮。-icon 指定要显示的图标类型。-title 提供对话框的标题,而 -message 提供其消息。

Warning message dialog
图:警告消息对话框

颜色选择器

颜色选择器是一个用于选择颜色的对话框。我们使用 tk_chooseColor 命令来显示对话框。

color_chooser.tcl
#!/usr/bin/wish

# ZetCode Tcl/Tk tutorial
#
# In this script, we use tk_chooseColor
# dialog to change the colour of the text.
#
# Author: Jan Bodnar
# Website: www.zetcode.com


label .l -text ZetCode
place .l -x 20 -y 90

button .b -text "Choose a color..." \
        -command "onSelect .l" 
place .b -x 20 -y 30

wm title . "color dialog" 
wm geometry . 350x200+300+300


proc onSelect {widget} {
    set col \
        [tk_chooseColor -title "Choose a color" -parent .]
    $widget configure -foreground $col
}

我们有一个按钮和一个标签。单击该按钮,我们会显示一个颜色选择器对话框。我们通过从对话框中选择一种颜色来更改标签文本的颜色。

label .l -text ZetCode
place .l -x 20 -y 90

我们创建一个 label 控件并将其放置在窗口上。

button .b -text "Choose a color..." \
        -command "onSelect .l" 
place .b -x 20 -y 30

我们创建一个 button 控件并将其放置在窗口上。我们将标签的控件路径传递给 onSelect 过程,该过程显示对话框并更改标签的颜色。

proc onSelect {widget} {
    set col \
        [tk_chooseColor -title "Choose a color" -parent .]
    $widget configure -foreground $col
}

onSelect 过程内部,我们显示对话框并更改标签颜色。首先,我们显示对话框并将选择的颜色值存储在 col 变量中。稍后,我们使用 configure 命令更改标签的前景色。该命令在控件的路径名上执行。标签的路径名被传递给该过程。

Color chooser
图:颜色选择器

文件对话框

tk_getOpenFile 对话框允许用户从文件系统中选择一个文件。

file_dialog.tcl
#!/usr/bin/wish

# ZetCode Tcl/Tk tutorial
#
# In this program, we use the
# tk_getOpenFile dialog to select a file from
# a filesystem.
#
# Author: Jan Bodnar
# Website: www.zetcode.com


set types {
    {"All Source Files"     {.tcl .tk } }
    {"Image Files"          {.gif .png .jpg} }
    {"All files"            *}
}

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

label .l -text "..."
place .l -x 20 -y 90

button .b -text "Select a file" \
        -command "onSelect .l"
place .b -x 20 -y 30


wm title . "openfile" 
wm geometry . 350x200+300+300

在我们的代码示例中,我们使用 tk_getOpenFile 对话框来选择一个文件,并在 label 控件中显示其名称。

set types {
    {"All Source Files"     {.tcl .tk } }
    {"Image Files"          {.gif .png .jpg} }
    {"All files"            *}
}

这些是文件过滤器。这些过滤器可用于仅在对话框中显示特定文件。

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

我们使用 tk_getOpenFile 命令显示对话框。我们使用 -filetypes 选项应用文件过滤器。选择的文件名存储在 file 变量中。configure 命令用于更改标签的文本。

tk_getOpenFile
图:文件对话框

在本 Tcl/Tk 教程中,我们使用了对话框窗口。