python MDCard涵盖其内容,kivyMD继承问题

thtygnil  于 2022-12-28  发布在  Python
关注(0)|答案(1)|浏览(122)

我在继承自MDCard的.kv文件中创建了此小部件:

<ElementCard@MDCard>:
  radius: '10dp'
  spacing: '10dp'
  padding: '10dp'
  image: ''
  text: ''
  sub_text: ''
  orientation: 'vertical'
  md_bg_color: 0.87, 0, 0.49
  ripple_behavior: True # effect
  on_release:
    app.root.transition = RiseInTransition()
  Image: 
    source: root.image
    halign: 'center'
  MDBoxLayout:
    orientation: 'vertical'
    md_bg_color: 'green'
    MDLabel:
      text: root.text
      color: 'white'
      halign: 'center'
    MDLabel:
      text: root.sub_text
      halign: 'center'

我用它:

<ui>:
...

  ElementCard:
    text: 'IA'
    sub_text: 'Inteligencia artificial'
    image: 'images/image1.jpg'
    on_release: 
      root.current = 'screen1'
...

问题是ElementCard小部件不显示内容:ElementCard is the purple block and it covers all its content.
当我将透明度设置为0.5时,程序如下所示:50% transparency
版本如下:
基维==2.1.0
kivymd==1.1.1
我试过重启机器,重新启动虚拟环境,重新安装kivy和kivymd,但是没有任何效果。
我认为问题出在继承上,因为当我像下面这样直接使用MDCard时:

MDCard:
          radius: '10dp'
          spacing: '10dp'
          padding: '10dp'
          orientation: 'vertical'
          md_bg_color: 0.87, 0, 0.49,0.6
          ripple_behavior: True # effect
          on_release:
            app.root.transition = RiseInTransition()
          Image: 
            source: 'images/image1.jpg'
            halign: 'center'
          MDBoxLayout:
            orientation: 'vertical'
            md_bg_color: 'green'
            MDLabel:
              text: 'IA'.
              color: 'white'
              halign: 'center'
            MDLabel:
              text: 'Inteligencia artificial'
              halign: 'center'

使用最后一段代码,程序运行良好:MDCard works normally.
main.py 码:

from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager
from kivymd.app import MDApp

class Ui(ScreenManager):
    pass

class MainApp(MDApp):
    def build(self):
        self.theme_cls.theme_style ='Dark'
        self.theme_cls.primary_palette ='Teal'
        Builder.load_file("design.kv")
        return Ui()
    def change_style(self, checked, value):
        if value:
            self.theme_cls.theme_style ='Dark'
            pass
        else:
            self.theme_cls.theme_style ='Light'
            pass

if __name__ == "__main__":
    MainApp().run()

设计.kv完整代码:

#:kivy 2.1.0
#:import RiseInTransition kivy.uix.screenmanager.RiseInTransition
#:import SlideTransition kivy.uix.screenmanager.SlideTransition

<CustomizeScreen@MDBoxLayout>:
  orientation: 'vertical'
  text: ''
  padding: 100
  MDLabel: 
    text: root.text
    halign: 'center'
    pos_hint:{'center_x': 0.5}
  
  MDFillRoundFlatButton: 
    text: 'REGRESAR'
    pos_hint: {'center_x': 0.5}
    on_release: 
      app.root.current = 'screen_principal'
      app.root.transition = SlideTransition(direction = 'left')

<ElementCard@MDCard>:
  radius: '10dp'
  spacing: '10dp'
  padding: '10dp'
  image: ''
  text: ''
  sub_text: ''
  orientation: 'vertical'
  md_bg_color: 0.87, 0, 0.49
  ripple_behavior: True # effect
  on_release:
    app.root.transition = RiseInTransition()
  Image: 
    source: root.image
    halign: 'center'
  MDBoxLayout:
    orientation: 'vertical'
    md_bg_color: 'green'
    MDLabel:
      text: root.text
      color: 'white'
      halign: 'center'
    MDLabel:
      text: root.sub_text
      halign: 'center'

<ui>:
  MDScreen:
    name: 'screen_principal'
    md_bg_color: "black"
    MDBoxLayout: 
      orientation: "vertical"
      MDBoxLayout: 
        size_hint: 1, 0.2 # x=1 ocupa todo, y=0.2 ocupa 20%
        orientation: "horizontal"
        padding: '10dp'
        MDCard:
          radius: '10dp'
          padding: '10dp'
          line_color: 1,0,1,1
          MDLabel: 
            text: 'APLICACIÓN DE TECNOLOGÍA'
            halign: 'center'
            pos_hint: {'center_y': 0.5}
          MDSwitch: 
            pos_hint: {'center_y': 0.5}
            on_active:
              app.change_style(*args)

      MDGridLayout: 
        cols: 3
        size_hint: 0.8, 0.8 # x=1 ocupa todo, y=0.8 ocupa 80%
        pos_hint: {'center_x': 0.5}
        padding: ['10dp','10dp','10dp','10dp']
        spacing: '10dp'

        ElementCard:
          text: 'IA'
          sub_text: 'Inteligencia artificial'
          image: 'images/image1.jpg'
          on_release: 
            root.current = 'screen1'
o3imoua4

o3imoua41#

我在github kivyMD项目中评论了这个问题,他们将其标记为bug,并提供了几个选项来实现link to github
编辑:
谢谢你指出我的错误与我的链接。
这是第一个选项:

from kivy.lang import Builder

from kivymd.app import MDApp
from kivymd.uix.card import MDCard

KV = """
<ElementCard>

    MDLabel:
        text: "HELLO WORLD"

MDScreen:

    ElementCard:
        size_hint: 0.5, 0.8 
        pos_hint: {"center_x": .5, "center_y": .5}
        md_bg_color: 0.87, 0, 0.49, 1
"""

class ElementCard(MDCard):
    pass

class MainApp(MDApp):
    def build(self):
        return Builder.load_string(KV)

MainApp().run()

和第二:

from kivy.lang import Builder

from kivymd.app import MDApp
from kivymd.uix.card import MDCard

KV = """
<ElementCard@MDCard>

    MDLabel:
        text: "HELLO WORLD"

MDScreen:

    ElementCard:
        size_hint: 0.5, 0.8 
        pos_hint: {"center_x": .5, "center_y": .5}
        md_bg_color: 0.87, 0, 0.49, 1
"""

class MainApp(MDApp):
    def build(self):
        return Builder.load_string(KV)

MainApp().run()

我想kivymd的开发人员将来会修复这个bug。

相关问题