SIMEL
SIMEL

Reputation: 8939

Is it possible to put a pattern inside a label with Tk/Tcl?

I want a label that will contain a template of a color, and not just a solid color.

What I mean is that the label will have as its background a template of a color and not just a solid color, for example, a pattern of diagonal lines in an alternating colors, like the option for a pattern fill in MS Office:

enter image description here

I don't want it to be a picture of the pattern I want, because I want it to re-size as the widget re-sizes and work with labels of different length and size.

Is there a way to do the same with tk/tcl?

Upvotes: 1

Views: 254

Answers (2)

Ergwun
Ergwun

Reputation: 12978

A pattern will require an image, but labels can only have an image or text, not both.

If you just want to have a pattern, then you can create an image for it procedurally by duplicating a tile image like this:

# 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

If you want to have text over a pattern, you could use a canvas. You can see how to tile an image in a canvas (taking canvas resizing into account) on the Tclers Wiki.

Upvotes: 1

patthoyts
patthoyts

Reputation: 33203

Labels can have either images or text strings. That is it. However, you can control the image all you want in response to UI events like Map or Unmap (show or hide) or on resize. So I suggest you generate the images as required and re-generate them on resizes.

Upvotes: 1

Related Questions