Home > Enterprise >  How to update MDLabel's Text on button click in kivy
How to update MDLabel's Text on button click in kivy

Time:09-29

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 tkinter. So I'm giving examples in tkinter which I wanna do with kivymd.

I tkinter:

from tkinter import *
import random

def change_word():
      site_list=['Google','Yahoo','Microsoft','APKpure','APKMB','Stackoverflow','Bing']
      text=random.choice(site_list)
      button_text.config(text=text)
      button_text.update()


root=Tk()
root.title('Help Me')
root.geometry('400x400')

button_text=Label(root,text='Click the Button Below to Change This Text',font='arial 15')
button_text.pack(pady=40)

button=Button(root,text='Change It',font='arial 15',command=change_word)
button.pack(pady=10)

root.mainloop()

I can update Label/Text with a def/Function using idname.config() to edit text and idname.update() to update it.

And in Kivymd:

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:
                  #What Command Should I type Here to Update 'text-update'/MDLabel's text?
"""

class AnsweredOrNot(MDApp):
      def build(self):
          builder=Builder.load_string(please_anwser_this)
          return builder
      
      def change_word(self): #What Parameters should I give after self?
            site_list=['Google','Yahoo','Microsoft','APKpure','APKMB','Stackoverflow','Bing']
            text=random.choice(site_list)


AnsweredOrNot().run()

I wanna update MDLabel.text/text-update.text with a function/def (Like tkinter/Any other way) while the button pressed. Can anyone Help me??

CodePudding user response:

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()
                  
"""

class AnsweredOrNot(MDApp):
      def build(self):
          builder=Builder.load_string(please_anwser_this)
          return builder
      
      def change_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

  • Related