原文: http://zetcode.com/gui/tcltktutorial/dialogs/

在 Tcl/Tk 教程的这一部分中,我们将使用对话框。

对话框窗口或对话框是大多数现代 GUI 应用必不可少的部分。 对话被定义为两个或更多人之间的对话。 在计算机应用中,对话框是一个窗口,用于与应用“对话”。 对话框用于输入数据,修改数据,更改应用设置等。对话框是用户与计算机程序之间进行通信的重要手段。

MessageDialog

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

  1. #!/usr/bin/wish
  2. # ZetCode Tcl/Tk tutorial
  3. #
  4. # In this program, we show various
  5. # message boxes.
  6. #
  7. # author: Jan Bodnar
  8. # last modified: March 2011
  9. # website: www.zetcode.com
  10. frame .fr
  11. pack .fr
  12. ttk::button .fr.erButton -text Error -command onError
  13. grid .fr.erButton
  14. ttk::button .fr.wButton -text Warning -command onWarn
  15. grid .fr.wButton -row 1 -column 0
  16. ttk::button .fr.queButton -text Question -command onQuest
  17. grid .fr.queButton -row 0 -column 1 -sticky we -columnspan 6
  18. ttk::button .fr.infButton -text Information -command onInfo
  19. grid .fr.infButton -row 1 -column 1
  20. proc onError {} {
  21. tk_messageBox -type ok -icon error -title Error \
  22. -message "Could not open file"
  23. }
  24. proc onWarn {} {
  25. tk_messageBox -type ok -icon warning -title Warning \
  26. -message "Deprecated function call"
  27. }
  28. proc onQuest {} {
  29. tk_messageBox -type ok -icon question -title Question \
  30. -message "Are you sure to quit?"
  31. }
  32. proc onInfo {} {
  33. tk_messageBox -type ok -icon info -title Information \
  34. -message "Download completed"
  35. }
  36. wm title . "message boxes"
  37. wm geometry . 300x150+300+300

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

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

我们创建一个错误按钮,它调用onError过程。 在方法内部,我们显示错误消息对话框。 该按钮将放置在网格的第一个单元格中。 ttk名称空间内的小部件为主题。 就功能而言,buttonttk::button是相同的按钮。 不同之处在于我们可以将主题应用于后者。

  1. proc onError {} {
  2. tk_messageBox -type ok -icon error -title Error \
  3. -message "Could not open file"
  4. }

如果按下错误按钮,则会显示错误对话框。 我们使用tk_messageBox命令创建消息框。 -type选项指定对话框中显示哪些按钮。 在我们的情况下,这是一个确定按钮。 -icon指定要显示的图标的类型。 -title提供对话框的标题,-message提供消息。

Tcl/Tk 中的对话框 - 图1

图:警告消息对话框

颜色选择器

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

  1. #!/usr/bin/wish
  2. # ZetCode Tcl/Tk tutorial
  3. #
  4. # In this script, we use tk_chooseColor
  5. # dialog to change the colour of the text.
  6. #
  7. # author: Jan Bodnar
  8. # last modified: March 2011
  9. # website: www.zetcode.com
  10. label .l -text ZetCode
  11. place .l -x 20 -y 90
  12. button .b -text "Choose a color..." \
  13. -command "onSelect .l"
  14. place .b -x 20 -y 30
  15. wm title . "color dialog"
  16. wm geometry . 350x200+300+300
  17. proc onSelect {widget} {
  18. set col \
  19. [tk_chooseColor -title "Choose a color" -parent .]
  20. $widget configure -foreground $col
  21. }

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

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

我们创建一个label小部件并将其放在窗口中。

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

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

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

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

Tcl/Tk 中的对话框 - 图2

图:颜色选择器

文件对话框

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

  1. #!/usr/bin/wish
  2. # ZetCode Tcl/Tk tutorial
  3. #
  4. # In this program, we use the
  5. # tk_getOpenFile dialog to select a file from
  6. # a filesystem.
  7. #
  8. # author: Jan Bodnar
  9. # last modified: March 2011
  10. # website: www.zetcode.com
  11. set types {
  12. {"All Source Files" {.tcl .tk } }
  13. {"Image Files" {.gif .png .jpg} }
  14. {"All files" *}
  15. }
  16. proc onSelect { label } {
  17. global types
  18. set file [tk_getOpenFile -filetypes $types -parent .]
  19. $label configure -text $file
  20. }
  21. label .l -text "..."
  22. place .l -x 20 -y 90
  23. button .b -text "Select a file" \
  24. -command "onSelect .l"
  25. place .b -x 20 -y 30
  26. wm title . "openfile"
  27. wm geometry . 350x200+300+300

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

  1. set types {
  2. {"All Source Files" {.tcl .tk } }
  3. {"Image Files" {.gif .png .jpg} }
  4. {"All files" *}
  5. }

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

  1. proc onSelect { label } {
  2. global types
  3. set file [tk_getOpenFile -filetypes $types -parent .]
  4. $label configure -text $file
  5. }

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

Tcl/Tk 中的对话框 - 图3

图:tk_getOpenFile

在 Tcl/Tk 教程的这一部分中,我们使用了对话框窗口。