是否有任何方法可以提供tk_messagebox一个来自TCL本身的输入,而不是通过用户点击按钮?

是否有任何方法可以提供tk_messagebox一个来自TCL本身的输入,而不是通过用户点击按钮?,tcl,tk,Tcl,Tk,我有一个触发tk_messageBox的条件。现在,在停留10秒钟后,我想让它消失,输入“ok”,而不需要用户点击或交互。有什么办法可以做到吗 if {condition is matched} { set input [tk_messageBox -message $message -icon <iconType> -type <messageBoxType>] } 如果{条件匹配}{ 设置输入[tk_messag

我有一个触发tk_messageBox的条件。现在,在停留10秒钟后,我想让它消失,输入“ok”,而不需要用户点击或交互。有什么办法可以做到吗

        if {condition is matched} {
          set input [tk_messageBox -message $message -icon <iconType> -type <messageBoxType>]
         }
如果{条件匹配}{
设置输入[tk_messageBox-message$message-icon-type]
}

标准消息框不支持此功能,因为至少有一个平台上的底层操作系统对话框(不记得是Windows还是macOS)不支持此功能

但您可以访问该对话框的脚本版本(Linux上的默认版本)并插入触发器:

# Set up the timeout; I got the widget name by reading the sources
set handle [after 10000 .__tk__messagebox.no invoke]

# This is the internal name of the implementation
set answer [tk::MessageBox -type yesno -message "Foo"]

# Make sure we clear our timeout in case the user *did* pick something
after cancel $handle

不同的消息框类型对默认按钮(名称的最后一部分)有不同的名称,但它们将是
ok
cancel
no
abort
,具体取决于对话框类型。这应该是显而易见的。(或者,只需销毁
。\uuu tk_messagebox
窗口。我认为这也会选择默认值。)

标准消息框不支持这一点,因为至少有一个平台上的底层操作系统对话框(不记得是Windows还是macOS)不支持这一点

但您可以访问该对话框的脚本版本(Linux上的默认版本)并插入触发器:

# Set up the timeout; I got the widget name by reading the sources
set handle [after 10000 .__tk__messagebox.no invoke]

# This is the internal name of the implementation
set answer [tk::MessageBox -type yesno -message "Foo"]

# Make sure we clear our timeout in case the user *did* pick something
after cancel $handle

不同的消息框类型对默认按钮(名称的最后一部分)有不同的名称,但它们将是
ok
cancel
no
abort
,具体取决于对话框类型。这应该是显而易见的。(或者,只需销毁
。\uuuTk\uMessageBox
窗口。我认为这也会选择默认值。)

这是使用toplevel的另一种可能的解决方案:

set message "The quick brown fox jumps over the lazy dog"

if {condition is matched} {
    toplevel .new_window

    wm title      .new_window "Window title"
    wm geometry   .new_window 320x100
    wm minsize    .new_window 320 100
    wm maxsize    .new_window 320 100
    wm protocol   .new_window WM_DELETE_WINDOW {destroy .new_window}
    wm attributes .new_window -topmost yes

    set input -1

    pack [label .new_window.message -text $message] -fill none -expand 1
    pack [frame .new_window.buttons] -fill none -expand 1

    pack [ttk::button .new_window.buttons.button1 -text "OK" -command {
        set input 1
        destroy .new_window
    }] -side left -padx 5

    pack [ttk::button .new_window.buttons.button2 -text "Cancel" -command {
        set input 0
        destroy .new_window
    }] -side right -padx 5

    after 10000 {
        if {[winfo exists .new_window]} {
            destroy .new_window
            if {$input == -1} {
                set input 1
            }
        }
    }
}
变量$input保存用户输入值(0或1),在10秒钟的超时后,不单击,窗口将自动关闭,deafult值为1(确定)

但是,请注意,在单击之前或在超时过期之前,$input变量的值为-1

编辑:为了避免后一种不确定的行为,您可能需要:

set message "The quick brown fox jumps over the lazy dog"

if {condition is matched} {
    toplevel .new_window

    wm title      .new_window "Window title"
    wm geometry   .new_window 320x100
    wm minsize    .new_window 320 100
    wm maxsize    .new_window 320 100
    wm protocol   .new_window WM_DELETE_WINDOW {
        set input 1
        destroy .new_window
    }
    wm attributes .new_window -topmost yes

    if {[info exists input]} {
        unset input
    }

    pack [label .new_window.message -text $message] -fill none -expand 1
    pack [frame .new_window.buttons] -fill none -expand 1

    pack [ttk::button .new_window.buttons.button1 -text "OK" -command {
        set input 1
        destroy .new_window
    }] -side left -padx 5

    pack [ttk::button .new_window.buttons.button2 -text "Cancel" -command {
        set input 0
        destroy .new_window
    }] -side right -padx 5

    after 10000 {
        if {[winfo exists .new_window]} {
            set input 1
            destroy .new_window}
        }
    }
    vwait input
}
暂停执行,等待用户输入或dafault应答


再见

这是使用toplevel的另一种可能的解决方案:

set message "The quick brown fox jumps over the lazy dog"

if {condition is matched} {
    toplevel .new_window

    wm title      .new_window "Window title"
    wm geometry   .new_window 320x100
    wm minsize    .new_window 320 100
    wm maxsize    .new_window 320 100
    wm protocol   .new_window WM_DELETE_WINDOW {destroy .new_window}
    wm attributes .new_window -topmost yes

    set input -1

    pack [label .new_window.message -text $message] -fill none -expand 1
    pack [frame .new_window.buttons] -fill none -expand 1

    pack [ttk::button .new_window.buttons.button1 -text "OK" -command {
        set input 1
        destroy .new_window
    }] -side left -padx 5

    pack [ttk::button .new_window.buttons.button2 -text "Cancel" -command {
        set input 0
        destroy .new_window
    }] -side right -padx 5

    after 10000 {
        if {[winfo exists .new_window]} {
            destroy .new_window
            if {$input == -1} {
                set input 1
            }
        }
    }
}
变量$input保存用户输入值(0或1),在10秒钟的超时后,不单击,窗口将自动关闭,deafult值为1(确定)

但是,请注意,在单击之前或在超时过期之前,$input变量的值为-1

编辑:为了避免后一种不确定的行为,您可能需要:

set message "The quick brown fox jumps over the lazy dog"

if {condition is matched} {
    toplevel .new_window

    wm title      .new_window "Window title"
    wm geometry   .new_window 320x100
    wm minsize    .new_window 320 100
    wm maxsize    .new_window 320 100
    wm protocol   .new_window WM_DELETE_WINDOW {
        set input 1
        destroy .new_window
    }
    wm attributes .new_window -topmost yes

    if {[info exists input]} {
        unset input
    }

    pack [label .new_window.message -text $message] -fill none -expand 1
    pack [frame .new_window.buttons] -fill none -expand 1

    pack [ttk::button .new_window.buttons.button1 -text "OK" -command {
        set input 1
        destroy .new_window
    }] -side left -padx 5

    pack [ttk::button .new_window.buttons.button2 -text "Cancel" -command {
        set input 0
        destroy .new_window
    }] -side right -padx 5

    after 10000 {
        if {[winfo exists .new_window]} {
            set input 1
            destroy .new_window}
        }
    }
    vwait input
}
暂停执行,等待用户输入或dafault应答


再见

见。这似乎是不可能的,但您可以创建自己的消息框(使用和等等)。请参阅。这似乎是不可能的,但您可以创建自己的消息框(使用和诸如此类)。谢谢您的帮助。:)谢谢你的帮助。:)谢谢你这么详细的帮助。我很感激。:)谢谢你这么详细的帮助。我很感激。:)