Igor Dragushhak
Igor Dragushhak

Reputation: 637

Why am I getting a blank screen by running my py and kv script?

When I start the program it gives me a blank, black screen. I used the python file for the functionality and the kv file for the properties of the objects. Py file:

import kivy
kivy.require('1.9.0')

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout


class FloatLayoutApp(App):   

    def build(self):
        return FloatLayout()

FlApp = FloatLayoutApp()
FlApp.run()

kv file:

<CustButton@Button>:
    font_size: 32
    color: 0, 0, 0, 1
    size: 150, 50
    background_normal: ""
    background_down: "bg-grey.jpg"
    background_color: .88, .88, .88, 1
    size_hint: .4, .3

<FloatLayout>:
    CustButton:
        text: "Top Left"
        pos_hint: {"x": 0, "top": 0}
    CustButton:
        text: "Bottom Left"
        pos_hint: {"right": 1, "y": 0}
    CustButton:
        text: "Top Right"
        pos_hint: {"right": 1, "top": 1}
    CustButton:
        text: "Bottom Left"
        pos_hint: {"left": 1, "bottom": 0}
    CustButton:
        text: "Center"
        pos_hint: {"center_x": 0, "center_y": 0}

Upvotes: 1

Views: 1170

Answers (1)

ikolim
ikolim

Reputation: 16031

You are getting a black / blank screen because in the build method it is returning a FloatLayout widget as the root and there are no widgets added to the root.

Kv language » How to load KV

There are two ways to load Kv code into your application:

By name convention:

Kivy looks for a Kv file with the same name as your App class in lowercase, minus “App” if it ends with ‘App’ e.g:

MyApp -> my.kv

If this file defines a Root Widget it will be attached to the App’s root attribute and used as the base of the application widget tree.

By Builder: You can tell Kivy to directly load a string or a file. If this string or file defines a root widget, it will be returned by the method:

Builder.load_file('path/to/file.kv')

or:

Builder.load_string(kv_string)

Solution

There are a few solutions to the problem.

Method 1

Rename your kv file to floatlayout.kv

Method 2

kv file

Replace class rule, <FloatLayout>: with root rule, FloatLayout:

Python Script

  1. Add import statement, from kivy.lang import Builder
  2. Replace return FloatLayout() with return Builder.load_file('kv-filename.kv')

Output

Img01 - Method 1 Img02 - Method 2

Upvotes: 1

Related Questions