是否可以更改笔记本标签的宽度? [英] Is it possible to change width of notebook tab?

查看:41
本文介绍了是否可以更改笔记本标签的宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我想知道是否可以更改笔记本小部件选项卡的宽度?

As the title says, I was wondering if it's possible to change the width of a notebook widget tab?

我想要做的是确保选项卡的大小相等并填充窗口的整个宽度,即使有人调整它的大小也是如此.目前,它看起来像这样:

What I'm trying to do is to make sure that the tabs are of equal size and fill the entire width of the window even when someone resizes it. Currently, it appears like so:

但是想要这样:

[我通过在标签名称前后添加空格来做到这一点]

[I did this one by adding spaces before and after the tab name]

第一个的相关代码是:

ttk::frame $fr.note.tab1
$fr.note add $fr.note.tab1 -text "Tab1" ;# For the second, it's "     Tab1     "

ttk::frame $fr.note.tab2
$fr.note add $fr.note.tab2 -text "Tab2"

ttk::frame $fr.note.tab3
$fr.note add $fr.note.tab3 -text "Tab3"

我试图查看 手册 但它似乎只有小部件本身可以改变其大小(选项卡下方的区域).

I tried to look in the manual but it seems that only the widget itself can have its size altered (the area below the tab).

有没有办法指定选项卡的宽度或解决方法?我试过了:

Is there perhaps a way to specify the width of the tab, or a workaround? I tried:

$fr.note add $fr.note.tab1 -text "Tab1" -width 90

当然,这会抛出一个错误,指出选项 -width 是一个未知选项(并且选项卡似乎无法在没有 configure 命令的情况下进行配置).

But of course, this threw an error saying that the option -width was an unknown option (and tab doesn't appear to be configurable with no configure command).

推荐答案

今天在classicTheme文件lib/tk8.6/ttk文件夹中窥探了一下源码,发现可以用这个来调整notebook的大小标签:

Today after some snooping about the source in the folder lib/tk8.6/ttk in the file classicTheme, I found that one can use this to resize the notebook tabs:

ttk::style configure TNotebook.Tab -width 20

示例:

package require Tk  ;# 8.6.4
ttk::notebook .note

set note .note
ttk::frame $note.tab1
$note add $note.tab1 -text "Tab 1"
ttk::frame $note.tab2
$note add $note.tab2 -text "Tab 2"
ttk::frame .note.tab3
$note add $note.tab3 -text "Tab 3"

pack $note -fill both -expand 1

set nw [winfo width .note]
set tc [llength [winfo children .note]]

ttk::style configure TNotebook.Tab -width [expr {$nw/6/$tc}]
ttk::style configure TNotebook.Tab -anchor center   ;# To center tab label

以上给出:

然而,所有笔记都将继承相同的配置,但如果窗口太小,选项卡将均匀调整大小:

All the notes will inherit the same configurations however, but in case a window is too small, the tabs will evenly resize:

您可能注意到我在设置选项卡宽度时除以 6,这是我在 winfo width-width 给出的值之间发现的因子差异,当配置标签(直接使用 $nw/$tc 否则会产生巨大的标签宽度).

You might notice I divided by 6 when setting the width of the tab, that was the factor difference I found between the value given by winfo width and -width when configuring the tab (directly using $nw/$tc would otherwise give huge tab widths).

另外,您还可以将注释绑定到 以允许选项卡在窗口大小发生变化时自动调整大小以适应窗口:

As an extra, one can also bind the note to <Configure> to allow the tabs to automatically resize to fit the window if the window size changes:

bind .note <Configure> {
  set nw [winfo width .note]
  set tc [llength [winfo children .note]]
  ttk::style configure TNotebook.Tab -width [expr {$nw/6/$tc}]
}

我不完全确定这是否可以被视为好的做法,它对我有用^^

I'm not entirely sure whether this could be considered good practice or not, it works for me ^^

这篇关于是否可以更改笔记本标签的宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