- Kivy Cookbook
- Hugo Solis
- 238字
- 2021-07-16 20:39:55
Reusing styles in multiple widgets
This recipe will teach you how to take advantage of reusing styles for different widgets, a procedure that could be useful for the scalability of a system.
Getting ready
We will use this example of a file, e8.kv
, where two widgets are defined:
<MyWidget1>: Button: on_press: self.text(txt_inpt.text) TextInput: id: txt_inpt <MyWidget2>: Button: on_press: self.text(txt_inpt.text) TextInput: id: txt_inpt
We must note that they are very similar, and actually just the name of the widget is different between them.
How to do it…
The following steps provide a way to join the two widgets:
- Let's conserve just one of the widgets.
- The name of the discarded widget will be added to name of the conserved widget, by separating with a comma:
<MyWidget1,MyWidget2>: Button: on_press: self.text(txt_inpt.text) TextInput: id: txt_inpt
How it works…
In this case, by separating the class names with a comma, all the classes listed in the declaration will have the same KV properties and you could join any number of similar widgets.
There's more…
In the Python code, the widgets could do different tasks, as in the next portion of code:
class MyWidget1(Widget): def text(self, val): print('text input text is: {txt}'.format(txt=val)) class MyWidget2(Widget): writing = StringProperty('') def text(self, val): self.writing = val
Similarly, you can join the widgets in the Kv language.
See also
If you want to get more details about widgets, see the recipes in Chapter 4, Widgets.
- 深入淺出WPF
- HDInsight Essentials(Second Edition)
- Swift Playgrounds少兒趣編程
- jQuery Mobile移動(dòng)應(yīng)用開(kāi)發(fā)實(shí)戰(zhàn)(第3版)
- Unity 3D/2D移動(dòng)開(kāi)發(fā)實(shí)戰(zhàn)教程
- Android移動(dòng)開(kāi)發(fā)案例教程:基于Android Studio開(kāi)發(fā)環(huán)境
- C語(yǔ)言程序設(shè)計(jì)簡(jiǎn)明教程:Qt實(shí)戰(zhàn)
- 持續(xù)集成與持續(xù)交付實(shí)戰(zhàn):用Jenkins、Travis CI和CircleCI構(gòu)建和發(fā)布大規(guī)模高質(zhì)量軟件
- Android Development Tools for Eclipse
- Java EE Web應(yīng)用開(kāi)發(fā)基礎(chǔ)
- TypeScript圖形渲染實(shí)戰(zhàn):2D架構(gòu)設(shè)計(jì)與實(shí)現(xiàn)
- Java 9 with JShell
- Spring Boot從入門(mén)到實(shí)戰(zhàn)
- 打造流暢的Android App
- 深度學(xué)習(xí):基于Python語(yǔ)言和TensorFlow平臺(tái)(視頻講解版)