0

Is it possible to make a Label with text in it fill up with for example green depending on a percentage? Like if the percentage is 50% the labels left half is filled with green?

I thought of making a Label with the color transparent and lay it over the original label, but it seems tkinter doesnt have transparent labels. You also cant seem to change the width of a label at runtime, which would also be necessary to adjust it when the percentage changes.

Is there any way to do what I described in tkinter? It seems not.

Elekam
  • 75
  • 1
  • 9
  • 1
    Why don't consider about `ttk.progressbar` directly?Do you want to put a text on a progressbar? – jizhihaoSAMA Jun 05 '20 at 12:04
  • 1
    You can try a progressbar with text mentioned [here](https://stackoverflow.com/questions/47896881/progressbar-with-percentage-label?noredirect=1&lq=1). – acw1668 Jun 05 '20 at 12:08
  • I didnt find this, thank you very much. I will try this. – Elekam Jun 05 '20 at 12:11
  • Under the right circumstances you can change the width of widgets at runtime, though that depends on how you've laid out the widget on the screen. – Bryan Oakley Jun 05 '20 at 17:02

1 Answers1

0

A simple trick is to use place to put a duplicate label on top of the original label. You can then use place options to control the size of the overlay.

The following example shows 60% progress (relwidth=.6). You can use place_configure to change the percentage at runtime to be any value you want.

label = tk.Label(root, text="Hello, world", borderwidth=0)
label.pack(padx=20, pady=20)

progress_label = tk.Label(label, text="Hello, world",
                          background="green", borderwidth=0, anchor="w")
progress_label.place(x=0, y=0, anchor="nw", relwidth=.6)

enter image description here

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685