在tcl/tk中如何将简单的快速<Double-1>(鼠标按钮1双击)绑定到表格列表并获取选定的行内容?

问题描述 投票:0回答:1

这是我自己回答的问题:

In tcl/tk, how to bind <Double-1>  to tablelist and get the selected row contents ?

< Double-1 > 代表鼠标按钮 - 1 在 tcl/tk 命令 bind 中双击。对于惯用右手的人来说,默认情况下,Button-1 是鼠标左键。

我写这个问题只是为了在搜索引擎上建立索引。我花了很长时间才弄清楚它是如何完成的。

bind double-click tcltk
1个回答
0
投票

假设您有一份学生名单。在这里,您将它们放入表列表 .students_tablelist 中。阅读下面的代码以了解它是如何完成的。

 package require tablelist    
 
 proc display_selected_student { } {
   
      #fill the body of this proc with the what should happen on double click 
      # we are going to give an example here on how to extract the field in the selected row
        set selected_tablelist_row_contents [.students_tablelist get [.students_tablelist curselection] ]
        set student_id [ lindex   $selected_tablelist_row_contents 0 ]
        set student_name [ lindex $selected_tablelist_row_contents 1 ]
        set student_enrollment_date [ lindex $selected_tablelist_row_contents 2 ]

        #printing the three variables above on terminal/command line prompt screen
        # and in a message box
        puts "Student ID: $student_id , Student Name: $student_name , Enrollemnt Date: $student_enrollment_date"
        tk_messageBox -message "Student ID: $student_id , Student Name: $student_name , Enrollemnt Date: $student_enrollment_date"
}
 

 #define the tablelist called ".students_tablelist" 
 tablelist::tablelist .students_tablelist -columntitles { "Student ID"  "Student Name"  "Enrollment Date" } -stretch all -background white -height 15 -width 100 

 #some data about a list of students 
 set student_table_data {
       { 1 john 1-march-2024}
       {2 mary 2-march-2024}
       { 3 solo 3-march-2024} 
    }


#fill the tablelist with the three rows from $students_table_data
foreach row $student_table_data {   
  .students_tablelist insert end $row
}

#this bind command is how you attach a mouse double click event to the tablelist
bind [ .students_tablelist bodytag] <Double-1>   {
    #call the proc above and run it.
    display_selected_student
}
   #display the tablelist .students_table on the main root window
   grid .students_table -row 0 -column 0
© www.soinside.com 2019 - 2024. All rights reserved.