![](/img/trans.png)
[英]How to change a Label text on another screen by pressing a Button Python Kivy
[英]How can I make in Kivy by clicking a button to change the text of another button(buttons)?
我正在使用 Kivy 框架來創建桌面應用程序。
例如,當您單擊“Eng”按鈕時,它會將按鈕的文本更改為英文。 以此類推,當您單擊名為“Fra”的按鈕時,它會更改為法語。
當您在 Textinput 中輸入一些信息並且單擊 Button 信息出現在 Label 上時,我已經嘗試裝扮該結構。 我只是用 Button 替換了 Label 但我仍然不知道如何使它工作。
我的 test.py 文件
class Container(BoxLayout):
pass
button_test=ObjectProperty()
wbutton_eng=ObjectProperty()
wbutton_fra=ObjectProperty()
def change_text(self):
self.buttontest.text
self.wbutton_fra.text()
self.wbutton_eng.text()
我的KV文件
<Container>:
BoxLayout:
canvas:
Color:
rgb: 120, 1, 1, 0.9
Rectangle:
size: self.size
orientation:'vertical'
button_test:buttonone
wbutton_eng:buttoneng
wbutton_fra:buttonfra
Button:
id:buttonone
text:'[color=ff3333]Text in French or English[/color]'
font_size:24
size_hint:1,0.09
background_color: 0,0,0.5
canvas.before:
Color:
rgba: 1, 2, 0.3, .6
Rectangle:
pos: self.pos
size: self.size
markup: True
Button:
id:buttoneng
text:'[color=ff3333]Eng[/color]'
font_size:24
size_hint:1,0.09
background_color: 0,0,0.5
canvas.before:
Color:
rgba: 1, 2, 0.3, .6
Rectangle:
pos: self.pos
size: self.size
markup: True
on_press
root.change_text()
Button:
id:buttonfra
text:'[color=ff3333]Fra[/color]'
font_size:24
size_hint:1,0.09
background_color: 0,0,0.5
canvas.before:
Color:
rgba: 1, 2, 0.3, .6
Rectangle:
pos: self.pos
size: self.size
markup: True
on_press
root.change_text()
邏輯是創建一個字典,其中包含作為鍵的語言標識符和作為值的要翻譯的文本。 然后將每個按鈕與語言標識符相關聯。
一個簡單的例子如下:
from kivy.app import App
from kivy.lang.builder import Builder
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout
class Container(BoxLayout):
message = StringProperty()
def retranslate(self, language):
texts = {"en": "Hello World", "fr": "Salut monde"}
self.message = texts.get(language, "")
Builder.load_string(
"""
<Container>:
orientation: 'vertical'
Button:
text: root.message
Button:
text: "Eng"
on_press: root.retranslate("en")
Button:
text: "Fra"
on_press: root.retranslate("fr")
"""
)
class MyApp(App):
def build(self):
w = Container()
w.retranslate("en")
return w
if __name__ == "__main__":
MyApp().run()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.