简体   繁体   中英

How to resize label in kivy to the size of my screen?

Hi I have created this kivy app. I just want to know how can i resize the label to the size of whole screen. And also if i resize the screen with mouse, the label also resizes itself to that screen. I know that to do this we need to use size: root.width, root.height . But that is done in the kivy file. What if i want to do this in my python file itself? Thanks for your suggestions.

Pls refer to the code below:

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget

class MyGrid(Widget):
    def __init__(self, **kwargs):
        super(MyGrid, self).__init__(**kwargs)
        self.inner = GridLayout()
        self.inner.cols = 1
        self.add_widget(self.inner)
        self.inner.add_widget(Label(text = 'Hi!'))


class MyApp(App):
    def build(self):
        return MyGrid()


if __name__ == '__main__':
    MyApp().run()

You can use the Window.height and Window.width properties and on_size method to achieve this. As shown in the below modified code:

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.core.window import Window

class MyGrid(Widget):
    def __init__(self, **kwargs):
        super(MyGrid, self).__init__(**kwargs)
        self.inner = GridLayout()
        self.inner.cols = 1
        self.add_widget(self.inner)
        self.inner.add_widget(Label(text = 'Hi!'))

    def on_size(self, *args):
        self.inner.height = Window.height
        self.inner.width = Window.width

class MyApp(App):
    def build(self):
        return MyGrid()


if __name__ == '__main__':
    MyApp().run()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM