Search code examples
pythonkivyraspberry-pi3

Kivy/Python code displaying white screen on rpi touchscreen


I'm having some trouble with some code I wrote that appears to work on the desktop, but when copied to my raspberry pi/touchscreen, I get a purely blank screen. Rpi has kivy installed. Kivy demos work just fine on both systems. Is there a chance there is something with import Window on kivy? Any advice would be greatly appreciated!

EDIT 1: I read online of someone else with a similar issue, needed to add (from kivy.lang.builder import Builder) and (Builder.load_file.py). I added this in the python code, which is letting me see most of the original program. Some of Kivy is showing, though for some reason- the graphic menu.png is missing and so is the textbox/grid layout at the bottom of the page (bottom of the kivy code below). This may be due to my positioning, so I will look into this.

Edit 2: This should be resolved- it was implementing Builder to the python code (correct version seen below). I also discovered- the raspberry pi might have an issue with images named 'menu.png'... Just changed the name of the image and everything was visible. No idea on that one. Thanks for the assistance.

Python Code:

from time import sleep

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.graphics import Color, Rectangle
from kivy.lang.builder import Builder

Builder.load_file('my.kv')


class MyFloatLayout(Widget):

    def formula(self, num):
        print (num)
        self.ids.formula_input.text = ''
        self.ids.formula_input.text = f'{num}'       

    def press(self):
        loaded = self.ids.formula_input.text
        loadnum = int(loaded)

        if loaded == "":
            print(f'Please enter a formula to continue!')
            self.ids.disp_stat.text = f'Please enter a formula'
            
        else:
            print(f'Dispensing Formula {loaded}!')
            self.ids.disp_stat.text = f'Dispensing Formula #{loaded}!'
            self.ids.formula_input.text = ''         
        
class MyApp(App):

    def build(self):
        Window.clearcolor = (1,1,1,1)
        return MyFloatLayout()
 
if __name__ == '__main__':
    MyApp().run()

Kivy Code:

(sideways carrot)MyFloatLayout(sideways carrot)

disp_stat:disp_stat
formula_input:formula_input

FloatLayout:
    size: root.width, root.height

    Label:
        text: "Welcome To Sharp!"
        color: (0,0,0,1)
        font_size: 40
        size_hint: (0.5,0.2)
        pos_hint: {"x":0.2, "top":1}

    Image:
        padding:200
        source: 'mylogo.png'
        size_hint: (0.5,0.25)
        pos_hint: {"x":.6, "top":1}
        allow_stretch: True
        keep_ratio: True
        
    Image:
        padding:200
        source: 'funimg.png'
        size_hint: (.5, .5)
        pos_hint: {"x":0, "top":.75}
        allow_stretch: True
        keep_ratio: True
        
    GridLayout:
        cols:1
        size_hint: (.4,.5)
        pos_hint: {"x":0.55, "top":.8}

        Label:
            text: "Select Your Formula:"
            font_size: 24
            color: (0,0,0,1)
            
        GridLayout:
            cols:2
            size_hint: (1,1)

            Button:
                text: "Relax"
                on_press: root.formula(1)

            Button:
                text: "Shemax"
                on_press: root.formula(2)

            Button:
                text: "Memax"
                on_press: root.formula(3)

            Button:
                text: "Bebax"
                on_press: root.formula(4)
                          
        Button:
            text: "Initialize Dispense!"
            font_size: 24       
            size_hint: (.2,.3)
            on_press: root.press()

    GridLayout:
        cols:2
        size_hint: (0.4,0.07)
        pos_hint: {"x":.05, "y":.06}

        Label:
            id:disp_stat
            text: "Formula to dispense:"
            color: (0,0,0,1)
            font_size: 12

        TextInput:
            id:formula_input
            text: ""
            font_size:12
            size_hint: (0.5,0.07)

Solution

  • Had the same issue. Solved it by increasing the memory dedicated to the GPU on the Raspberry Pi.

    Edit your /boot/config.txt file (sudo required)

    Scroll down to the [all] section, and edit the gpu_mem line as follows:

    gpu_mem=256

    Then reboot your pi.