KVLang Series – 3
More Widgets in a BoxLayout
Content:
- .kv file
- .py file
- screenshot of output
0003_moreWidgets.kv
In the .kv file we add some more widgets under a BoxLayout.
If we added the widgets directly to the file, not being underneath our layout, it would be and ERROR because they would be considered root rules and “you can have one root rule, and any number of class or template rules.”
The BoxLayout has a default orientation: ‘horizontal’.
### 0003_moreWidgets.kv BoxLayout: Label: text: 'Input:' TextInput: text: 'Default Text' Button: text: 'Press Me'
0003_moreWidgets.py
This is adapted from previous python file. We make minor changes to update the .kv filename and give a new window height.
- EDIT
CHANGE: The OLD self.root = Builder.load_file(”) TO the NEW
-
EDIT
WAS: Config.set(‘graphics’, ‘height’, ‘200’)
NOW: Config.set(‘graphics’, ‘height’, ’30’)
''' 0003_moreWidgets.py Used to display 0003_moreWidgets.kv - No new concepts in here. ''' import kivy kivy.require('1.8.0') # replace with your current kivy version ! from kivy.app import App from kivy.lang import Builder from kivy.config import Config Config.set('graphics', 'width', '323') Config.set('graphics', 'height', '30') class MyApp(App): def build(self): self.root = Builder.load_file('0003_moreWidgets.kv') return self.root if __name__ == '__main__': MyApp().run()
0003_moreWidgets ScreenShot
Here is what this looks like run on Windows XP. In Pixels, it has:
- width: 323
- height: 200