首页 > 解决方案 > python kivy从firebase获取数据以便能够滚动它

问题描述

您好,我正在制作一个软件,有人会问我一个问题,这个问题将被记录到 firebase,然后它会被打印回屏幕上。问题是在问了几个问题之后,我在 firebase 中的录音变长了,这在我的屏幕上水平消失了。

现在的问题是我需要能够滚动这些问题,最好是垂直滚动。还有一种方法可以将firebase字符串解码为简单的文本而不是那么难看。非常感谢您的帮助!!!!

from kivy.app import App
from kivy.lang import Builder
import json
import requests
a=[]
KV = Builder.load_string ("""
ScreenManager:
    id: manager
    Screen:
        BoxLayout:
            orientation: 'vertical'
            GridLayout:
                cols:1
                Label:
                    text: 'Write a JSON string'
                TextInput:
                    size_hint_y: .4
                    id: JSON
            
                
                Button:
                    text: 'Ask me a question . . <3'
                    on_press:
                        self.text='Ask another question?'
                    on_release: 
                        app.patch(JSON.text)
                        self.text='Ask another question?'
                        
                        app.set_text() 
                        
             
                
              
                Button:
                    text: 'Get & Print Database'
                    on_release:  
                        app.get()
                        app.set_text()
                        
                Label:
                    
                    cols:1
                    id:new_label 
                        
                Label:
                    
                    id: my_label
               
                        
                
                
       
             
""")

class MyApp(App):

    url = 'https://questions-44894-default-rtdb.firebaseio.com/pfpffp.json' # You must add .json to the end of the URL

    def patch(self, JSON):
        to_database = json.loads(JSON)
        requests.patch(url = self.url, json = to_database)

    def post(self, JSON):
        to_database = json.loads(JSON)
        requests.post(url = self.url, json = to_database)

    def put(self, JSON):
        to_database = json.loads(JSON)
        requests.put(url = self.url, json = to_database)

    def delete(self, JSON):
        requests.delete(url = self.url[:-5] + JSON + ".json")

    auth_key = 'bTwFAO5zo89eyXpTu30B4C3Fh2hsw3p3JBDZOgYA' # Refer to the YouTube video on where to find this.

    def get(self):
        request = requests.get(self.url + '?auth=' + self.auth_key)
        #print(type(request))
        request.json()
        new_label=self.root.ids.new_label
        new_label.text= request.text
        

    def set_text(self):

        JSON = self.root.ids.JSON
        my_label = self.root.ids.my_label
        my_label.text = JSON.text
        pass

    def build(self):
        return KV

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

标签: pythonkivy

解决方案


推荐阅读