2016-08-18 55 views
0

everyone,Python + kivy + SQLite:如何設置標籤初始值以及如何更新標籤文本?

我想使用kivy+Python來顯示db文件中的項目。

爲此我曾問一個問題之前:Python+kivy+SQLite: How to use them together

應用程序的鏈接包含一個屏幕。它工作得很好。

今天我已經將應用程序更改爲兩個屏幕。 first screen沒有特別的要求,只能將應用程序引導至second screen。在second screenlabelbutton。通過點擊button我想讓label text更改。 label text指的是我上面鏈接中的db文件中的car type

對於這個兩個屏幕設計,我有兩個問題:

問題1:被點擊button時如何更新label text

我試圖用兩種方法:

方法A:self.ids.label.text = str(text)
它顯示了我的錯誤信息:AttributeError: 'super' object has no attribute '__getattr__'我用Google搜索了很多,但仍然無法理解。

方法B:self.ids["label"].text = str(text) 它顯示了我的錯誤信息:KeyError: 'label'我很困惑,因爲我已經定義了label

問題2:如何將label origin text設置爲其中一種車型,以便每次顯示第二個屏幕時,都會顯示一個車型。

下面是代碼:(對於db file請參考上面的鏈接。)

# -*- coding: utf-8 -*- 
from kivy.app import App 
from kivy.base import runTouchApp 
from kivy.lang import Builder 
from kivy.properties import ListProperty 
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.floatlayout import FloatLayout 
from kivy.uix.label import Label 
from kivy.uix.widget import Widget 
from kivy.graphics import Rectangle 
from kivy.properties import NumericProperty, StringProperty, BooleanProperty, ListProperty 
from kivy.base import runTouchApp 
from kivy.clock import mainthread 

import sqlite3 
import random 

class MyScreenManager(ScreenManager): 

    def init(self, **kwargs): 
     super().__init__(**kwargs) 

     @mainthread # execute within next frame 
     def delayed(): 
      self.load_random_car() 
     delayed() 

    def load_random_car(self): 
     conn = sqlite3.connect("C:\\test.db") 
     cur = conn.cursor() 

     cur.execute("SELECT * FROM Cars ORDER BY RANDOM() LIMIT 1;") 

     currentAll = cur.fetchone() 
     conn.close() 
     currentAll = list(currentAll) # Change it from tuple to list 

     print currentAll 

     current = currentAll[1] 
     print current 
     self.ids.label.text = str(current) # Method A 
#  self.ids["label"].text = str(current) # Method B 

class FirstScreen(Screen): 
    pass 

class SecondScreen(Screen): 
    pass 

root_widget = Builder.load_string(''' 
#:import FadeTransition kivy.uix.screenmanager.FadeTransition 
MyScreenManager: 
    transition: FadeTransition() 
    FirstScreen: 
    SecondScreen: 

<FirstScreen>: 
    name: 'first' 
    BoxLayout: 
     orientation: 'vertical' 
     Label: 
      text: "First Screen" 
      font_size: 30 
     Button: 
      text: 'Go to 2nd Screen' 
      font_size: 30 
      on_release: app.root.current = 'second' 

<SecondScreen>: 
    name: 'second' 
    BoxLayout: 
     orientation: 'vertical' 
     Label: 
      id: label 
      text: 'click to get a new car' # I want its text changed when the button is clicked everytime. And its original text value should be one of the random car type from the db file. 
      font_size: 30 
     Button: 
      text: 'Click to get a random car' 
      font_size: 30 
      on_release: app.root.load_random_car() 

''') 

class ScreenManager(App): 
    def build(self): 
     return root_widget 

if __name__ == '__main__': 
    ScreenManager().run() 

我已經讀了很多從互聯網上,但我無法理解他們。 :-(

請幫我改正的代碼。謝謝你這麼多!

回答

1
from kivy.app import App 
from kivy.lang import Builder 
from kivy.properties import StringProperty 
from kivy.uix.widget import Widget 

Builder.load_string(""" 
<ScreenX> 
    BoxLayout: 
     orientation: 'vertical' 
     Label: 
      text: root.label_text 
     Button: 
      text: 'Click Me' 
      on_release: root.on_clicked_button() 
""") 

class ScreenX(Widget): 
    label_text = StringProperty("Default Value") 
    def on_clicked_button(self): 
     self.label_text = "New Text!" 


class MyApp(App): 
    def build(self): 
     return ScreenX() 

MyApp().run() 

通常你會怎麼做這個...

+0

棒極了!太謝謝你了! – Rita