访问 .py 文件中动态创建的小部件 (kivy)

问题描述 投票:0回答:0

我正在使用kivy、kivymd和python创建一个简单的住房应用程序(学习kivy), 我在 python 端动态制作了小部件,我想评估它们,以便能够从数据库中检索数据。我不明白或不知道如何使用 weakref 或任何其他方法来访问图像和下面函数中的许多标签。

    def show_records(self):
        screen_manager.current = "single"
        mydb = mysql.connector.connect(
            host="localhost",
            user="root",
            passwd="password123",
            database="housing_details"

        )
        c = mydb.cursor()
        c.execute('SELECT * FROM test_two')

        myresults = c.fetchall()

        for x in myresults:
            self.root.get_screen('single').ids.grid.add_widget(
                MDCard(
                    Image(
                        pos_hint={"center_y":.5},
                        size_hint=(None,None),
                        height='160dp',
                        width="150dp"
                    ),
                    MDBoxLayout(
                        MDLabel(text="rental type", pos_hint={"center_x":.7}),
                        MDLabel(text="location", pos_hint={"center_x":.7}),
                        MDLabel(text="price", pos_hint={"center_x":.7}),
                        MDLabel(text="contact info", pos_hint={"center_x":.7}),
                        MDLabel(text="more info", pos_hint={"center_x":.7}),
                        orientation="vertical",
                        #md_bg_color=[1,0,0,1]
                    ),
                    orientation="horizontal",
                    size_hint=(1, None),
                    height="200dp",
                    padding=8,
                    spacing=2,
                    elevation=5,
                    radius=15,
                    ripple_behaviour=True,
                    md_bg_color=[1,1,0,1]
                            )
                        )

        mydb.commit()
        mydb.close()


if __name__ == "__main__":
    Galaxy().run()
MDScreen:
  name: "single"


  MDFloatLayout:
    md_bg_color:0,1,1,1
    MDIconButton:
      icon:"arrow-left"
      pos_hint:{"center_y":.95}
      user_font_size:"30sp"
      theme_text_color:"Custom"
      text_color:rgba(26,24,58,255)
      on_release:
        root.manager.transition.direction= "right"
        root.manager.current="login"
    MDScrollView:
      pos_hint:{"center_y":.399,"center_x":.5}
      size:self.size
      MDGridLayout:
        size_hint_y:None
        height:self.minimum_height
        width:self.minimum_width
        cols:1
        spacing:"20dp"
        padding:"20dp"
        id:grid

python mysql kivy-language kivymd
© www.soinside.com 2019 - 2024. All rights reserved.