2

I have the following statement on my code to create a slider:

[sg.Text('SPI Frequency [MHz]: '),sg.Slider((0.50,2.50),1.250,0.750,size=(80,15),orientation='h',key='FREQ_SLIDER',enable_events=True,tick_interval=0.75)]

However, my final resolution is not of 0.75 but it is rounded. Instead, I have the following slider: Slider

Why I can't get a resolution of 0.75 on my slider?

How I could do it?

Thanks Jorge

I would like to have a slider with a resolution of 0.75 for each tick

  • Have you tried changing the resolution parameter: https://stackoverflow.com/a/57511884/5574063 – af3ld Nov 09 '22 at 16:53

3 Answers3

2

digits

An integer specifying how many significant digits should be retained when converting the value of the scale to a string. If the number is less than or equal to zero, then the scale picks the smallest value that guarantees that every possible slider position prints as a different string.

There's no option digits in sg.Slider now.

The range (0.50, 2.50) is not exactly to fit the default value 1.250 and resolution 0.75, maybe replaced by (-0.25, 2.75) which will have the values [-0.25, 0.50, 1.25, 2.00, 2.75]

So the code will be like this, although the numerical ticks rounded, but the range for the Slider is correct.

import PySimpleGUI as sg

layout = [
    [sg.Slider(
        range=(-0.25, 2.75),                    # Revised from (0.50,2.50)
        default_value=1.25,
        resolution=0.75,
        tick_interval=0.75,
        size=(40,15),
        orientation='horizontal',
        key='Slider'),
    ],
]
window = sg.Window('Title', layout, finalize=True)
window['Slider'].widget.configure(digits=3)
window.read(close=True)

enter image description here

Jason Yang
  • 11,284
  • 2
  • 9
  • 23
1

I cannot comment to say so, but there is not enough information to give a specific answer, only general. It depends on whether you are using Tk, Qt, or Wx. You can find the source code for each here: https://github.com/PySimpleGUI/PySimpleGUI, where you can find the Slider types and what class they use in the backend library. Use this information to lookup that backend library and how they draw the scaling. My guess, you will need to hack it like another question suggested here: Custom QDial notch ticks with PyQt. If I were you, try using a different backend to see what results you get. Otherwise, define your own slider class using the code from the repo but that might only get you so far and require a lot of work. Perhaps instead of 0.75, try scaling everything up by the lowest common denominator, or just use 750-2750 kHz instead of MHz as it's obviously displaying only one digit past the .!

MrMattBusby
  • 116
  • 5
0

After reading through the PySimpleGui, I found out that they refer to Tkinter scale. So, I went to test in Tkinter scale.

tkscale = element.Widget = tk.Scale(tk_row_frame,orient=element.Orientation,variable=element.TKIntVar,from_=range_from, to_=range_to,resolution=element.Resolution,length=slider_length, width=slider_width,bd=element.BorderWidth, relief=element.Relief, font=font,tickinterval=element.TickInterval)

I went to test in Tkinter.Scale, and it seems that Tkinter.Scale only accept the first positive value to be 0 + resolution, so I believe it is impossible to start your first value with 0.5, unless you directly configure with the source code.

You can try it with the following code.

import tkinter as Tk

root = Tk.Tk()

var = Tk.DoubleVar()
scale = Tk.Scale( root, from_=-0.75,to_=2.75, resolution = 0.75, digit = 3, tickinterval = 0.75, orient = Tk.HORIZONTAL )
scale.grid(column=0, row=0, columnspan=3)

root.mainloop()
Joshua
  • 144
  • 7