Python Forum
tkinter slider question - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: tkinter slider question (/thread-32616.html)



tkinter slider question - Nick_tkinter - Feb-21-2021

Hello guys.I have a question about a tutorial I saw and I don't understand something.
So, I have the following code that has 2 sliders.
from tkinter import *

root = Tk()
root.geometry("800x700")

def vertical_num(var):
    my_label = Label(root , text = vertical_slider.get() )
    my_label.place(x=650,y=100)
    
def horizontal_num(var):
    my_label = Label(root , text = horizontal_slider.get() )
    my_label.place(x=100,y=100)
    

vertical_slider = Scale(root , from_ = 0 , to = 400 , command = vertical_num)
vertical_slider.pack()

horizontal_slider = Scale(root , from_ = 0 , to = 400 , orient = HORIZONTAL , command = horizontal_num)
horizontal_slider.pack()


root.mainloop()
I want to get the values of sliders in real time (when I change the position of sliders of course) and I don't understand the point of passing an argument in these two functions I have.Without these arguments, I receive errors.

I don't use them inside functions.
So, what's the point of passing arguments in these 2 functions.
It's weird.

Thanks a lot!!


RE: tkinter slider question - deanhystad - Feb-22-2021

The slider passes the slider value when it calls the function. If there is no argument in the function Python raises a TypeError exception. You discovered this very quickly when you removed the argument and ran the code. After finding out the code raises a TypeError if there is no argument in the function you could modify the function to print the function argument and you would have seen that it is the slider value being passed.

When you have a question you should experiment around a bit before asking questions on the forum. You will learn faster.

Here's your code using the argument to get the slider value.
from tkinter import *
  
def vertical_num(value):
    vertical_label['text'] = str(value)
     
def horizontal_num(value):
    horizontal_label['text'] = str(value)
     
 
root = Tk()

vertical_slider = Scale(root, from_=0, to=400, command=vertical_num)
vertical_slider.pack()
vertical_label = Label(root, text='0', width=4)
vertical_label.pack()
 
horizontal_slider = Scale(root, from_ = 0, to = 400, orient=HORIZONTAL, command=horizontal_num)
horizontal_slider.pack()
horizontal_label = Label(root, text='0', width=4)
horizontal_label.pack()

root.mainloop()
The functions should not create labels. There should be labels and the function should change the label text.

The two functions in the above example are nearly identical. So I made them one function that works for both slider/labels.
from tkinter import *
  
def update_label(label, value):
    label['text'] = str(value)

root = Tk()

v_lbl = Label(root , text='0', width=4)
h_lbl = Label(root , text='0', width=4)
v_slider = Scale(root, from_=0, to=400, command=lambda x, lbl=v_lbl: update_label(lbl, x))
h_slider = Scale(root, from_=0, to=400, orient=HORIZONTAL,
                 command=lambda x, lbl=h_lbl: update_label(lbl, x))

v_slider.pack()
v_lbl.pack()
h_slider.pack()
h_lbl.pack()

root.mainloop()