【问题标题】:Is it possible to put a pattern inside a label with Tk/Tcl?是否可以在带有 Tk/Tcl 的标签内放置模式?
【发布时间】:2013-01-10 11:03:48
【问题描述】:

我想要一个包含颜色模板的标签,而不仅仅是纯色。

我的意思是标签将有一个颜色模板作为其背景,而不仅仅是纯色,例如,交替颜色的对角线图案,就像 MS Office 中的图案填充选项:

我不希望它是我想要的图案的图片,因为我希望它随着小部件重新调整大小并使用不同长度和大小的标签来重新调整大小。

有没有办法对 tk/tcl 做同样的事情?

【问题讨论】:

    标签: tcl tk


    【解决方案1】:

    图案需要一张图片,但标签只能有图片或文字,不能同时有。

    如果您只是想要一个图案,那么您可以通过复制这样的平铺图像以程序方式为其创建图像:

    # Set foreground and background colours for your tile
    set fg "#9999ff"
    set bg "#ffffff"
    
    # create tile data (this will give vertical stripes every 4 pixels)
    set data "$fg $bg $bg $bg"
    
    # Create the tile image
    image create photo tile
    tile put [list $data]
    
    # Create the actual image (tile set)
    image create photo tileset
    
    # Fill the image with the tile to the desired size
    tileset copy tile -to 0 0 200 300
    
    # Display the tiled image in a label
    label .l -image tileset
    pack .l
    

    如果您想在图案上放置文本,您可以使用画布。 您可以在 the Tclers Wiki 上查看如何在画布中平铺图像(考虑调整画布大小)。

    【讨论】:

      【解决方案2】:

      标签可以包含图像或文本字符串。这就对了。但是,您可以根据需要控制图像以响应 Map 或 Unmap(显示或隐藏)等 UI 事件或调整大小。所以我建议您根据需要生成图像并在调整大小时重新生成它们。

      【讨论】:

      • <Configure> 事件是在小部件相对于其父级调整大小或重新定位时发送的事件。 (理论上,它可以为某些其他事情发送,但 Tk 不会触及它们。)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多