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


Messages In This Thread
tkinter slider question - by Nick_tkinter - Feb-21-2021, 10:05 PM
RE: tkinter slider question - by deanhystad - Feb-22-2021, 01:31 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  neeed to activate a custom tk slider janeik 1 845 Oct-09-2023, 09:29 AM
Last Post: deanhystad
  Beginner question re: Tkinter geometry return2sender 3 958 Jun-19-2023, 06:19 PM
Last Post: deanhystad
  Tkinter GUI question texan1836 3 1,920 Apr-13-2023, 03:12 AM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 3,461 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] question for a tkinter dialog box RobertAlvarez424 2 2,263 Aug-25-2021, 03:08 PM
Last Post: RobertAlvarez424
  Python tkinter question tablet Nick_tkinter 8 5,073 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,333 Nov-14-2020, 10:51 AM
Last Post: Osman_P
  question on tkinter canvas PhotoImage gr3yali3n 1 2,146 Sep-05-2020, 12:18 PM
Last Post: Larz60+
  Tkinter parameter question DPaul 2 2,089 Mar-14-2020, 09:35 AM
Last Post: DPaul
  Newbie question with Tkinter Entry mariolopes 2 2,262 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