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
  [Tkinter] question for a tkinter dialog box RobertAlvarez424 2 188 Aug-25-2021, 03:08 PM
Last Post: RobertAlvarez424
  Python tkinter question tablet Nick_tkinter 8 1,370 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 2,184 Nov-14-2020, 10:51 AM
Last Post: Osman_P
  question on tkinter canvas PhotoImage gr3yali3n 1 694 Sep-05-2020, 12:18 PM
Last Post: Larz60+
  Tkinter parameter question DPaul 2 797 Mar-14-2020, 09:35 AM
Last Post: DPaul
  Newbie question with Tkinter Entry mariolopes 2 1,006 Oct-12-2019, 11:02 PM
Last Post: Larz60+
  Help on drawing a shape and slider mnh001 12 2,597 Aug-22-2019, 11:21 PM
Last Post: mnh001
  TKinter Slider widget Jemeronimo 1 2,462 Jan-29-2019, 05:32 PM
Last Post: Larz60+
  Making a Matplotlib in Tkinter using a slider Jemeronimo 1 3,314 Dec-05-2018, 08:06 AM
Last Post: Gribouillis
  tkinter filedialog and pickle - custom icon question. kim07133 0 1,588 Jan-08-2018, 12:10 PM
Last Post: kim07133

Forum Jump:

User Panel Messages

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