You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Tkinter Dialog窗口添加横向与纵向滚动条?

为Tk Dialog窗口添加横竖滚动条的方案

可以直接通过Tk的Canvas组件配合滚动条实现,下面是修改后的完整代码,替换你原代码中从set frame [frame [$dlg getframe].frame]开始的部分即可:

set dlg [Dialog .new -anchor n -place center -side right -cancel 6 -default 0 -parent . -modal local \
     -transient 0]
# place this button first because it is default value (-default 0)
$dlg add -text "check all" -underline 2 -command {run_simu_check_all}
$dlg add -text "uncheck all" -underline 2 -command {run_simu_uncheck_all}
# when "ok" is hit, windows is closed and tests are run (another windows will be open)
$dlg add -text "OK" -underline 2 -command {.new enddialog "";pexecute_simu_script $Cockpit::bf}

# this button is placed in last place because it is the choosen value (-cancel 5) if we cancel (esc key)
$dlg add -text "  Annuler  " -underline 2 -command {.new enddialog ""}

# ------------- 替换为带滚动条的容器 -------------
set main_frame [frame [$dlg getframe].main_frame]
pack $main_frame -fill both -expand 1

# 创建垂直滚动条
set v_scroll [scrollbar $main_frame.v_scroll -orient vertical]
pack $v_scroll -side right -fill y

# 创建水平滚动条
set h_scroll [scrollbar $main_frame.h_scroll -orient horizontal]
pack $h_scroll -side bottom -fill x

# 创建Canvas,用于承载内部frame
set canvas [canvas $main_frame.canvas \
    -yscrollcommand "$v_scroll set" \
    -xscrollcommand "$h_scroll set"]
pack $canvas -side left -fill both -expand 1

# 关联滚动条与Canvas的滚动命令
$v_scroll configure -command "$canvas yview"
$h_scroll configure -command "$canvas xview"

# 在Canvas中创建内部frame,用于放置所有复选框和标题
set inner_frame [frame $canvas.inner_frame]
set inner_frame_id [$canvas create window 0 0 -window $inner_frame -anchor nw]

# 绑定内部frame大小变化事件,更新Canvas的滚动区域
bind $inner_frame <Configure> {
    %W configure -width [winfo reqwidth %W]
    $canvas configure -scrollregion [list 0 0 [winfo reqwidth %W] [winfo reqheight %W]]
}

# 绑定Canvas的鼠标滚轮事件(兼容不同平台)
bind $canvas <MouseWheel> {
    $canvas yview scroll [expr {- (%D / 120)}] units
}
bind $canvas <Button-4> {
    $canvas yview scroll -1 units
}
bind $canvas <Button-5> {
    $canvas yview scroll 1 units
}

# 添加标题标签
set label [label $inner_frame.label]
pack $label -side top -anchor n -fill x -expand 1

# checkbox display for each scenario 
set liste_entite [liste_entite $Cockpit::bf fic_simu_modtb_comp]
foreach entite $liste_entite {
    if {$entite != "NONE"} {
        global ${entite}_var
        if {![info exists ${entite}_var]} {set ${entite}_var 0}
        # 移除固定-width,避免截断长文本
        pack [checkbutton $inner_frame.$entite \
                    -text "$entite" \
                    -font {arial 8} \
                    -justify left \
                    -variable ${entite}_var \
                    -command {} \
                   ] -side top -anchor w
    }
}
# ------------- 滚动条容器结束 -------------

.new configure -title "choose scenario to run"
[.new getframe].main_frame.canvas.inner_frame.label configure -text "select scenario to run"

wm iconbitmap .new $Cockpit::icon
.new draw

关键修改说明:

  • 用Canvas + 内部Frame的组合替代原有的单一Frame,Canvas负责滚动逻辑,内部Frame承载所有内容
  • 添加了垂直和水平滚动条,分别处理纵向列表过长和横向文本溢出的问题
  • 移除了复选框的-width 50属性,让文本可以完整显示,配合水平滚动条查看长场景名称
  • 绑定了鼠标滚轮事件,支持通过滚轮快速滚动列表
  • 自动关联内部Frame的大小变化,确保滚动区域能覆盖所有内容

内容的提问来源于stack exchange,提问作者iostrym

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 19:50:27