如何在kivy中单击按钮更新MDLabel的文本
How to update MDLabel's Text on button click in kivy
我是 python(以及基维)的初学者。我在 4 天前开始学习 kivy(也许 kivymd
)。我学习了它的基础知识。但是我遇到了一些问题,在学习kivy之前我学习了tkinter
。所以我在 tkinter 中给出了我想用 kivymd 做的例子。
我 tkinter:
from tkinter import *
import random
def change_word():
site_list=['Google','Yahoo','Microsoft','APKpure','APKMB','Whosebug','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()
我可以使用 def/Function 更新 Label
/文本,使用 idname.config()
编辑文本并使用 idname.update()
更新它。
在 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','Whosebug','Bing']
text=random.choice(site_list)
AnsweredOrNot().run()
我想在按下按钮时用 function/def 更新 MDLabel.text
/text-update.text
(类似于 tkinter/Any 其他方式)。谁能帮帮我??
您可以使用 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','Whosebug','Bing']
text=random.choice(site_list)
self.root.ids.text_update.text=(text)
AnsweredOrNot().run()
我已将标签 ID 从 text-update
更改为 text_update
我是 python(以及基维)的初学者。我在 4 天前开始学习 kivy(也许 kivymd
)。我学习了它的基础知识。但是我遇到了一些问题,在学习kivy之前我学习了tkinter
。所以我在 tkinter 中给出了我想用 kivymd 做的例子。
我 tkinter:
from tkinter import *
import random
def change_word():
site_list=['Google','Yahoo','Microsoft','APKpure','APKMB','Whosebug','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()
我可以使用 def/Function 更新 Label
/文本,使用 idname.config()
编辑文本并使用 idname.update()
更新它。
在 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','Whosebug','Bing']
text=random.choice(site_list)
AnsweredOrNot().run()
我想在按下按钮时用 function/def 更新 MDLabel.text
/text-update.text
(类似于 tkinter/Any 其他方式)。谁能帮帮我??
您可以使用 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','Whosebug','Bing']
text=random.choice(site_list)
self.root.ids.text_update.text=(text)
AnsweredOrNot().run()
我已将标签 ID 从 text-update
更改为 text_update