Python | PageLayout in Kivy
Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux, and Windows, etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.
Kivy Tutorial – Learn Kivy with Examples.
PageLayout:
The PageLayout works in a different manner from other layouts. It is a dynamic layout, in the sense that it allows flipping through pages using its borders. The idea is that its components are stacked in front of each other, and we can just see the one that is on top.
The PageLayout class is used to create a simple multi-page layout, in a way that allows easy flipping from one page to another using border.
To use PageLayout you have to import it by the below command:
from kivy.uix.pagelayout import PageLayout
Note:
PageLayout does not currently honor the size_hint, size_hint_min, size_hint_max, or pos_hint properties.That means we can not use all these in a page layout.
Basic Approach to create PageLayout:
1) import kivy
2) import kivyApp
3) import Pagelayout
4) import button
5) Set minimum version(optional)
6) create App class:
- define build() function
7) return Layout/widget/Class(according to requirement)
8) Run an instance of the class
Implementation of the Approach:
Python3
import kivy
from kivy.app import App
from kivy.uix.pagelayout import PageLayout
from kivy.uix.button import Button
class PageLayout(PageLayout):
def __init__( self ):
super (PageLayout, self ).__init__()
btn1 = Button(text = 'Page 1' )
btn2 = Button(text = 'Page 2' )
btn3 = Button(text = 'Page 3' )
self .add_widget(btn1)
self .add_widget(btn2)
self .add_widget(btn3)
class Page_LayoutApp(App):
def build( self ):
return PageLayout()
if __name__ = = '__main__' :
Page_LayoutApp().run()
|
Output:
Page 1 image
Page 2 image
Page 3 image
In PageLayout You can add some features on every page. We can add image, create canvas, add color, add multiple widgets, multiple layouts
This is how we can use the PageLayout in an efficient way. One of the best example Our gallery Contains multiple pages.
Below is the code in which i am adding the different color to every page with the help of get_color_from_hex
Implementation of the PageLayout with features
Python3
import kivy
from kivy.app import App
from kivy.uix.pagelayout import PageLayout
from kivy.uix.button import Button
from kivy.utils import get_color_from_hex
class PageLayout(PageLayout):
def __init__( self ):
super (PageLayout, self ).__init__()
btn1 = Button(text = 'Page 1' )
btn1.background_color = get_color_from_hex( '# FF0000' )
btn2 = Button(text = 'Page 2' )
btn2.background_color = get_color_from_hex( '# 00FF00' )
btn3 = Button(text = 'Page 3' )
btn3.background_color = get_color_from_hex( '# 0000FF' )
self .add_widget(btn1)
self .add_widget(btn2)
self .add_widget(btn3)
class Page_LayoutApp(App):
def build( self ):
return PageLayout()
if __name__ = = '__main__' :
Page_LayoutApp().run()
|
Output:
Page 1
Page 2
Page 3
Video Output:
Note: More effective when works on Android, Ios, any other touch supported Laptops.
Reference: https://kivy.org/doc/stable/api-kivy.uix.pagelayout.html
Last Updated :
19 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...