我正在尝试使用kivy和Python开发应用程序。我正在尝试将变量从python函数传递给kivy标签。即使我在过去的答案中寻找解决方案,也没有针对我特定问题的解决方案。
这是Python代码
from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
import pandas as pd
class Home(Screen):
pass
class Trends(Screen):
provincia = ObjectProperty()
def getdata(self):
df = pd.read_csv("https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-province/dpc-covid19-ita-province.csv")
prov = self.provincia.text
r = df['denominazione_provincia']== prov
nu = len(r)
rows = []
for check, row in zip(r, range(nu)):
if check == True:
rows.append(row)
for n in rows:
tot_casi = df.iloc[n]['totale_casi']
date_casi = df.iloc[n]['data']
casi = StringProperty(str(tot_casi))
#variable i need to pass to label
return print(casi)
pass
class Autocertificate(Screen):
pass
class News(Screen):
pass
class Main(ScreenManager):
pass
kv = Builder.load_file('my.kv')
class MyApp(App):
def build(self):
return kv
if __name__ == '__main__':
MyApp().run()
这是my.kv:
Main:
Home:
Trends:
News:
Autocertificate
<Home>:
name: 'Home'
canvas.before:
Rectangle:
pos: 0, self.height/2
size: self.width, self.height/2
source: 'bg.png'
Rectangle:
pos: 0, 0
size: self.width, self.height/2
Color:
rgba: (1, 1, 1, 1)
GridLayout:
rows: 8
padding: [10, 10, 10, 10]
Label:
Label:
text: 'Prevenire \nè meglio \nche curare.'
font_size: 40
color: 1, 1, 1, 1
bold: True
Label:
Label:
Button:
on_release: app.root.current = "Autocertificate"
color: 0.2, 0.6, 0.9, 1
background_color: 1,1,1,0
text: 'Autocertificazioni'
font_size: 15
Button:
on_release: app.root.current = "Trends"
color: 0.2, 0.6, 0.9, 1
background_color: 1,1,1,0
text: 'Contagi'
font_size: 15
Button:
color: 0.2, 0.6, 0.9, 1
background_color: 1,1,1,0
text: 'Notizie'
font_size: 15
Button:
color: 0.2, 0.6, 0.9, 1
background_color: 1,1,1,0
text: 'Chi Contattare'
font_size: 15
<Trends>:
name: 'Trends'
provincia: prov
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
GridLayout:
padding: 10
rows: 9
GridLayout:
cols:3
Button:
on_release: app.root.current = "Home"
size: 60, 60
size_hint: None, None
Image:
source: 'back_white.png'
center_x: self.parent.center_x
center_y: self.parent.center_y
size_hint_y: None
height: dp(60)
Label:
text: 'Contagi'
Label
Label
TextInput:
id: prov
hint_text: 'Inserisci la tua Provincia.'
background_color: 1, 1, 1, 1
size_hint: (.2, None)
height: 30
multiline: False
background_color: 0,0,0,0.05
GridLayout:
cols:5
Label:
Label:
Button:
background_color: 0.1, 0.7, 0.9, 1
text: 'Vedi Contagi'
size_hint: None, None
size: 100, 40
pos: 200, 20
on_press: root.getdata()
#this below is the label i need to fill
Label:
Label:
Label:
Label:
Label:
Label:
Label:
<Autocertificate>:
name: 'Autocertificate'
canvas.before:
Rectangle:
pos: 0, self.height/2
size: self.width, self.height/2
source: 'bg.png'
Rectangle:
pos: 0, 0
size: self.width, self.height/2
Color:
rgba: (1, 1, 1, 1)
<News>:
name: 'News'
canvas.before:
Color:
rgba: 0.2, 0.6, 0.9, 1
Rectangle:
pos: self.pos
size: self.size
size: self.size
解决此问题的最佳做法是什么?