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

71 Views Asked by At

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.

3

There are 3 best solutions below

1
acw1668 On BEST ANSWER

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

0
Bahman Haghighi On

If you want to customize the display of the Tkinter Scale widget to show percent values from 100 to 0 and back to 100, you can use the from_ and to options. Here's an example code for your Python3 program:

import tkinter as tk

def scale_changed(value):
    # Do something with the scaled value
    print(f"Scaled Value: {value}%")

# Create the main window
root = tk.Tk()
root.title("Percent Scale")

# Create a scale widget
scale = tk.Scale(root, from_=100, to=0, orient=tk.HORIZONTAL, command=scale_changed)
scale.pack(pady=20)

# Start the Tkinter event loop
root.mainloop()

In this code, the from_ option is set to 100, and the to option is set to 0. This will create a Scale widget that goes from 100 to 0 horizontally. The scale_changed function is called whenever the scale value changes, and you can modify it to suit your needs.

Feel free to adjust the code as necessary for your specific requirements.

5
Obaskly On

you can directly handle the transformation within the lambda function attached to the Scale widget's command parameter. The scale will still technically run from 0 to 200, but the value will be transformed on the fly to the desired range (100 to 0 to 100) whenever the slider is moved.

import tkinter as tk

root = tk.Tk()
root.title("Custom Scale")

# Create the scale and handle value transformation in a lambda function
scale = tk.Scale(root, from_=0, to=200, orient="horizontal", length=300, sliderlength=10,
                 command=lambda value: print(100 - abs(100 - int(value))))

scale.set(100)  # Initialize at the middle position
scale.pack()

root.mainloop()