首页/文章/ 详情

tcl/tk学习笔记(4)|一个案例带你玩懂主题组件ttk::notebook

1年前浏览1774

本文介绍使用ttk::notebook组件设计多页面管理的GUI界面。ttk::notebook组件可将页面分为多页来管理,每一页有一个标签,可使用标签在多个页面之间进行切换,适合复杂的功能界面GUI设计。

例如下图为hypermesh自动探测实体孔的界面,该界面将功能界面分成了五页,点击其中一个页面标签,页面就会隐藏此前显示的页面窗口而切换到关联的页面,使用该界面便可实现较为复杂的hypermesh 探测二维和三维孔的功能。

         

图1 hypermesh探测实孔窗口界面

上图中包含的组件大致有为ttk::notebook 、ttk::combobox 、label、entry、radiobutton 、button等组件。模仿上面的页面,我将尝试做一个简化版的界面,如下图所示。

图2 本文设计界面

如何实现图2的多页面版图呢?源码走起!你细品>>>

    #create a notebook in the caseset nb [ttk::notebook .n -width 500 -height 500 -padding 30]grid $nbset f1 [ttk::frame $nb.f1 -relief raised -borderwidth 4 \-height 100 -width 50]set f2 [ttk::frame $nb.f2 -relief raised -borderwidth 4 \-height 100 -width 50]set f3 [ttk::frame $nb.f3 -relief raised -borderwidth 4 \-height 100 -width 50]$nb add $f1 -text "Preparation"$nb add $f2 -text "2D Holes"$nb add $f3 -text "3D Holes"#create Preparationset lbp_1 [label $f1.lb1 -fg blue -text "Select entities:" ]set lbp_2 [label $f1.lb2 -fg blue -text "Detect holes in:" ]grid $lbp_1 -sticky w -pady 1mgrid $lbp_2 -sticky w -pady 1mset mylist [list components elements]set comb [ttk::combobox $f1.cb -textvariable combName -value $mylist\-postcommand {puts "output the user selected item: $combName"}]#$comb config -postcommand {puts "output the user selected item: $combName"}grid $comb -sticky e -column 2 -row 0 -padx 150 -pady 1mset combName components#create 2DHolesset lst [list general Circular Rounded Square Rectangular]set lbd_1 [label $f2.lbd1 -fg blue -text "Hole Shape"]set comb_2d [ttk::combobox $f2.comb_2d -textvariable combovalue -values $lst \-justify right];#justify show position of arrowgrid $lbd_1 -sticky w -column 0 -row 0grid $comb_2d -sticky w -column 1 -row 0 -padx 180 -pady 1m$comb_2d set [lindex $lst 0] #set combovalue "general"#$comb_2d config -postcommand {puts "output the user selected item: $combovalue"}# it must be "<< >>" in the bind,not "< >"bind $comb_2d <<ComboboxSelected>> {    switch [%W get] {        "general" {puts "output the user selected item is: $combovalue"}        "Circular" {puts "output the user selected item: $combovalue"}        "Rounded" {puts "output the user selected item: $combovalue"}        "Square" {puts "output the user selected item: $combovalue"}        "Rectangular" {puts "output the user selected item: $combovalue"}    }}#3D holesset rdn_1 [radiobutton $f3.rdb1 -text "Minimum dimesion" -variable tag -value 5 -anchor w]set rdn_2 [radiobutton $f3.rdb2 -text "Maxinum dimesion" -variable tag -value 10 -anchor w]grid $rdn_1 -sticky w -column 0 -row 0grid $rdn_2 -sticky w -column 0 -row 1

    下面再对呈现的代码胡说八道一番,不喜跳过>>>

    实际上,notebook 将每一个页面的内容都分布在ttk::frameframe组件里,然后使用add 命令向notebook 添加可用的组件,例如每个f rame 中可布置button 、entry 、label 、ttk::combobox 等功能组件以实现二次开发的功能。

    主题组件ttk::combobox包括一个下拉框和一个输入框组件。该组件能从一系列固定值中选择值,类似于调节框,一次只能选择一个值。当选中-value中一个值时,便将该值赋给-textvariable下的变量。ttk::notebook在其值发生变化时,将《ComboboxSelected虚拟事件发送给自身,此时你可以使用组合框的get方法找出所选内容,然后对其进行操作以实现预定功能。此种方法,通常可结合bind命令辅以switch选择函数来实现 , 然后利用“%W get”获得当前事件组件的-value值。(% W 获得当前事件组件的完整路径名称)

      bind $comb_2d <<ComboboxSelected>> {script}

      当然,你也可以不使用bind来绑定ttk::notebook,借助ttk::notebook自带的-postcommand 命令选项也可实现变量的传递,如下:

        set comb [ttk::combobox $f1.cb -textvariable combName -value $mylist\-postcommand {puts "output the user selected item: $combName"}]

        接下来如何给ttk::notebook 赋初值呢?这里有两种方法:

          set textvarible value . comb_2d set [lindex $lst 0]

          效果如下图所示:

          图3 2D Holes页面初始值预览

          写在最后,社长最近推出hypermesh二次开发系列文章,此为基础篇tcl/tk的学习,好记性不如烂笔头,被知识冲昏头脑的你还不搭上社长的末班车
           
          往期推荐

          1、tcl/tk学习笔记(3)|快速入局菜单menu组件

          2、tcl/tk学习笔记(2)|一个简单计算器的GUI教给你的不只是计算

          3、tcl/tk学习笔记(1)|8行代码实现列表框和滚动条联动

                    

          来源:仿真社
          二次开发
          著作权归作者所有,欢迎分享,未经许可,不得转载
          首次发布时间:2023-02-13
          最近编辑:1年前
          仿真社
          硕士 ABAQUS/LS-DYNA探索者
          获赞 199粉丝 480文章 87课程 5
          点赞
          收藏
          作者推荐
          未登录
          还没有评论
          课程
          培训
          服务
          行家
          VIP会员 学习 福利任务 兑换礼品
          下载APP
          联系我们
          帮助与反馈