Python Forum
neeed to activate a custom tk slider
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
neeed to activate a custom tk slider
#1
hello.
At present time,I need to mouse click on the slider1 manually to activate it (to change transparency of a top level window.
How do I activate it programmatically?
I have tried to code slider1.set(0.0) but that might be just the position of slider1?
Visualy its positioned at 0.0 as result, but its not activated.I have also tried to set focus before setting value.
Whats needed here to activate it?

#ยจ--function to set transparency in top level window
def slider1_event(value):
    top.attributes("-alpha",slider1.get())
    slide1val.configure(text=str(round(value,2)))

#-- set characteristics and command function
slider1 = customtkinter.CTkSlider(window, fg_color="red", from_=0.0, to=1.0, number_of_steps=20, command=slider1_event)
slider1.place(x=1590,y=40)

#-- trying desperate to activate slider1:
slider1.focus_set()
#slider1.set(0.5)
#slider1.set(0.0)

#---label to show value of slider1---------
slide1val= customtkinter.CTkLabel(window, width=50,font=("Ariel",16,"bold"),text_color="#000000",fg_color="#f0f0f0", text="", justify=("center"))
slide1val.place(x=1800,y=35)
Reply
#2
Controls in tkinter (and customtkinter) do not call the command callback when the value is set programmatically. This avoids the vicious cycle of the callback doing something to tweak the control display which results in the command being callled which ...

You can have the callback set the control like this:
import customtkinter as ctk

class Window(ctk.CTk):
    def __init__(self):
        super().__init__()
        self.s1 = ctk.CTkSlider(self, number_of_steps=20, width=200, command=self.moved)
        self.s2 = ctk.CTkSlider(self, number_of_steps=20, width=200, command=self.moved)
        self.s1.pack(padx=50, pady=50)
        self.s2.pack(padx=50, pady=50)

    def moved(self, value):
        self.s1.set(value)
        self.s2.set(value)
 
Window().mainloop()
If you wanted to set the value programmatically and call the callback function, you would just call the callback function directly.

Or you could not use the command attribute. Create a tkinter Variable and use trace() to bind the callback to the variable.
import customtkinter as ctk
import tkinter as tk
import random


class Window(ctk.CTk):
    def __init__(self):
        super().__init__()
        self.slider = tk.DoubleVar()
        self.slider.trace("w", self.moved)
        ctk.CTkSlider(self, width=200, variable=self.slider).pack(padx=50, pady=50)
        ctk.CTkSlider(self, width=200, variable=self.slider).pack(padx=50, pady=50)
        self.spooky()

    def moved(self, *_):
        print(self.slider.get())

    def spooky(self):
        self.slider.set(random.random())
        self.after(5000, self.spooky)


Window().mainloop()
To programmatically set the value and call the callback you set() the value. Now you have that vicious circle I mentioned earlier. If you need to modify the control display without activating the command you need to somehow de-activate the callback.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter slider question Nick_tkinter 1 2,530 Feb-22-2021, 01:31 PM
Last Post: deanhystad
  [Tkinter] how to activate Enter key in text box astral_travel 3 2,767 Oct-17-2020, 05:56 PM
Last Post: astral_travel
  [Tkinter] Tkinter custom widget styling and creating custom theme karolp 6 4,877 May-06-2020, 06:11 PM
Last Post: karolp
  Help on drawing a shape and slider mnh001 12 5,544 Aug-22-2019, 11:21 PM
Last Post: mnh001
  TKinter Slider widget Jemeronimo 1 3,900 Jan-29-2019, 05:32 PM
Last Post: Larz60+
  Making a Matplotlib in Tkinter using a slider Jemeronimo 1 5,695 Dec-05-2018, 08:06 AM
Last Post: Gribouillis
  [Tkinter] Resetting scale value back to 0 after the slider loses focus lwhistler 3 6,985 Mar-07-2017, 10:01 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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