RobertJN64 / TKinterModernThemes

A collection of modern themes with code that makes it easy to integrate into a tkinter project.
MIT License
91 stars 7 forks source link

Float type in IntVar #9

Closed OlegXio closed 2 months ago

OlegXio commented 2 months ago

Screenshot

self.password_length = tk.IntVar(value=25)
self.Scale(100, 0, self.password_length, row=1, col=0).grid(row=2, column=0, sticky='w')
self.pswlenght = ttk.Label(self.master, textvariable=self.password_length, width=50).grid(row=2, column=1)

In the above code snippet, you can see that a variable of Int type is set for the slider, but when we try to print it, we get a value of float type. We could set text=int(password_lenght.get()) instead of textvariable for ttk.Label, but this is not an updatable function. It could be updated if Scale had the ability to set the attribute command=updatescale_handler.

When trying to use StringVar, DoubleVar instead of IntVar the identical situation occurs (despite the fact that the Scale widget should accept only IntVar or DoubleVar in variable:


WidgetFrame.py:971 :
def Scale(self, lower: float, upper: float, variable: Union[tk.IntVar, tk.DoubleVar] <....>)
RobertJN64 commented 2 months ago

Sadly, the ttk scale widget does not have the same customization as the tk version. However, we can work around this using a function like you suggest, here is an example:

import TKinterModernThemes as TKMT
import tkinter as tk

class App(TKMT.ThemedTKinterFrame):
    def __init__(self, theme, mode, usecommandlineargs=True, usethemeconfigfile=True):
        super().__init__("Image", theme, mode, usecommandlineargs=usecommandlineargs,
                         useconfigfile=usethemeconfigfile)

        self.password_length = tk.IntVar(value=25)
        self.output = tk.StringVar(value="25")

        self.frame = self.addLabelFrame("Frame")
        self.frame.Scale(0, 100, self.password_length, widgetkwargs={"command": self.handle_update})
        self.pswlenght = self.frame.Text("", widgetkwargs={"textvariable":self.output})

        self.run()

    def handle_update(self, val):
        self.output.set(str(round(float(val))))

if __name__ == "__main__":
    App("park", "dark")