Kivy - 从Spinner当前值更新ProgressBar值

时间:2018-02-13 03:48:42

标签: python kivy

我在kivy有一个Spinner和一个ProgressBar。 Spinner填充了6到50之间的值。我的目标是从Spinner(字符串)中提取当前值,将其解析(到int)并使用ProgressBar的value属性上的Spinner值。每次用户更新微调器值时都会发生这种情况。这是我的代码:

main.py

from kivy.app import App
from kivy.config import Config
Config.set('graphics', 'resizable', False)
from kivy.core.window import Window
from kivy.uix.floatlayout import FloatLayout


class ApplicationLayout(FloatLayout):

    def spinnerValues(self, min_value, max_value):

        self.possible_values = []
        for k in range(min_value, max_value + 1):
            self.possible_values.append("" + str(k) + "")

        return self.possible_values

    def clearSelection(self):

        self.ids.cb_word.active = False
        self.ids.cb_letters.active = False
        self.ids.cb_lowercase.active = False
        self.ids.cb_uppercase.active = False
        self.ids.cb_repeated_chars.active = False
        self.ids.cb_numbers.active = False
        self.ids.cb_special_chars.active = False
        self.ids.lb_strength.text = ''
        self.ids.pb_strength.value = 0
        self.ids.it_word.text = ''
        self.ids.it_passwords.text = ''

    def currentValue(self, size):
        print(type(int(size)))  # just to ensure that this method can return an int
        return int(size)


class PasswordGeneratorApp(App):
    def build(self):
        Window.clearcolor = (67 / 255, 67 / 255, 67 / 255, 1)
        self.title = 'Password Generator'
        return ApplicationLayout()


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

passwordgenerator.kv

<Spinner@Spinner>
    size_hint:[0.07,0.05]  

<ButtonWidget@Button>
    size_hint:[0.15,0.07]
    font_size:'18sp'
    background_normal: ''
    background_color: (63/255, 191/255, 191/255, 0.6)

<OneWordLabel@Label>
    font_size:'20sp'
    size_hint:[0.1,0.05]    

<MultiWordLabel@Label>
    font_size:'20sp'
    size_hint:[0.25,0.05]

<CheckBox@CheckBox>
    size_hint:[0.05, 0.05]

<TextInput@TextInput>
    size_hint:[0.3,0.06]
    multiline: True

<ProgressBar@ProgressBar>
    size_hint:[0.4,None]

<ApplicationLayout>:
    padding: [30,30,0,0] 
    MultiWordLabel:
        pos_hint:{'y':0.9} 
        text: 'Password length'

    Spinner:
        text_autoupdate: True
        id: sp_passw_length
        pos_hint: {'right':0.32, 'y':0.9}
        values:  root.spinnerValues(6, 50)
        on_text: root.currentValue(self.text)

    ProgressBar:
        id:pb_strength
        pos_hint: {'right': 0.77,'y':0.84}
        value: root.currentValue(sp_passw_length.text)

    OneWordLabel:
        id: lb_strength
        text: 'Strong' 
        pos_hint: {'right': 0.92,'y':0.9}

    OneWordLabel:
        text: 'Include:'
        pos_hint: {'right':0.125, 'y': 0.8}  #fix positioning when screen is maximized

    CheckBox:
        id:cb_word
        pos_hint: {'right':0.19, 'y':0.8}
        active: False
    OneWordLabel:
        text: 'Word'
        pos_hint: {'right':0.275, 'y':0.8}

    TextInput:
        id: it_word
        pos_hint: {'right':0.58, 'y':0.795}  
        disabled: not cb_word.active    

    CheckBox:
        id: cb_letters
        pos_hint: {'right':0.67, 'y':0.8}

    OneWordLabel:
        text: 'Letters:'            
        pos_hint: {'right':0.77, 'y':0.8}    

    CheckBox:
        id: cb_lowercase
        pos_hint: {'right':0.70, 'y':0.73}

    OneWordLabel:
        text: 'Lowercase'
        pos_hint: {'right': 0.815, 'y':0.73}

    CheckBox:     
        id: cb_uppercase
        pos_hint: {'right':0.70, 'y':0.67} 

    OneWordLabel:
        text: 'Uppercase'
        pos_hint: {'right': 0.815, 'y':0.67}

    CheckBox:
        id: cb_numbers
        pos_hint: {'right':0.19, 'y':0.58}

    OneWordLabel:
        text: 'Numbers'
        pos_hint: {'right': 0.30, 'y':0.58}

    CheckBox:
        id: cb_repeated_chars
        pos_hint: {'right':0.51, 'y':0.58}

    MultiWordLabel:
        text: 'Don\'t repeat characters'
        pos_hint: {'right':0.775, 'y':0.58}

    CheckBox:
        id: cb_special_chars
        pos_hint: {'right':0.19, 'y':0.51}

    MultiWordLabel:
        text: 'Special characters'
        pos_hint: {'right':0.425, 'y':0.51}

    OneWordLabel:
        text: 'Quantity:'
        pos_hint: {'right':0.58, 'y':0.51}

    Spinner:
        text_autoupdate: True
        pos_hint: {'right': 0.69, 'y': 0.51}
        values: root.spinnerValues(1, 20)

    ButtonWidget:
        text: 'Generate'
        pos_hint: {'right':0.45, 'y':0.32}

    ButtonWidget:
        text: 'Clear'
        pos_hint: {'right':0.65, 'y':0.32}
        on_press: root.clearSelection()

    MultiWordLabel:
        text: 'Your password(s):'
        pos_hint: {'right': 0.385, 'y':0.15}

    TextInput:
        id: it_passwords
        pos_hint: {'right':0.69, 'y':0.145}
        readonly: True

感兴趣的Spinner和ProgressBar分别具有'sp_passw_length'和'pb_strength'id。

1 个答案:

答案 0 :(得分:0)

如果它不是空字符串,则只需要将值转换为int:

ProgressBar:
    id:pb_strength
    pos_hint: {'right': 0.77,'y':0.84}
    value: root.currentValue(int(sp_passw_length.text)) if sp_passw_length.text != "" else 6