设置kivy整数的最小值和最大值

时间:2017-09-05 13:33:38

标签: python python-3.x kivy python-3.6

我有一个数字,我希望范围从0到100,不多也不少。我尝试将号码设置为:

ego = NumericProperty(0, min=0, max=100)

然而,当我按下此按钮时,该数字仍然可以超过100:

on_release: root.update_ego()
Button:
    text: "increase ego"
    pos: 700,500
    on_release: root.update_ego()

我的.py文件说:

def update_ego(self):
    self.ego += 1

3 个答案:

答案 0 :(得分:1)

由于我不知道问题的原因(也许你必须在代码中的另一个地方设置数字),我建议这个解决方法:

{{1}}

答案 1 :(得分:0)

您应该执行以下操作:

from kivy.properties import BoundedNumericProperty
...
# returns the boundary value when exceeded
ego = BoundedNumericProperty(0, min=0, max=100,
    errorhandler=lambda x: 100 if x > 100 else 0)

示例 - 浮点小数

main.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BoundedNumericProperty


class RootWidget(BoxLayout):
    ego = BoundedNumericProperty(0.0, min=0.0, max=2.0,
                                 errorhandler=lambda x: 2.0 if x > 2.0 else 0.0)

    def update_ego(self):
        print('before increment: ego={0:5.2f}'.format(self.ego))
        self.ego += 1.0
        print('after increment: ego={0:5.2f}'.format(self.ego))


class Test2App(App):

    def build(self):
        return RootWidget()


if __name__ == "__main__":
    Test2App().run()

test2.kv

#:kivy 1.10.0

<RootWidget>:
    orientation: "vertical"
    Label:
        font_size: 70
        center_x: root.width / 4
        top: root.top - 50
        text: "{0:5.2f}".format(root.ego)
    Button:
        text: "increase ego"
        on_release: root.update_ego()

输出

enter image description here

答案 2 :(得分:0)

作为更新,对于醉酒的其他人,您可以完全在kivy按钮本身中更新变量。

Option Explicit
Dim monthsList(11) As String
Dim firstCell As Integer
Dim lastCell As Integer

Private Sub ComboBox1_Change()

    Columns("A:XX").Hidden = False

    '0 to reset the columns
    If ComboBox1.Value = 0 Then Exit Sub

    'take the last month of the list
    Dim var1() As String
    var1 = Split(monthsList(11), "~")

    firstCell = 4 'assuming that the start of the date begins from column 4
    lastCell = Val(var1(2))

    Range(Cells(firstCell, firstCell), Cells(firstCell, lastCell)).EntireColumn.Hidden = True

    Erase var1

    var1 = Split(monthsList(ComboBox1.Value - 1), "~")

    firstCell = Val(var1(1))
    lastCell = Val(var1(2))

    'display only current selected month
    Range(Cells(firstCell, firstCell), Cells(firstCell, lastCell)).EntireColumn.Hidden = False

End Sub


Private Sub Worksheet_Activate()

    Erase monthsList

    'unhide any columns or write a macro that recognises the hidden columns and keeps it hidden
    Columns("A:XX").Hidden = False
    Dim i As Integer
    ComboBox1.Clear
    For i = 0 To 12
        ComboBox1.AddItem i
    Next

    '0 added to display all the sheets

    ComboBox1.Select (1)

    'let this be the start of the date range
    'select the cell in the second row
    Range("D2").Select

    'go to the last date
    Selection.End(xlToRight).Select

    'go to the first row
    ActiveCell.Offset(-1, 0).Select

    lastCell = ActiveCell.Column
    'select the last month
    Selection.End(xlToLeft).Select

    For i = 12 To 1 Step -1
                            'cell value             start date of the month     last date of the month
        monthsList(i - 1) = ActiveCell.Value & "~" & ActiveCell.Column & "~" & lastCell
        lastCell = ActiveCell.Column - 1
        Selection.End(xlToLeft).Select
    Next

End Sub

这可以减少您拥有的功能数量,并避免不必要的逻辑障碍。