我正在追踪tutorials for Kivy,但无法从.kv文件正确加载属性.出于某种原因,有时我可以通过listdir(kv_path)中的for kv拉.kv文件:Builder.load_file(kv_path kv),但是我不能仅仅通过将kv文件放在相同的根目录中并让它“自己导入?”
例如,使用文件时:
main.py
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
class Widgets(Widget):
pass
class SimpleKivy3(App):
def build(self):
return Widgets()
if __name__ == "__main__":
SimpleKivy3().run()
SimpleKivy3.kv
:
font_size: 40
size: 170,75
color: 0,1,0,1
:
Button:
pos: root.x, root.top - self.height
text: "Kivy"
Button:
pos: 170,0
text: "Tutorials"
我在终端上获得以下输出:
$python main.py
[INFO ] [Logger ] Record log in /home/nickshu/.kivy/logs/kivy_18-09-12_58.txt
[INFO ] [Kivy ] v1.11.0.dev0, git-038acbf, 20180912
[INFO ] [Python ] v3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56)
[GCC 7.2.0]
[INFO ] [Factory ] 195 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2
[INFO ] [Window ] Provider: sdl2(['window_egl_rpi'] ignored)
[INFO ] [GL ] Using the "OpenGL" graphics system
[INFO ] [GL ] Backend used
[INFO ] [GL ] OpenGL version
[INFO ] [GL ] OpenGL vendor
[INFO ] [GL ] OpenGL renderer
[INFO ] [GL ] OpenGL parsed version: 3, 0
[INFO ] [GL ] Shading version
[INFO ] [GL ] Texture max size <16384>
[INFO ] [GL ] Texture max units <32>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [ProbeSysfs ] device match: /dev/input/event6
[INFO ] [MTD ] Read event from
[INFO ] [Base ] Start application main loop
[INFO ] [MTD ] range position X is 1266 - 5676
[INFO ] [MTD ] range position Y is 1094 - 4762
[INFO ] [MTD ] range touch major is 0 - 0
[INFO ] [MTD ] range touch minor is 0 - 0
[INFO ] [MTD ] range pressure is 0 - 255
[INFO ] [MTD ] axes invertion: X is 0, Y is 0
[INFO ] [MTD ] rotation set to 0
显示以下窗口!
有人知道我在做什么错吗?这些是我对/ dev / input / event6的许可
crwxrwxr-1个root输入13,70 Sep 11 23:47 / dev / input / event6
非常感谢你!
解决方法:
根据docs,您有2种可能性:
1.
By name convention:
Kivy looks for a Kv file with the same name as your App class in lowercase, minus “App” if it ends with ‘App’ e.g:
MyApp -> my.kv
If this file defines a Root Widget it will be attached to the App’s root attribute and used as the base of the application widget tree.
因此,您必须将.kv的名称更改为小写,并将App类的名称更改为以App结尾:
simplekivy3.kv
:
font_size: 40
size: 170,75
color: 0,1,0,1
:
Button:
pos: root.x, root.top - self.height
text: "Kivy"
Button:
pos: 170,0
text: "Tutorials"
*的.py
from kivy.app import App
from kivy.uix.widget import Widget
class Widgets(Widget):
pass
class SimpleKivy3App(App):
def build(self):
return Widgets()
if __name__ == "__main__":
SimpleKivy3App().run()
2.
Builder: You can tell Kivy to directly load a string or a file. If
this string or file defines a root widget, it will be returned by the
method:
Builder.load_file(‘path/to/file.kv’)
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
class Widgets(Widget):
pass
Builder.load_file('SimpleKivy3.kv')
class SimpleKivy3(App):
def build(self):
return Widgets()
if __name__ == "__main__":
SimpleKivy3().run()
标签:kivy,python