Python Forum
tkinter slider question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter slider question
#1
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!!
Reply
#2
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  neeed to activate a custom tk slider janeik 1 776 Oct-09-2023, 09:29 AM
Last Post: deanhystad
  Beginner question re: Tkinter geometry return2sender 3 906 Jun-19-2023, 06:19 PM
Last Post: deanhystad
  Tkinter GUI question texan1836 3 1,825 Apr-13-2023, 03:12 AM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 3,305 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] question for a tkinter dialog box RobertAlvarez424 2 2,205 Aug-25-2021, 03:08 PM
Last Post: RobertAlvarez424
  Python tkinter question tablet Nick_tkinter 8 4,930 Mar-04-2021, 10:44 PM
Last Post: Larz60+
  [Tkinter] Noob question:Using pyttsx3 with tkinter causes program to stop and close, any ideas? Osman_P 4 5,233 Nov-14-2020, 10:51 AM
Last Post: Osman_P
  question on tkinter canvas PhotoImage gr3yali3n 1 2,106 Sep-05-2020, 12:18 PM
Last Post: Larz60+
  Tkinter parameter question DPaul 2 2,022 Mar-14-2020, 09:35 AM
Last Post: DPaul
  Newbie question with Tkinter Entry mariolopes 2 2,201 Oct-12-2019, 11:02 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