M.Wagner
M.Wagner

Reputation: 100

Kivy not showing Window running on Raspberry Pi 3

I'm trying to run my Kivy application on a Raspberry Pi.
The usecase is to display a interface for an RFID controlled coffee machine.
The scanning part Works and everything expcect the interface works aswell since I tested it with TKinter, before switching to Kivy.

Here is my code:

import RPi.GPIO as GPIO     #Access to the Raspberry Pi GPIO Pins
import serial               #PySerial for the serial connection to the RFID reader
import time                 #Required for sleep function
import datetime             #Required for timestamp
import MySQLdb as SQL       #Connection to MySQL Database
import threading            #Threading for RFID detection

from subprocess import call #Um die Datei in einem neuen Thread aufzurufen

import kivy                #GUI
kivy.require('1.10.0')

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder


#GPIO Setup
GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.OUT)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

GPIO.output(18, GPIO.HIGH)

#MySQL DB Connection
db = SQL.connect("localhost", "root", "raspberry", "Coffee")
curs = db.cursor()

#Select NTUser by the read ID from the MySQL Database
def getID(readID):
    try:
        curs.execute("SELECT NTUSER FROM usrlist WHERE id=%s", readID)
        usr = curs.fetchone()
        return usr[0]
    except:
        print("User not registered")


def put_into_db():
    #Input data into the Database
    print('done')



def espresso():
    print('Espresso')

def coffee():
    print('Coffee')



def scanner_thread():
    call(["python", "/RFID_Coffeemachine/RFID_Scanner.py", "&"])

#Thread for RFID reading
thread = threading.Thread(target=scanner_thread)
thread.start()

class Coffee(Screen):
    def __init__ (self,**kwargs):
    #GUI
        layout_coffee = BoxLayout(orientation="vertical")
        header_coffee = BoxLayout(orientation="horizontal")
        body_coffee = BoxLayout(orientation="horizontal")
        footer_coffee = BoxLayout(orientation="horizontal")



        self.greeting = Label(
            text='Welcome to the config screen',
            size_hint=(.5, .25))                        #Greeting Label
        def get_login(self):                                  #Function to update the greeting label -> Later integration with the user DB
            login_file = open('/home/pi/login.txt', "r")
            current_login = login_file.readline()
            login_file.close()
            if current_login == ' ':
                self.greeting.text='Please Scan RFID Token'
            else:
                self.greeting.text = current_login

        body_coffee.add_widget(self.greeting)                   #Adding the Label to the Layout
        Clock.schedule_interval(get_login, 1)              #Updating the greeting text every second

        layout_coffee.add_widget(header_coffee)
        layout_coffee.add_widget(body_coffee)
        layout_coffee.add_widget(footer_coffee)

class Config(Screen):
    def __init__ (self,**kwargs):
    #GUI
        layout_config = BoxLayout(orientation="vertical")
        header_config = BoxLayout(orientation="horizontal")
        body_config = BoxLayout(orientation="horizontal")
        footer_config = BoxLayout(orientation="horizontal")

        greeting = Label(
            text='Welcome to the config screen',
            size_hint=(.5, .25))                           #Greeting Label
        body_config.add_widget(greeting)                   #Adding the Label to the Layout

        layout_config.add_widget(header_config)
        layout_config.add_widget(body_config)
        layout_config.add_widget(footer_config)

class GUIApp(App):
    def build(self):

        screens = ScreenManager()

        coffee = Coffee(name="coffee")
        config = Config(name="config")

        screens.add_widget(coffee)
        screens.add_widget(config)

        return screens  



if __name__ == "__main__":
    GUIApp().run()
#thread.stop()
#db.close()
#ser.close()

I'm pretty sure that I did the imports and the "__main__" right, which were the issue in the posts with similar problems. I'm also pretty sure that the installation of Kivy worked properly on the Pi, since the Demo applications run fine.

Upvotes: 1

Views: 675

Answers (1)

M.Wagner
M.Wagner

Reputation: 100

So it seems like I had some things missing:

For the Screens to work I need to add:
def __init__ (self,**kwargs): after each Screen class and put all the layouts and widgets in there.

Also I forgot to add the Layouts to the screens:
self.add_widget(layout_coffee)at the very end of the __init__

Upvotes: 1

Related Questions