我是 python 的初学者(也是 kivy 的初学者)。我kivymd4 天前开始学习 kivy(可能)。我学习了它的基础知识。但我遇到了一些问题,在学习 kivy 之前我学习了tkinter。所以我在 tkinter 中给出了我想用 kivymd 来做的例子。
kivymd
tkinter
我 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()
我可以Label使用 def/Function 来更新 /Text,idname.config()以编辑文本并idname.update()更新它。
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','Stackoverflow','Bing'] text=random.choice(site_list) AnsweredOrNot().run()
我想在按下按钮时使用 function/def(如 tkinter/任何其他方式)更新MDLabel.text/ 。有人能帮帮我吗?text-update.text
MDLabel.text
text-update.text
您可以使用 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
text-update
text_update