Kivy:我在主循环上添加功能时出现黑屏

时间:2018-08-19 02:08:07

标签: python kivy

def Lnlau(self): #LNLAU = Load_Nodeid_List_And_Update
    FRM = open('NodeidList.txt', 'r') #FileReadMode

    while True:
    for i, l in enumerate(FRM):
        if (i)%2 == 1: #1 , 3 ,5 ,7...
            l_new = l.rstrip('\n')
            FSM = l_new

            linecontents = BusInfo_Update()
            linecontents.Update(FSM) #BusInfo_Update().Update(FSM) eg) FSM = 'ICB23221.txt' 
        else:
            pass

当我启动应用程序时,我想在此功能之上启动。 所以我将此功能添加到class Loading_Screen(Screen) 并使用Clock.schedule_once(self.Lnlau, 0),但我只能在屏幕上看到的是黑屏。没有错误。如果我在main.py上取消了此功能,则没有问题。我该怎么办?使新类使用此功能?

main.py

__version__ = "1.0"
#-*- coding: utf-8 -*-

import kivy
import os
kivy.require('1.10.0')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.animation import Animation
from kivy.clock import Clock
#from kivy.config import Config #windows size fixed
#Config.set('graphics', 'resizable', 0)
from kivy.core.window import Window 
Window.size = (1080, 1920) 
##########FOR BUS INFORMATION UPDATE#############
from urllib import urlencode, quote_plus
from urllib2 import Request as RQ
from urllib2 import urlopen as UO
import urllib
import xml.etree.ElementTree as etree
import os
import datetime

class Loading_Screen(Screen):
    def __init__(self, **kwargs):
        super(Loading_Screen, self).__init__(**kwargs)
        Clock.schedule_once(self.callNext, 2) 

    def callNext(self,dt): 
        self.manager.current = 'Before_First_Screen'

class Load_BusInfo():
    pass

class Before_First_Screen(Screen):
    pass

class First_Screen(Screen):
    pass

class Test_Screen(Screen):
    pass

###################destination_station_name#####################

class DTN_TECHNOPARK(Screen): 
    pass

class DTN_BITZONE(Screen): 
    pass

class DTN_INUSTATION(Screen): 
    pass

class Bus_Information(Screen):
    def __init__(self, **kwargs):
        super(Bus_Information, self).__init__(**kwargs)
    #oo = os.path.join('/data/data/org.test.tubuc/files/app/station/', 'ICB164000039.txt')
    oo = os.path.join('/root/hi/station/', 'ICB164000039.txt')
        with open(oo) as businfo:
            Businfo= [] 
            nolinenum=businfo.readline()
            while nolinenum!='': 
                Businfo.append(nolinenum)
                leftstations = (businfo.readline().rstrip('\n') + ' stations'.rstrip('\n'))
                lefttime = (businfo.readline().rstrip('\n') + ' seconds'.rstrip('\n'))
                nolinenum = businfo.readline().rstrip('\n')
                Businfo.append(leftstations)
                Businfo.append(lefttime)
            self.businfolist = Businfo
            self.lenbil = int(len(Businfo))
            self.numberoflist = int(len(Businfo)/3)

class ScreenManagement(ScreenManager):
    pass

presentation = Builder.load_file("main.kv")

class TubucApp(App):
    def build(self):
        return presentation


TubucApp().run()

main.kv

# -*- coding: utf-8 -*-
#:import NoTransition kivy.uix.screenmanager.NoTransition
#:import Label kivy.uix.button.Label

ScreenManagement:
    transition: NoTransition()
    Loading_Screen:
    Before_First_Screen:
    First_Screen:
    Test_Screen:
    DTN_TECHNOPARK:
    DTN_BITZONE:
    DTN_INUSTATION:
    Bus_Information:

<Loading_Screen>:
    name: 'Loading_Screen'
    canvas:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'image/Loading_Screen.png'

