简体   繁体   English

有没有办法在 PySimpleGui spinboxes 中限定 integer 输入范围的最小值和最大值,以便最小值不会超过最大值?

[英]Is there any way to delimit minimum and maximum of an integer input range in PySimpleGui spinboxes, so that minimum doesn't surpases maximum?

My objective is to input both a minimum value, and a maximum value, in a window in PySimpleGui using spinboxes.我的目标是使用旋转框在 PySimpleGui 的 window 中输入最小值和最大值。 But the idea is also to limit the range of the spinboxes according to each other, so that the minimum value doesn't surpass the maximum value;但想法也是根据彼此限制旋转框的范围,使最小值不超过最大值; ex.前任。 if i input 50 in the upper spinbox (minimum) the lower one (maximum) should not be able to take a number below 50, or also similarly in reverse, if i input 60 in the lower spinbox (maximum), the upper one (minimum) should only accept a number below 60.如果我在上旋转框中输入 50(最小),下旋转框(最大)不应该能够取低于 50 的数字,或者同样反过来,如果我在下旋转框(最大)中输入 60,则上旋转框( minimum) 应该只接受低于 60 的数字。

So far i have done this for the code for both spinboxes (setValues is a set with integers defined previously on the code).到目前为止,我已经为两个 spinboxes 的代码完成了此操作(setValues 是一个集合,其中包含先前在代码中定义的整数)。

minValues = int(min(setValues))
maxValues = int(max(setValues))
print(minValues, maxValues)

layout = [[sg.Spin([i for i in range(minValues,maxValues+1)], initial_value=minValues), sg.Text('Minimum value')],
        [sg.Spin([i for i in range(minValues,maxValues+1)], initial_value=maxValues), sg.Text('Maximum value')],
        [sg.Submit(), sg.Cancel()]]
window = sg.Window('Window Title', layout)

While preferably i would like a solution in PySimpleGui, an answer suggested in other GUI libraries would also be welcome.虽然我最好希望在 PySimpleGui 中有一个解决方案,但也欢迎其他 GUI 库中建议的答案。

Update option values of another sg.Spin when event of this sg.Spin .当此sg.Spin事件发生时,更新另一个sg.Spin的选项values

Demo code as following,演示代码如下,

在此处输入图像描述

import PySimpleGUI as sg

sg.theme("DarkBlue")
sg.set_options(font=("Courier New", 16))

minValue = 5
maxValue = 20

layout = [
    [sg.Spin([i for i in range(minValue, maxValue)],     initial_value=minValue, size=(2, 1), enable_events=True, key='-MIN-'), sg.Text('Minimum value')],
    [sg.Spin([i for i in range(minValue+1, maxValue+1)], initial_value=maxValue, size=(2, 1), enable_events=True, key='-MAX-'), sg.Text('Maximum value')],
    [sg.Submit(), sg.Cancel()],
]

window = sg.Window('Title', layout, finalize=True)

value_min, value_max = minValue, maxValue

while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    elif event == '-MAX-':
        value_max = values[event]
        window['-MIN-'].update(values=[i for i in range(minValue, value_max)])
    elif event == '-MIN-':
        value_min = values[event]
        window['-MAX-'].update(values=[i for i in range(value_min+1, maxValue+1)])
    print(event, values)

window.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM