kivy - 让一个微调器记住它的选择

时间:2014-11-24 12:35:42

标签: python-3.x spinner kivy jsonstore getselection

我希望有人可以告诉我你可以让一个微调器在加载应用程序时记住它的值。

我在考虑使用kivy.storage.jsonstore导入JsonStore来存储和检索用户在数据库中选择的值,这很容易。

但是经过几个小时的搜索,我找不到任何关于让微调器在加载时说出该值的文档。

例如,我的微调器中有很多项目(大约60个),滚动需要一段时间,因为我说我能记住最后选择的项目,但我无法弄清楚如何使它成为默认值。

    Spinner:
    id: stationSpinner
    text:'Select a Station'
    values: ('Reset Saved Station','Appledore','Axminster','Bampton','Barnstaple','Bere Alston','Bideford','Bovey Tracey','Braunton','Bridgwater','Brixham','Buckfastleigh','Budleigh Salterton','Burnham on Sea','Camels Head','Castle Cary','Chagford','Chard','Cheddar','Chulmleigh','Colyton','Combe Martin','Crediton','Crewkerne','Crownhill','Cullompton','Dartmouth','Dawlish','Danes Castle','Middlemoor','Exmouth','Frome','Glastonbury','Greenbank','Hartland','Hatherleigh','Holsworthy','Honiton','Ilfracombe','Ilminster','Ivybridge','Kingsbridge','Kingston','Lundy Island','Lynton','Martock','Minehead','Modbury','Moretonhampstead','Nether Stowey','Newton Abbot','North Tawton','Okehampton','Ottery St Mary','Paignton','Plympton','Plymstock','Porlock','Princetown','Salcombe','Seaton','Shepton Mallet','Sidmouth','Somerton','South Molton','Street','Taunton','Tavistock','Teignmouth','Tiverton','Topsham','Torquay','Torrington','Totnes','USAR','Wellington','Wells','Williton','Wincanton','Witheridge','Wiveliscombe','Woolacombe','Yelverton','Yeovil')
    size_hint: None, None
    size: (150, 44)
    pos_hint: {'center_x':0.5, 'y': 0.35}
    on_text: app.show_selected_value()

因此,第一次加载应用时,它会说“选择一个电台”#39;第二次加载它显示最后选择的电台。显然我已经将reset选项放入以清除db文件。

我尝试过使用

SvdStn = store.get('Stations')['Saved']
self.root.station.text = SvdStn

但它不起作用,我猜它相当简单,但由于我无法找到任何文件,我无法理解它...我会保持看起来并且可能从不同的方向来看,可能有一个按钮变得可见,最后一个选择在微调器旁边,但如果db为空,则将visible属性设置为false。

编辑:这是我的代码py file and logo on onedrive

欢迎任何想法

由于

RAIF

这是代码的相关部分:

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.storage.jsonstore import JsonStore
from os.path import join

kv = '''
<IncidentScreen>:
    canvas.before:
        Color:
            rgba: 1, 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size

    station: stationSpinner
    Image:
        source:'DSFRSLogo.png'
        allow_stretch:True
        pos_hint: {'root.x':0.5, 'y':.2}

    Spinner:
        id: stationSpinner
        text:'Select a Station'
        values: ('Reset Saved Station','Appledore','Axminster','Bampton','Barnstaple','Bere Alston','Bideford','Bovey Tracey','Braunton','Bridgwater','Brixham','Buckfastleigh','Budleigh Salterton','Burnham on Sea','Camels Head','Castle Cary','Chagford','Chard','Cheddar','Chulmleigh','Colyton','Combe Martin','Crediton','Crewkerne','Crownhill','Cullompton','Dartmouth','Dawlish','Danes Castle','Middlemoor','Exmouth','Frome','Glastonbury','Greenbank','Hartland','Hatherleigh','Holsworthy','Honiton','Ilfracombe','Ilminster','Ivybridge','Kingsbridge','Kingston','Lundy Island','Lynton','Martock','Minehead','Modbury','Moretonhampstead','Nether Stowey','Newton Abbot','North Tawton','Okehampton','Ottery St Mary','Paignton','Plympton','Plymstock','Porlock','Princetown','Salcombe','Seaton','Shepton Mallet','Sidmouth','Somerton','South Molton','Street','Taunton','Tavistock','Teignmouth','Tiverton','Topsham','Torquay','Torrington','Totnes','USAR','Wellington','Wells','Williton','Wincanton','Witheridge','Wiveliscombe','Woolacombe','Yelverton','Yeovil')
        size_hint: None, None
        size: (150, 44)
        pos_hint: {'center_x':0.5, 'y': 0.35}
        on_text: app.show_selected_value()

'''
class IncidentScreen(FloatLayout):
    station = ObjectProperty(None)
    results = ObjectProperty(None)
    data_dir = App().user_data_dir
    store = JsonStore(join(data_dir, 'storage.json'))
    try:
        store.get('stations')['saved']
    except KeyError:
        SvdStn = ""
    else:
        SvdStn = store.get('stations')['saved']
        print(SvdStn)
        self.root.station.text = SvdStn

class DSFRSapp(App):

    def build(self):            
        Builder.load_string(kv)
        fl = IncidentScreen()
        return fl

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

1 个答案:

答案 0 :(得分:0)

你提到的用于微调器的方法工作正常,但看起来你误解了selfApp()的使用。 self引用类的当前实例,并作为类方法的参数提供,因此您不能在类方法之外使用它。您已尝试直接在kivy属性下使用它,但在这种情况下它不包含值。您还尝试使用App(),但这会尝试创建App对象的实例。我将把加载和保存留在应用程序类中,并允许使用额外的参数对其构造函数初始化微调器,如下所示:

class IncidentScreen(FloatLayout):
    station = ObjectProperty(None)
    results = ObjectProperty(None)

    def __init__(self, station, **kwargs):
        super(IncidentScreen, self).__init__(**kwargs)
        self.station.text = station

class DSFRSapp(App):

    def build(self):            
        Builder.load_string(kv)
        data_dir = self.user_data_dir
        store = JsonStore(join(data_dir, 'storage.json'))
        SvdStn = ""

        try:
            store.get('stations')['saved']
            SvdStn = store.get('stations')['saved']
        except KeyError:
            pass

        fl = IncidentScreen(SvdStn)
        return fl

您必须为show_selected_value方法执行类似操作。我不得不禁用它以使代码运行,因为它会在gui显示时立即调用。总的来说,我建议你仔细看看python如何处理类。以后它会为你节省很多精力。如果你谷歌,有很多教程。