【问题标题】:How to update MDLabel's Text on button click in kivy如何在kivy中单击按钮时更新MDLabel的文本
【发布时间】:2021-11-21 07:02:42
【问题描述】:

我是 python 的初学者(也是 kivy)。 4天前我开始学习kivy(也许kivymd)。我学习了它的基础知识。但是我遇到了一些问题,在学习 kivy 之前我学习了tkinter。所以我在 tkinter 中给出了我想用 kivymd 做的例子。

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

我可以使用idname.config() 来编辑文本并使用idname.update() 来更新带有def/Function 的Label/Text。

在基维姆德:

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

我想在按下按钮时使用函数/def(如 tkinter/任何其他方式)更新 MDLabel.text/text-update.text。谁能帮帮我?

【问题讨论】:

    标签: python tkinter kivy kivymd


    【解决方案1】:

    您可以使用 ID 将文本引用到标签

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

    我已将标签 ID 从 text-update 更改为 text_update

    【讨论】:

    • 谢谢兄弟,youtube 上没人告诉我ids
    猜你喜欢
    • 2020-09-01
    • 2017-07-20
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2020-06-26
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多