pythontkinterslide

How to make a Tkinter Scale count from 0 to 100 to 0


I want to create a Python3 programm that contains a Tkinter Scale. And I want it to scale it in percent from 0(left) to 100 (middle) and back to 0 (on the right). How do I get rid of the '-' without breaking the code? Here's what I got so far:

Here's what I got so far:

#!/usr/bin/python3

import tkinter as tk
from tkinter import ttk
...
scale = tk.Scale(root, from_=-100, to=100, orient="horizontal", length=300, sliderlength=10)
...

it does nearly what I want, but I want to count from 0 to 100 and back to to 0.


Solution

  • If you want to show the current value above the slider from 0 to 100 to 0, you can set showvalue=0 and use another label to show the value you want:

    def show_value(value):
        value = int(value)
        x, y = scale.coords()
        value_label["text"] = 100 - abs(value)
        value_label.place(x=scale.winfo_x()+x, y=scale.winfo_y(), anchor="s")
    
    
    # label for showing the scale value
    value_label = tk.Label(root, font=("", 8))
    
    scale = tk.Scale(root, from_=-100, to=100, orient="horizontal", length=300, sliderlength=10,
                     showvalue=0, command=show_value)
    
    ...
    
    # show the initial value
    root.update()
    show_value(0)
    

    Note that the above solution just change the value displayed but the value returned by scale.get() is still from -100 to 100.

    Result:

    enter image description here

    enter image description here

    enter image description here