首页 > 解决方案 > PySimpleGUI 滑块是否有小数范围?

问题描述

我需要一些方法来在 PySimpleGUI 中的滑块上方显示一个十进制值。

我已经尝试过输入十进制值,但是当我这样做时会引发错误。

import PySimpleGUI as sg

layout = [sg.Slider(range=(850,999), default_value=997, size=(40,15), orientation='horizontal')]

我希望它在滑动范围时显示十进制值,但现在它所做的只是将其设置为 850 或 999,而不是 85.0 和 99.9。

标签: sliderpysimplegui

解决方案


当然,您只需将范围定义为 85 到 99 并将分辨率设置为 0.1

layout = [[sg.Slider(range=(85.0,99.9), default_value=99.7, resolution=.1, size=(40,15), orientation='horizontal')]]

此处的文档中对此进行了讨论: https ://pysimplegui.readthedocs.io/en/latest/#slider-element

IDE 中显示的文档字符串还显示范围可以是浮点数,并将其他值也列为可能是浮点数。如果它们没有出现在您的 IDE 中,请使用 Python 的帮助系统

python
Python 3.6.2 |Anaconda, Inc.| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import PySimpleGUI as sg
>>> help(sg.Slider)
Help on class Slider in module PySimpleGUI:

class Slider(Element)
 |  A slider, horizontal or vertical
 |
 |  Method resolution order:
 |      Slider
 |      Element
 |      builtins.object
 |
 |  Methods defined here:
 |

... removed a couple of methods to show just the init method

 |
 |  __init__(self, range=(None, None), default_value=None, resolution=None, tick_interval=None, orientation=None, disable_number_display=False, border_width=None, relief=None, change_submits=False, enable_events=False, disabled=False, size=(None, None), font=None, background_color=None, text_color=None, key=None, pad=None, tooltip=None, visible=True)
 |      :param range: Union[Tuple[int, int], Tuple[float, float]] slider's range (min value, max value)
 |      :param default_value: Union[int, float] starting value for the slider
 |      :param resolution: Union[int, float] the smallest amount the slider can be moved
 |      :param tick_interval: Union[int, float] how often a visible tick should be shown next to slider
 |      :param orientation: (str) 'horizontal' or 'vertical' ('h' or 'v' also work)
 |      :param disable_number_display: (bool) if True no number will be displayed by the Slider Element
 |      :param border_width: (int) width of border around element in pixels

推荐阅读