Skip to content Skip to sidebar Skip to footer

How To Update Mdlabel's Text On Button Click In Kivy

I'm beginner in python(and also on kivy). I started to learning kivy(maybe kivymd) 4days ago. I learned basics of it. But I got some problems, Before learning kivy I learned tkinte

Solution 1:

You can reference text to the Label using ids

from kivymd.app import MDApp
from kivy.lang import Builder
import random
from kivy.core.window import Window

Window.size=(400,600)

please_anwser_this="""
MDScreen:
      MDLabel:
            id:text_update
            text:'Click The Button below to Change this text'
            halign:'center'
            pos_hint:{'center_x':0.5,'center_y':0.6}
      MDFillRoundFlatIconButton:
            text:'Change It'
            pos_hint:{'center_x':0.5,'center_y':0.5}
            icon:'crop-rotate'
            on_press:
                  app.change_word()
                  
"""classAnsweredOrNot(MDApp):
      defbuild(self):
          builder=Builder.load_string(please_anwser_this)
          return builder
      
      defchange_word(self): #What Parameters should I give after self?
            site_list=['Google','Yahoo','Microsoft','APKpure','APKMB','Stackoverflow','Bing']
            text=random.choice(site_list)
            self.root.ids.text_update.text=(text)
            


AnsweredOrNot().run()

I've changed Label id from text-update to text_update

Post a Comment for "How To Update Mdlabel's Text On Button Click In Kivy"