<Before_First_Screen>:
    name: 'Before_First_Screen'
    Button:
        canvas:
            Rectangle:
                pos: self.pos
                size: self.size
                source: 'image/BBG2.png'
        on_release:
            app.root.current = 'First_Screen'

<First_Screen>:
    name: 'First_Screen'

    BoxLayout:
        orientation: 'horizontal'
        spacing: 50 # spacing between button
        padding: [50, 50, 50, 50]
        canvas:
            Rectangle:
                pos: self.pos
                size: self.size 
                source: 'image/background.png' #backgroundimage
        Button:
            id: dongmak
            width: 40
            pos_hint: {'x' : 0, 'y':.45}
            size_hint: [.6,.1]
            background_normal:'image/2.png'
            background_down:'image/after.png'
            border: (0,0,0,0)
            on_release: app.root.current = 'Bus_Information'
        Button:
            id: campustown
            color: 100,100,100,100 #text_color
            width: 40
            pos_hint: {'x' : 0, 'y':.45}
            size_hint: [.6,.1]
            background_normal:'image/3.png'
            background_down:'image/after.png'
            border: (0,0,0,0)
            on_release: app.root.current = 'DTN_BITZONE'
        Button:
            id: technopark
            color: 100,100,100,100 #text_color
            pos_hint: {'x' : 0, 'y':.45}
            size_hint: [.6,.1]
            background_normal:'image/4.png'
            background_down:'image/after.png'
            border: (0,0,0,0)
            on_release: app.root.current = 'DTN_INUSTATION'
        Button:
            id: bitzone
            width: 40
            pos_hint: {'x' : 0, 'y':.45}
            size_hint: [.6,.1]
            background_normal:'image/5.png'
            background_down:'image/after.png'
            border: (0,0,0,0)   
        Button:
            id: inustation
            width: 40
            pos_hint: {'x' : 0, 'y':.45}
            size_hint: [.6,.1]
            background_normal:'image/1.png'
            background_down:'image/after.png'
            border: (0,0,0,0)
            #font_size: 15
            #text: 'inustation'
<Test_Screen>:
    name: 'Test_Screen'

    Button:
        on_release: app.root.current = 'First_Screen'
        text: 'back to the home screen'
        font_size: 50

############destionation_station_name################
<DTN_TECHNOPARK>:
    name: 'DTN_TECHNOPARK'

    Button:
        on_release: app.root.current = 'First_Screen'
        text: 'destionation >> technopark'
        font_size: 50

<DTN_BITZONE>:
    name: 'DTN_BITZONE'

    Button:
        on_release: app.root.current = 'First_Screen'
        text: 'destionation >> bitzone'
        font_size: 50

<DTN_INUSTATION>:
    name: 'DTN_INUSTATION'

    Button:
        on_release: app.root.current = 'First_Screen'
        text: 'destionation >> inustation'
        font_size: 50
######################################################
<Bus_Information>:
    name: 'Bus_Information'
    canvas:
        Rectangle:
            pos: self.pos
            size: self.size 
            source: 'image/BBG.png' #backgroundimage

    #for sero in range(root.numberoflist):
    #   for garo in range(3):


    GridLayout:
        cols: 3
        rows: root.numberoflist
        spacing: 0
        padding: [0,100]
        on_parent:
            for i in range(root.lenbil): txt = root.businfolist[i]; self.add_widget(Label(text = txt, text_size=(cm(2), cm(2)), 
            pos=self.pos, id=txt))

1 个答案:

答案 0 :(得分:1)

问题:Kivy应用中的无尽循环

运行While True:函数中包含Lnlau(self)的Kivy App时,该程序将永远不会退出循环,从而阻止Kivy执行所有其他需要做的事情。结果,您将看到的是一个黑色窗口,您将无法与之交互。

解决方案

使用Clock.schedule_interval()Clock.create_trigger()函数来“安排”您的Lnlau()函数,以使其反复调用。