1. <small id='aniGJ'></small><noframes id='aniGJ'>

      <tfoot id='aniGJ'></tfoot>

      1. <i id='aniGJ'><tr id='aniGJ'><dt id='aniGJ'><q id='aniGJ'><span id='aniGJ'><b id='aniGJ'><form id='aniGJ'><ins id='aniGJ'></ins><ul id='aniGJ'></ul><sub id='aniGJ'></sub></form><legend id='aniGJ'></legend><bdo id='aniGJ'><pre id='aniGJ'><center id='aniGJ'></center></pre></bdo></b><th id='aniGJ'></th></span></q></dt></tr></i><div id='aniGJ'><tfoot id='aniGJ'></tfoot><dl id='aniGJ'><fieldset id='aniGJ'></fieldset></dl></div>
        <legend id='aniGJ'><style id='aniGJ'><dir id='aniGJ'><q id='aniGJ'></q></dir></style></legend>
          <bdo id='aniGJ'></bdo><ul id='aniGJ'></ul>

        Python+kivy+SQLite:如何设置标签初始值以及如何更新

        时间:2023-08-30
              <bdo id='G5mZu'></bdo><ul id='G5mZu'></ul>

              <small id='G5mZu'></small><noframes id='G5mZu'>

            • <i id='G5mZu'><tr id='G5mZu'><dt id='G5mZu'><q id='G5mZu'><span id='G5mZu'><b id='G5mZu'><form id='G5mZu'><ins id='G5mZu'></ins><ul id='G5mZu'></ul><sub id='G5mZu'></sub></form><legend id='G5mZu'></legend><bdo id='G5mZu'><pre id='G5mZu'><center id='G5mZu'></center></pre></bdo></b><th id='G5mZu'></th></span></q></dt></tr></i><div id='G5mZu'><tfoot id='G5mZu'></tfoot><dl id='G5mZu'><fieldset id='G5mZu'></fieldset></dl></div>

                <tbody id='G5mZu'></tbody>

              1. <legend id='G5mZu'><style id='G5mZu'><dir id='G5mZu'><q id='G5mZu'></q></dir></style></legend>
                • <tfoot id='G5mZu'></tfoot>
                  本文介绍了Python+kivy+SQLite:如何设置标签初始值以及如何更新标签文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  大家,

                  我想使用 kivy+Python 来显示 db 文件中的项目.

                  I want to use kivy+Python to display items from a db file.

                  为此,我之前问过一个问题:Python+kivy+SQLite: How一起使用

                  To this purpose I have asked a question before: Python+kivy+SQLite: How to use them together

                  链接中的应用程序包含一个屏幕.它工作得很好.

                  The App in the link contains one screen. It works very well.

                  今天我已将应用程序更改为 两个 屏幕.第一屏没有特殊要求,只是将App引导到第二屏.在 second screen 中有一个 label 和一个 button.通过单击 button 我想更改 label text.label text 指的是我在上面链接中的 db 文件中的 car type.

                  Today I have changed the App to two screens. The first screen has no special requirement but to lead the App to the second screen. In the second screen there is a label and a button. By clicking the button I want to have the label text changed. The label text refers to the car type from the db file that I have in the link above.

                  对于这两个屏幕的设计,我有两个问题:

                  For this two screens design, I have two questions:

                  问题一:点击按钮时如何更新label text?

                  我尝试了两种方法:

                  方法一: self.ids.label.text = str(text)
                  它向我显示了错误消息: AttributeError: 'super' object has no attribute '__getattr__' 我用谷歌搜索了很多,但仍然无法理解.

                  Method A: self.ids.label.text = str(text)
                  It shows me the error message: AttributeError: 'super' object has no attribute '__getattr__' I have googled a lot but still cannot understand.

                  方法B: self.ids["label"].text = str(text)它向我显示错误消息: KeyError: 'label' 我很困惑,因为我定义了 label.

                  Method B: self.ids["label"].text = str(text) It shows me the error message: KeyError: 'label' I am confused because I have the label defined.

                  问题2:如何将label origin text设置为一种车型,这样每次第二屏显示时,已经有车型显示了.

                  Question 2: How to set the label origin text to one of the car type, so that everytime the second screen is shown, there is already a car type shown.

                  代码如下:(db文件请参考上面的链接)

                  Here is the code: (For the db file please refer to the link above.)

                  # -*- 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()
                  

                  我从互联网上阅读了很多内容,但我无法全部理解.:-(

                  I have read a lot from internet, but I cannot understand them all. :-(

                  请帮助我更正代码.非常感谢!

                  Please help me to correct the code. Thank you so much!

                  推荐答案

                  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()
                  

                  通常是你会这样做的方式......

                  is typically how you would do this ...

                  这篇关于Python+kivy+SQLite:如何设置标签初始值以及如何更新标签文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Kivy Buildozer 无法构建 apk,命令失败:./distribute.s 下一篇:Buildozer 未知命令/目标 android_old

                  相关文章

                  最新文章

                  1. <small id='6OWha'></small><noframes id='6OWha'>

                    <legend id='6OWha'><style id='6OWha'><dir id='6OWha'><q id='6OWha'></q></dir></style></legend>
                    • <bdo id='6OWha'></bdo><ul id='6OWha'></ul>

                    <i id='6OWha'><tr id='6OWha'><dt id='6OWha'><q id='6OWha'><span id='6OWha'><b id='6OWha'><form id='6OWha'><ins id='6OWha'></ins><ul id='6OWha'></ul><sub id='6OWha'></sub></form><legend id='6OWha'></legend><bdo id='6OWha'><pre id='6OWha'><center id='6OWha'></center></pre></bdo></b><th id='6OWha'></th></span></q></dt></tr></i><div id='6OWha'><tfoot id='6OWha'></tfoot><dl id='6OWha'><fieldset id='6OWha'></fieldset></dl></div>

                    1. <tfoot id='6OWha'></tfoot>