python用变量输出abcd,将变量从.py传递到.kv文件

I am trying to use variables from python file to .kv file

so I searched similar questions and found out the way use Property and coded like this:

# in python file

class Test2App(App):

abcd = StringProperty('test')

def build(self):

return presentation

# in kv file

:

text: "contents (%s)"%(app.abcd)

background_color: (255, 255, 255,1)`

and an error comes up.

AttributeError: 'NoneType' object has no attribute 'bind'

File "/usr/local/lib/python2.7/dist-packages/kivy/lang/builder.py", line 249, in create_handler

return eval(value, idmap), bound_list

File "/root/Desktop/hi/t2.kv", line 61, in

text: "contents (%s)"%(app.abcd)

File "/usr/local/lib/python2.7/dist-packages/kivy/lang/parser.py", line 75, in __getattribute__

object.__getattribute__(self, '_ensure_app')()

File "/usr/local/lib/python2.7/dist-packages/kivy/lang/parser.py", line 70, in _ensure_app

app.bind(on_stop=lambda instance:

File "/usr/local/lib/python2.7/dist-packages/kivy/lang/builder.py", line 615, in _apply_rule

rctx['ids'])

File "/usr/local/lib/python2.7/dist-packages/kivy/lang/builder.py", line 254, in create_handler

cause=tb)

t2.py

#-*- coding: utf-8 -*-

__version__ = "1.0"

import kivy

import os

kivy.require('1.10.0')

from kivy.app import App

from kivy.lang import Builder

from kivy.uix.button import Button

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.properties import StringProperty

#from kivy.config import Config #windows size fixed

#Config.set('graphics', 'resizable', 0)

from kivy.core.window import Window

Window.size = (540, 960)

#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

def oopath(ndid, uor):

path = os.path.join(ndid + '.txt')

return path

##############################################################################################################

class StationTest(Screen):

def __init__(self, **kwargs):

super(StationTest, self).__init__(**kwargs)

oo = oopath('TESTTEST', 0)

self.rpandgv(oo)

def rpandgv(self,path):

with open(path) 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("t2.kv")

class Test2App(App):

abcd = StringProperty('test')

def build(self):

return presentation

Test2App().run()

t2.kv

# -*- coding: utf-8 -*-

#:import NoTransition kivy.uix.screenmanager.NoTransition

#:import SlideTransition kivy.uix.screenmanager.SlideTransition

#:import Label kivy.uix.button.Label

ScreenManagement:

transition: SlideTransition(direction='left')

StationTest:

:

name: 'StationTest'

canvas:

Rectangle:

pos: self.pos

size: self.size

source: 'image/background.png' #backgroundimage

header: _header

ScrollView:

FloatLayout:

size_hint_y: None

height: 500

BoxLayout:

id: _header

orientation: 'vertical'

size_hint: 1, 0.10

pos_hint: {'top': 1.0}

anchor: _anchor

canvas:

Color:

rgba: 0.8, 0.6, 0.4, 1.0

Rectangle:

pos: self.pos

size: self.size

Label:

text: "STATION > STATION"

font_size: 40

BoxLayout

id: _anchor

size_hint_y: 0.3

canvas.before:

Color:

rgba: 0.3, 0.5, 0.8, 1.0

Rectangle:

pos: self.pos

size: self.size

Label:

text: "TEST1234"

BoxLayout:

orientation: 'vertical'

size_hint: 1, 0.35

padding: 0, -200, 0, 0

MyButton:

MyButton:

MyButton:

MyButton:

:

text: "contents (%s)"%(app.abcd)

background_color: (255, 255, 255,1)

解决方案

There are two solutions to this problem. Please refer to the solutions and example for details.

Solution 1: kv file - Using If statement

The variable, abcd is None when the kv file is parsed. Add if...else... statement to solve the problem.

Snippet - kv file

:

text: "" if app.abcd is None else "contents (%s)"%(app.abcd)

background_color: (255, 255, 255,1) # white background color

color: 0, 0, 0, 1 # black color text

Solution 2: Python code - Initialize in build() method

Initialize variable, abcd in the build() method.

Snippet - Python code

class TestApp(App):

abcd = StringProperty('')

def build(self):

self.abcd = 'test'

return ScreenManagement()

Example

main.py

from kivy.app import App

from kivy.uix.screenmanager import ScreenManager, Screen

from kivy.properties import StringProperty

class StationTest(Screen):

pass

class ScreenManagement(ScreenManager):

pass

class TestApp(App):

abcd = StringProperty('test')

def build(self):

return ScreenManagement()

TestApp().run()

test.kv

#:kivy 1.11.0

#:import SlideTransition kivy.uix.screenmanager.SlideTransition

:

transition: SlideTransition(direction='left')

StationTest:

:

name: 'StationTest'

BoxLayout:

orientation: 'vertical'

size_hint: 1, 0.35

padding: 0, -200, 0, 0

MyButton:

MyButton:

MyButton:

MyButton:

:

# text: "" if app.abcd is None else "contents (%s)"%(app.abcd)

text: "contents (%s)"%(app.abcd)

background_color: (1, 1, 1, 1) # white background

color: 0, 0, 0, 1 # black color text

Output

jW1IB.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值