1

I'm trying to apply a style to a slider widget in tkinter. However, the changes to slider_style parameters on the code below don't affect my_slider. Specifically, I'm trying to change the trough width to something smaller. I have seen on different tutorials that either the width or the sliderthickness parameters might do the trick, but I had no luck with either (or with any other style parameter, for that matter). Here is the relevant code:

from tkinter import *
import tkinter.ttk as ttk

window_width = 1000
window_height = 100
root = Tk()
root.title('Measure Marker')
root.geometry(f'{window_width}x{window_height}')

slider_style = ttk.Style()
slider_style.configure('SliderStyle.Horizontal.TScale', width=5, sliderthickness =5)
my_slider = ttk.Scale(root, from_=0, to=100, length=900, style='SliderStyle.Horizontal.TScale')
my_slider.pack(pady=30)

root.mainloop()

Am I applying SliderStyle correctly? Is that the way to go to change the trough width?

j_4321
  • 15,431
  • 3
  • 34
  • 61

1 Answers1

0

I tried to do this a while ago, this works I literally just tested it,

from tkinter import *

def sel():
   selection = "Value = " + str(var.get())
   label.config(text = selection)

window_width = 1000
window_height = 100
root = Tk()
root.geometry(f'{window_width}x{window_height}')

var = DoubleVar()
scale = Scale(root, width=5, orient=HORIZONTAL, variable = var )
scale.pack(anchor=CENTER)

button = Button(root, text="Get Scale Value", command=sel)
button.pack(anchor=CENTER)

label = Label(root)
label.pack()

root.mainloop()


  • You know that your code can be simplified to `import tkinter as tk; root = tk.Tk(); scale = tk.Scale(root, width=5, orient="horizontal"); scale.pack(); root.mainloop()` – TheLizzard May 18 '21 at 23:41
  • yeah, i know, just grabbed it from something else i was trying to do a while back. Doesn't hurt to add the value of the slider – WirelessTaco May 18 '21 at 23:46
  • On my Windows 10, it automatically displays the value on top of the slider. So I removed the label, button and doublevar – TheLizzard May 18 '21 at 23:47
  • my bad lol, not using windows, good job tho – WirelessTaco May 18 '21 at 23:49
  • Thanks @WirelessTaco. Chaging the `width` parameter affects the trough height, however, which is not what I'm looking for. It would change the actual width if `orient=VERTICAL`, but using vertical sliders is not an option in my case. – Felipe Martins May 19 '21 at 11:37
  • maybe check it this tutorial https://www.tutorialspoint.com/python/tk_scale.htm – WirelessTaco May 19 '21 at 23:57
  • @WirelessTaco I think I went trough that one already. It too says to use the `width` property of `Scale`, but I wasn't able to set it when creating the slider. It simply changes nothing. That's why I was trying the `style` options. – Felipe Martins May 20 '21 at 16:44
  • so which part of the scale are you trying to change? – WirelessTaco May 21 '21 at 07:57