Python Forum
How to change a tkinter label with scale and how to save that new value for later?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change a tkinter label with scale and how to save that new value for later?
#4
I would make a new widget that combines the scale widget with the label. Something like this:
import tkinter as tk
 
 
class Slider(tk.Frame):
    """A Scale widget with a label that displays the power range of the slider. Also converts slider to PWM."""
  
    default_table = ((1, "Off"), (51, "Low Speed"), (76, "High Speed"), (0, "Maximum Speed"))

    def __init__(self, parent, title, width=200, command=None, table=None, rmin=1100, rmax=1900):
        super().__init__(parent, width=width)
        self.command = command
        self.table = table or default_table
        self._rmin = rmin
        self._rmax = rmax
        self._title = tk.Label(self, text=title)
        self._label = tk.Label(self, text=self.table[0][1])
        self._thrust = tk.DoubleVar(self, 0)
        self._slider = tk.Scale(
            self,
            variable=self._thrust,
            orient=tk.HORIZONTAL,
            from_=0,
            to=100,
            command=self._slider_changed,
            length=width,
        )
        self._title.grid(row=0, column=0, sticky="W")
        self._label.grid(row=0, column=1, sticky="E")
        self._slider.grid(row=1, column=0, columnspan=2, sticky="EW")
 
    def title(self):
        """Return title str."""
        return self._title["text"]

    def label(self):
        """Return label str."""
        return self._label["text"]

    @property
    def thrust(self):
        """Return thrust value for label."""
        return self._thrust.get()
 
    @thrust.setter
    def thrust(self, new_value):
        """Set thrust value."""
        self._thrust.set(new_value)
 
    def pwm(self):
        """Return thrust setting as PWM"""
        return int(self._rmin + self._rmax - self._rmin * self.thrust / 100)
 
    def _slider_changed(self, _):
        """Slider moved.  Update label and call callback."""
        thrust = self._thrust.get()
        for level, new_text in self.table:
            if thrust < level:
                break
        self._label["text"] = new_text
        if self.command is not None:
            self.command(self.pwm())
 
    def __str__(self):
        return f"<Slider {self.title()}, slider = {self.thrust}, power = {self.label()}, pwm = {self.pwm()}"
 
 
class MainWindow(tk.Tk):
    """Demonstrate making sliders."""
 
    def __init__(self):
        super().__init__()
        for name in ("light", "forward", "reverse", "ascend", "descend"):
            slider = Slider(self, name)
            slider.command = lambda x, y=slider: print(y)
            slider.pack(side=tk.TOP, padx=10, pady=10)
 
 
MainWindow().mainloop()
Reply


Messages In This Thread
RE: How to change a tkinter label with scale and how to save that new value for later? - by deanhystad - Jun-28-2024, 01:35 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to save to multiple locations during save cubangt 1 677 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  how to open a popup window in tkinter with entry,label and button lunacy90 1 1,125 Sep-01-2023, 12:07 AM
Last Post: lunacy90
  change directory of save of python files akbarza 3 1,172 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  How to properly scale text in postscript conversion to pdf? philipbergwerf 3 1,321 Nov-07-2022, 01:30 PM
Last Post: philipbergwerf
  KeyError: 0 when trying to dedupe and match records at scale Catalytic 1 2,359 Apr-07-2022, 06:34 PM
Last Post: deanhystad
  Matplotlib scale julienhofmann 0 1,912 Apr-04-2021, 08:50 AM
Last Post: julienhofmann
  OOP Label In Tkinter Harshil 2 2,036 Aug-21-2020, 07:14 PM
Last Post: bowlofred
  Not able to update y2 axis scale in plotly ankitawadhwa 0 2,034 Jan-27-2020, 06:44 PM
Last Post: ankitawadhwa
  Change the scale on a Matplotlib Interactive Graph khatharsis 0 2,986 Oct-13-2019, 06:14 AM
Last Post: khatharsis
  How to manually define color bar scale in seaborn heatmap SriRajesh 3 18,736 Sep-08-2019, 11:12 AM
Last Post: RudraMohan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020