Kivy MDLabel 在 RecycleView 中更新数据后丢失文本

huangapple go评论114阅读模式
英文:

Kivy MDLabel in RecycleView losing text after updating data

问题

I'm having two screens, loginScreen and mainScreen. In the mainScreen I have a RecycleView of MDLabels. Initially when entering on mainScreen everything works fine, but whenever I'm refreshing the data of my RecycleView, the text of some labels keeps disappearing and appearing on scrolling.
When I use regular kivy labels instead of MDLabels, I'm not getting this strange behavior. Am I doing something wrong in the code or is this expected when using MDLabels in RecycleView?

main.py

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager
from kivy.uix.screenmanager import Screen
from kivymd.color_definitions import colors
from kivymd.uix.boxlayout import MDBoxLayout
import random

class DailyService(MDBoxLayout):
    pass

class MainScreen(Screen):
    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)

    def switchButton(self):
        self.manager.switchToLoginScreen()

class LoginScreen(Screen):
    def __init__(self, **kwargs):
        super(LoginScreen, self).__init__(**kwargs)

    def switchButton(self):
        self.manager.switchToMainScreen()
    
class MyScreenManager(ScreenManager):
    def __init__(self, **kwargs):
        super(MyScreenManager, self).__init__(**kwargs)
        
    def switchToMainScreen(self):
        data = []
        for i in range(20):
            k = random.randint(0, 9)
            if k%2 == 0:
                color =  colors['BlueGray']['700']
            else:
                color =  colors['Green']['700']
            data.append({'day': 'DAY',
                         'service': 'SERVICE',
                         'bg_color':  color})
        self.mainScreen.rvid.data = data
        self.current = 'mainScreen'

    def switchToLoginScreen(self):
        self.current = 'loginScreen'


class MyApp(MDApp):
    def build(self):
        self.theme_cls.theme_style = 'Dark'
        self.theme_cls.primary_palette = 'Blue'
        self.theme_cls.accent_palette = 'Amber'
        return Builder.load_file('main.kv')

if __name__ == '__main__':
    MyApp().run()

main.kv

<LoginScreen>:
    name: 'loginScreen'

    Button:
        text: 'MAIN'
        on_release: root.switchButton()
        
<DailyService>:
    bg_color: app.theme_cls.primary_dark
    day: ''
    service: ''
    
    MDGridLayout:
        rows: 2
        MDLabel:
            halign: 'center'
            text: root.day

        MDLabel:
            halign: 'center'
            md_bg_color: root.bg_color
            text: root.service

<MainScreen>:
    name: 'mainScreen'
    rvid: myRv

    MDRecycleView:
        viewclass: 'DailyService'
        id: myRv
        RecycleBoxLayout:
            default_size: None, dp(200)
            default_size_hint: 1, None
            size_hint_y: None
            height: self.minimum_height
            orientation: 'vertical'
            
    Button:
        pos_hint: {'x': 0.5, 'bottom': 1}
        size_hint: 0.4, 0.1
        text: 'LOGIN'
        on_release: root.switchButton()

MyScreenManager:
    loginScreen: loginScreenId
    mainScreen: mainScreenId

    LoginScreen:
        id: loginScreenId

    MainScreen:
        id: mainScreenId

Screenshot of the first enter on mainScreen:
Kivy MDLabel 在 RecycleView 中更新数据后丢失文本

Screenshot of the second enter on mainScreen after data update:
Kivy MDLabel 在 RecycleView 中更新数据后丢失文本

英文:

I'm having two screens, loginScreen and mainScreen. In the mainScreen I have a RecycleView of MDLabels. Initially when entering on mainScreen everything works fine, but whenever I'm refreshing the data of my RecycleView, the text of some labels keeps disappearing and appearing on scrolling.
When I use regular kivy labels instead of MDLabels, I'm not getting this strange behavior. Am I doing something wrong in the code or is this expected when using MDLabels in RecycleView?

main.py

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager
from kivy.uix.screenmanager import Screen
from kivymd.color_definitions import colors
from kivymd.uix.boxlayout import MDBoxLayout
import random

class DailyService(MDBoxLayout):
    pass

class MainScreen(Screen):
    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)

    def switchButton(self):
        self.manager.switchToLoginScreen()

class LoginScreen(Screen):
    def __init__(self, **kwargs):
        super(LoginScreen, self).__init__(**kwargs)

    def switchButton(self):
        self.manager.switchToMainScreen()
    
class MyScreenManager(ScreenManager):
    def __init__(self, **kwargs):
        super(MyScreenManager, self).__init__(**kwargs)
        #self.current = &#39;loginScreen&#39;
        
    def switchToMainScreen(self):
        data = []
        for i in range(20):
            k = random.randint(0, 9)
            if k%2 == 0:
                color =  colors[&#39;BlueGray&#39;][&#39;700&#39;]
            else:
                color =  colors[&#39;Green&#39;][&#39;700&#39;]
            data.append({&#39;day&#39;: &#39;DAY&#39;,
                         &#39;service&#39;: &#39;SERVICE&#39;,
                         &#39;bg_color&#39;:  color})
        self.mainScreen.rvid.data = data
        self.current = &#39;mainScreen&#39;

    def switchToLoginScreen(self):
        self.current = &#39;loginScreen&#39;


class MyApp(MDApp):
    def build(self):
        self.theme_cls.theme_style = &#39;Dark&#39;
        self.theme_cls.primary_palette = &#39;Blue&#39;
        self.theme_cls.accent_palette = &#39;Amber&#39;
        return Builder.load_file(&#39;main.kv&#39;)

if __name__ == &#39;__main__&#39;:
    MyApp().run()

main.kv


&lt;LoginScreen&gt;:
	name: &#39;loginScreen&#39;

	Button:
		text: &#39;MAIN&#39;
		on_release: root.switchButton()
		
&lt;DailyService&gt;:
	bg_color: app.theme_cls.primary_dark
    day: &#39;&#39;
    service: &#39;&#39;
	
    MDGridLayout:
        rows: 2
        MDLabel:
			halign: &#39;center&#39;
            text: root.day

        MDLabel:
			halign: &#39;center&#39;
			md_bg_color: root.bg_color
            text: root.service

		
&lt;MainScreen&gt;:
	name: &#39;mainScreen&#39;
	rvid: myRv

	MDRecycleView:
		viewclass: &#39;DailyService&#39;
		id: myRv
		RecycleBoxLayout:
			default_size: None, dp(200)
			default_size_hint: 1, None
			size_hint_y: None
			height: self.minimum_height
			orientation: &#39;vertical&#39;
			
	Button:
		pos_hint:{&quot;x&quot;:0.5,&#39;bottom&#39;: 1}
		size_hint: 0.4, 0.1
		text: &#39;LOGIN&#39;
		on_release: root.switchButton()

MyScreenManager:
	loginScreen: loginScreenId
	mainScreen: mainScreenId

    LoginScreen:
        id: loginScreenId
		
	MainScreen:
		id: mainScreenId

Screenshot of the first enter on mainScreen:
Kivy MDLabel 在 RecycleView 中更新数据后丢失文本

Screenshot of the second enter on mainScreen after data update:
Kivy MDLabel 在 RecycleView 中更新数据后丢失文本

答案1

得分: 0

问题似乎在使用Kivy和Kivymd的主分支时已解决。

英文:

The issue appears to be solved when using Kivy and Kivymd from the master branch.

huangapple
  • 本文由 发表于 2023年6月18日 23:56:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定