Kivi - 如何根据取自 mysql 的变量更改标签文本
Kivi - How to change text of label based on variable taken from mysql
我需要根据 MySql 数据库中一行的值更改标签。我有这个 table:
|id_match | match |
| 1 | Juventus - Milan |
| 2 | Roma - inter |
我有这个.py
import mysql.connector
from kivmob import KivMob, TestIds, RewardedListenerInterface
from kivy.app import App
from kivy.uix.button import Button
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.properties import StringProperty
Builder.load_file("prove.kv")
db = mysql.connector.connect(
db="db",
host="127.0.0.1",
user="root",
password="-----")
c = db.cursor()
c.execute("""CREATE TABLE if not exists partite(
id_match INTEGER,
match VARCHAR(255),
PRIMARY KEY (id_match))
""")
class prono1(Screen):
home = ObjectProperty(None)
away = ObjectProperty(None)
def get_match(self, id_partita):
query = "SELECT partita FROM db.partite where id_partita = VALUES (%s)"
values = (id_partita)
c.execute(query, values)
partita = c.fetchone() ## <---- In this variable I have the label that I want to display
return print(partita)
class prono2(Screen):
home = ObjectProperty(None)
away = ObjectProperty(None)
def addresultToDB(self):
query = """INSERT INTO risultati (user_id, id_partita, home, away) VALUES (%s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
home = VALUES (home),
away = VALUES (away)"""
values = (user_id, 2, self.home.text, self.away.text)
c.execute(query, values)
db.commit()
print(c.rowcount, "record inserted.")
class ScreenManagement(ScreenManager):
ID_global = StringProperty('cccc')
class MyApp(MDApp):
user_id = StringProperty()
def set_screen(self, screen_name):
self.root.current = screen_name
def build(self):
sm = ScreenManager()
sm.add_widget(prono1(name='prono1'))
sm.add_widget(prono2(name='prono2'))
return sm
if __name__ == "__main__":
MyApp().run()
还有这个.KV
<Prono1>:
name:'prono1'
home: home
away: away
MDCard:
size_hint: None, None
size: 300,200
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
padding: 25
spacing: 25
orientation: "vertical"
MDLabel:
size_hint_y: None
size_hint_x: 1
height: self.texture_size[1]
text_size: self.width, None
font_size: 20
padding: 10, 20
color: 0,0,0,1
text: 'bla bla bla' #### <------------ name of the match that I want to display (WITH ID = 1 - so Juventus - Milan)
halign: 'center'
GridLayout:
cols:3
MDTextFieldRect:
id: home
halign:"center"
font_size:30
MDLabel:
id: None
text:" - "
font_size:18
halign:"center"
MDTextFieldRect:
id: away
font_size:30
halign:"center"
MDRoundFlatButton:
text: "Invia"
font_size: 12
pos_hint: {"center_x": 0.75,"center_y": 0.20 }
on_press: root.addresultToDB()
<Prono2>:
name:'prono2'
home: home
away: away
MDCard:
size_hint: None, None
size: 300,200
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
padding: 25
spacing: 25
orientation: "vertical"
MDLabel:
size_hint_y: None
size_hint_x: 1
height: self.texture_size[1]
text_size: self.width, None
font_size: 20
padding: 10, 20
color: 0,0,0,1
text: 'bla bla bla' #### <------------ name of the match that I want to display (WITH ID = 2 - so Roma - inter)
halign: 'center'
GridLayout:
cols:3
MDTextFieldRect:
id: home
halign:"center"
font_size:30
MDLabel:
id: None
text:" - "
font_size:18
halign:"center"
MDTextFieldRect:
id: away
font_size:30
halign:"center"
MDRoundFlatButton:
text: "Invia"
font_size: 12
pos_hint: {"center_x": 0.75,"center_y": 0.20 }
on_press: root.addresultToDB()
使用 def get_match()
我正在获取我正在寻找的值。但是我不知道如何在我在评论中突出显示的标签中显示。
这样,如果我更改每个 id_match 的匹配项,我在显示的标签中就有更新的匹配项。
尝试以下操作:
def build(self):
self.sm = sm = ScreenManager()
self.wid1 = wid1 = prono1(name='prono1')
self.wid2 = wid2 = prono2(name='prono2')
sm.add_widget(wid1)
sm.add_widget(wid2)
sm.ids['wid1']= wid1
sm.ids['wid2']= wid2
return sm
这样您就可以从 .py
文件中的任何位置访问 prono1
和 prono2
实例。
现在,在 .kv
文件中,在要访问的标签中设置一个 id
。例如:
MDLabel:
id: labl1 ## <------ Here you set the id
size_hint_y: None
size_hint_x: 1
height: self.texture_size[1]
text_size: self.width, None
font_size: 20
padding: 10, 20
color: 0,0,0,1
text: 'bla bla bla' #### <------------ name of the match that I want to display (WITH ID = 2 - so Roma - inter)
halign: 'center'
要从 .py
文件中访问该标签,您需要执行以下操作:
App.get_running_app().sm.ids['wid1'].ids.labll
在上面的代码中,ids['wid1']
是您的 prono1 实例,.ids.lbll
引用带有 id:labl1
的标签
现在您可以在程序中的任何位置编辑该标签,例如,如果您想在函数 get_match
中编辑该标签,则:
def get_match(self, id_partita):
query = "SELECT partita FROM db.partite where id_partita = VALUES (%s)"
values = (id_partita)
c.execute(query, values)
partita = c.fetchone() ## <---- In this variable I have the label that I want to display
App.get_running_app().sm.ids['wid1'].ids.labll.text = 'Text has changed'
return print(partita)
我需要根据 MySql 数据库中一行的值更改标签。我有这个 table:
|id_match | match |
| 1 | Juventus - Milan |
| 2 | Roma - inter |
我有这个.py
import mysql.connector
from kivmob import KivMob, TestIds, RewardedListenerInterface
from kivy.app import App
from kivy.uix.button import Button
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.properties import StringProperty
Builder.load_file("prove.kv")
db = mysql.connector.connect(
db="db",
host="127.0.0.1",
user="root",
password="-----")
c = db.cursor()
c.execute("""CREATE TABLE if not exists partite(
id_match INTEGER,
match VARCHAR(255),
PRIMARY KEY (id_match))
""")
class prono1(Screen):
home = ObjectProperty(None)
away = ObjectProperty(None)
def get_match(self, id_partita):
query = "SELECT partita FROM db.partite where id_partita = VALUES (%s)"
values = (id_partita)
c.execute(query, values)
partita = c.fetchone() ## <---- In this variable I have the label that I want to display
return print(partita)
class prono2(Screen):
home = ObjectProperty(None)
away = ObjectProperty(None)
def addresultToDB(self):
query = """INSERT INTO risultati (user_id, id_partita, home, away) VALUES (%s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
home = VALUES (home),
away = VALUES (away)"""
values = (user_id, 2, self.home.text, self.away.text)
c.execute(query, values)
db.commit()
print(c.rowcount, "record inserted.")
class ScreenManagement(ScreenManager):
ID_global = StringProperty('cccc')
class MyApp(MDApp):
user_id = StringProperty()
def set_screen(self, screen_name):
self.root.current = screen_name
def build(self):
sm = ScreenManager()
sm.add_widget(prono1(name='prono1'))
sm.add_widget(prono2(name='prono2'))
return sm
if __name__ == "__main__":
MyApp().run()
还有这个.KV
<Prono1>:
name:'prono1'
home: home
away: away
MDCard:
size_hint: None, None
size: 300,200
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
padding: 25
spacing: 25
orientation: "vertical"
MDLabel:
size_hint_y: None
size_hint_x: 1
height: self.texture_size[1]
text_size: self.width, None
font_size: 20
padding: 10, 20
color: 0,0,0,1
text: 'bla bla bla' #### <------------ name of the match that I want to display (WITH ID = 1 - so Juventus - Milan)
halign: 'center'
GridLayout:
cols:3
MDTextFieldRect:
id: home
halign:"center"
font_size:30
MDLabel:
id: None
text:" - "
font_size:18
halign:"center"
MDTextFieldRect:
id: away
font_size:30
halign:"center"
MDRoundFlatButton:
text: "Invia"
font_size: 12
pos_hint: {"center_x": 0.75,"center_y": 0.20 }
on_press: root.addresultToDB()
<Prono2>:
name:'prono2'
home: home
away: away
MDCard:
size_hint: None, None
size: 300,200
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
padding: 25
spacing: 25
orientation: "vertical"
MDLabel:
size_hint_y: None
size_hint_x: 1
height: self.texture_size[1]
text_size: self.width, None
font_size: 20
padding: 10, 20
color: 0,0,0,1
text: 'bla bla bla' #### <------------ name of the match that I want to display (WITH ID = 2 - so Roma - inter)
halign: 'center'
GridLayout:
cols:3
MDTextFieldRect:
id: home
halign:"center"
font_size:30
MDLabel:
id: None
text:" - "
font_size:18
halign:"center"
MDTextFieldRect:
id: away
font_size:30
halign:"center"
MDRoundFlatButton:
text: "Invia"
font_size: 12
pos_hint: {"center_x": 0.75,"center_y": 0.20 }
on_press: root.addresultToDB()
使用 def get_match()
我正在获取我正在寻找的值。但是我不知道如何在我在评论中突出显示的标签中显示。
这样,如果我更改每个 id_match 的匹配项,我在显示的标签中就有更新的匹配项。
尝试以下操作:
def build(self):
self.sm = sm = ScreenManager()
self.wid1 = wid1 = prono1(name='prono1')
self.wid2 = wid2 = prono2(name='prono2')
sm.add_widget(wid1)
sm.add_widget(wid2)
sm.ids['wid1']= wid1
sm.ids['wid2']= wid2
return sm
这样您就可以从 .py
文件中的任何位置访问 prono1
和 prono2
实例。
现在,在 .kv
文件中,在要访问的标签中设置一个 id
。例如:
MDLabel:
id: labl1 ## <------ Here you set the id
size_hint_y: None
size_hint_x: 1
height: self.texture_size[1]
text_size: self.width, None
font_size: 20
padding: 10, 20
color: 0,0,0,1
text: 'bla bla bla' #### <------------ name of the match that I want to display (WITH ID = 2 - so Roma - inter)
halign: 'center'
要从 .py
文件中访问该标签,您需要执行以下操作:
App.get_running_app().sm.ids['wid1'].ids.labll
在上面的代码中,ids['wid1']
是您的 prono1 实例,.ids.lbll
引用带有 id:labl1
现在您可以在程序中的任何位置编辑该标签,例如,如果您想在函数 get_match
中编辑该标签,则:
def get_match(self, id_partita):
query = "SELECT partita FROM db.partite where id_partita = VALUES (%s)"
values = (id_partita)
c.execute(query, values)
partita = c.fetchone() ## <---- In this variable I have the label that I want to display
App.get_running_app().sm.ids['wid1'].ids.labll.text = 'Text has changed'
return print(partita)