一旦我输入了select这个词,它怎么能瞬间显示在标签中呢?

Once I select the word, how it can be instantaneously displayed in the label?

open Tk;;
let top = openTk ()
let _ = Wm.title_set top "Listbox 2"
let v = Textvariable.create ();;
Textvariable.set v " ? " ;;
let l = Label.create ~textvariable:v
    ~background:(`Color "#FDF1B8")
    ~foreground:(`Color "#0F056B")
    top
let mylist = Listbox.create
    ~selectmode:`Single 
    ~background:(`Color "#FF7F00")
    ~foreground:(`Color "#3F2204")
    ~selectbackground:(`Color "#BEF574")
    ~selectforeground:(`Color "#095228")
    top
let some_composers = ["Mozart";"Chopin";
              "Beethoven";"Verdi";"Bizet"]
let _ = Listbox.insert
    ~index:`End
    ~texts:some_composers
    mylist
let b = Button.create ~text:"Show selected composer"
    ~command:(fun () ->
      try
    let n = match (List.hd (Listbox.curselection mylist)) with
    | `Num y -> y
    | _ -> failwith "No Selection"
    in
    Textvariable.set v (List.nth some_composers n)
      with _ -> (print_endline "No Selection"; flush stdout)
         )
    top
let bq = Button.create ~text:"Quit"
    ~command:(fun () ->
         print_endline "Bye."; flush stdout; closeTk ())
    top;;
pack [l];;
pack [mylist];;
pack [b];;
pack [bq];;
let _ = Printexc.print mainLoop ()

上面的代码很简单。它允许 select 列表框中的特定音乐家,按下列表框下方的按钮,然后音乐家的名字显示在 window 顶部的标签中。

不使用按钮,我想删除按钮,当我select音乐家的名字时,它会立即显示在windows上方的标签中。

有正确的方法吗?

尝试

open Tk;;
let top = openTk ()
let _ = Wm.title_set top "Listbox 2"
let v = Textvariable.create ();;
Textvariable.set v " ? " ;;

let mylist = Listbox.create
    ~selectmode:`Single 
    ~background:(`Color "#FF7F00")
    ~foreground:(`Color "#3F2204")
    ~selectbackground:(`Color "#BEF574")
    ~selectforeground:(`Color "#095228")
    top
let some_composers = ["Mozart";"Chopin";
              "Beethoven";"Verdi";"Bizet"]
let _ = Listbox.insert
    ~index:`End
    ~texts:some_composers
    mylist

let n = List.hd (Listbox.curselection mylist) in
Textvariable.set v (List.nth some_composers n);
let l = Label.create 
    ~textvariable:(Textvariable.get mylist)
    ~background:(`Color "#FDF1B8")
    ~foreground:(`Color "#0F056B")
    top

let bq = Button.create ~text:"Quit"
    ~command:(fun () ->
         print_endline "Bye."; flush stdout; closeTk ())
    top;;
pack [l];;
pack [mylist];;
pack [bq];;
let _ = Printexc.print mainLoop ()

我现在只有时间快速回答,但也许这会有所帮助。

列表框的默认处理程序不会执行您想要的操作,它们只会更改您单击的项目的外观。但是您可以使用 bind 函数为按钮单击建立任何所需的处理。

tk bind 函数用于指定当用户界面中发生事件时应该发生什么。您想要做的是指定在列表框中单击按钮 1(比方说)时发生的事情。我的系统上没有安装 labltk,所以我不能尝试这段代码,但它大致是这样的:

bind ~events: [`ButtonPressDetail 1] ~action: myhandler listbox

在此之前(当然)您需要定义myhandler以插入所需的文本。

更新

这段代码对我有用。请注意,您要绑定到按钮释放事件(以便选择已经发生)。

open Tk

let some_composers =
    ["Mozart"; "Chopin"; "Beethoven"; "Verdi"; "Bizet"]

let main () =
    let top = openTk () in
    Wm.title_set top "Listbox 2";
    let v = Textvariable.create () in
    Textvariable.set v "?";
    let l =
        Label.create
            ~textvariable: v
            ~background: (`Color "#FDF1B8")
            ~foreground: (`Color "#0F056B")
            top
    in
    let mylist =
        Listbox.create
            ~selectmode: `Single
            ~background: (`Color "#FF7F00")
            ~foreground: (`Color "#3F2204")
            ~selectbackground:(`Color "#BEF574")
            ~selectforeground:(`Color "#095228")
            top
    in
    Listbox.insert ~index: `End ~texts: some_composers mylist;
    let set_composer ev =
        match Listbox.curselection mylist with
        | [] -> () (* Not really possible *)
        | index :: _ ->
            Textvariable.set v (Listbox.get mylist index)
    in
    bind ~events: [`ButtonReleaseDetail 1]
         ~action: set_composer
         mylist;
    let bq = Button.create
        ~text: "Quit"
        ~command:
            (fun () ->
                print_endline "Bye.";
                flush stdout;
                closeTk ())
        top
    in
    pack [l];
    pack [mylist];
    pack [bq];
    Printexc.print mainLoop ()

let () = main ()

当我 运行 这样时,我看到了您想要的行为:

$ labltk m.ml

希望对您有所帮助。