Kivy没有在Raspberry Pi 3上显示Window运行

时间:2018-01-25 09:43:47

标签: python user-interface raspberry-pi kivy

我试图在Raspberry Pi上运行我的Kivy应用程序。
用例是显示RFID控制咖啡机的接口。
扫描部分工作,一切都说明界面工作,因为我在使用TKinter测试之前,切换到Kivy。

这是我的代码:

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()

我非常确定我执行了imports"__main__"权利,这是有类似问题的帖子中的问题。 我也很确定Kivy的安装在Pi上运行正常,因为演示应用程序运行良好。

1 个答案:

答案 0 :(得分:0)

所以看起来我有些东西不见了:

要使屏幕正常工作,我需要添加:
在每个Screen类之后def __init__ (self,**kwargs):并将所有布局和小部件放在那里。

我也忘了将布局添加到屏幕上:
self.add_widget(layout_coffee)

的最后__init__