【发布时间】:2022-01-16 23:18:55
【问题描述】:
我需要根据 MySql db 中一行的值更改标签。我有这张桌子:
|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 的匹配项,我在显示的标签中就有更新的匹配项。
【问题讨论】:
-
“partita”是字符串吗?
-
是的,它是一个字符串