Search code examples
python-3.xttkwidgets

How do I disable/enable the ScaleEntry from ttkwidgets?


I tried to change the state of the ScaleEntry widget from the ttkwidgets package. Changing the state of the entry was easy enough.

scaleEntry.config_entry(state='disabled')
scaleEntry.config_entry(state='!disabled')

However, the scale part seems to be working differently. I tried configuring the same way as the entry scaleEntry.config_scale(state='disabled') and also tried through the configure method. It seems like in both cases, the "state" option is unknown. Even though the scale version from tkinter can be disabled in a specific way, the scale from ttkwidgets does not seem to work the same way.


Solution

  • Update:

    Based on your comment below you are looking at widgets from the ttkwidgets library.

    Based on this I did some digging on both their docs site and in the code itself and found there is no defined method to disable the widget.

    Based on this section of code:

    enter image description here

    I am able to print out all possible arguments for the ScaleEntry by using .keys().

    Example code:

    from ttkwidgets import ScaleEntry
    import tkinter as tk
    
    
    window = tk.Tk()
    scaleentry = ScaleEntry(window, scalewidth=200, entrywidth=3, from_=0, to=20)
    scaleentry.config_entry(justify='center')
    print(scaleentry.keys())
    scaleentry.pack()
    window.mainloop()
    

    Key results:

    ['borderwidth', 'class', 'compound', 'cursor', 'entryscalepad', 'entrywidth', 'from', 'height', 'orient', 'padding', 'relief', 'scalewidth', 'style', 'takefocus', 'to', 'width']
    

    From the list and scanning the code for ScaleEntry no such argument exist to disable this scale. So I almost came to the conclusion that it was not possible. However after reading into the code that makes up the ScaleEntry class I found this line:

    enter image description here

    I realize we can still disable it by targeting the internal class attribute _scale for the win! This is because at the end of the day the ScaleEntry widget is simply a tk Frame that has 2 class attributes. A ttk.Scale and a ttk.Entry.

    Example:

    from ttkwidgets import ScaleEntry
    import tkinter as tk
    
    
    window = tk.Tk()
    state = True
    scaleentry = ScaleEntry(window, scalewidth=200, entrywidth=3, from_=0, to=20)
    scaleentry.config_entry(justify='center')
    print(scaleentry.keys())
    scaleentry.pack()
    
    
    def toggle_state():
        global state
        if state:
            scaleentry._scale.state(['disabled'])
            scaleentry._entry.state(['disabled'])
            state = False
        else:
            scaleentry._scale.state(['!disabled'])
            scaleentry._entry.state(['!disabled'])
            state = True
    
    
    tk.Button(window, text='Toggle Scale', command=toggle_state).pack()
    window.mainloop()
    

    Example before toggle:

    enter image description here

    Example after toggle:

    enter image description here