Properly using .kv Files for Kivy and importing them to your Python script(正确使用 Kivy 的 .kv 文件并将它们导入 Python 脚本)
问题描述
我正在关注
有谁知道我做错了什么?这些是我对 /dev/input/event6
crwxrwxr-- 1 根输入 13, 2011 年 9 月 70 日 23:47/dev/input/event6
非常感谢!
根据
I am following tutorials for Kivy and I haven't been able to properly load properties from .kv files. For some reason, I am able to sometimes pull .kv files via a for kv in listdir(kv_path): Builder.load_file(kv_path+kv)
, but I can't do it by simply putting the kv files in the same root directory and letting it "import by itself?"
As an example, when using the files:
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
<Button>:
font_size: 40
size: 170,75
color: 0,1,0,1
<Widgets>:
Button:
pos: root.x, root.top - self.height
text: "Kivy"
Button:
pos: 170,0
text: "Tutorials"
I obtain the following output on my Terminal:
$ 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 <gl>
[INFO ] [GL ] OpenGL version <b'3.0 Mesa 18.0.5'>
[INFO ] [GL ] OpenGL vendor <b'Intel Open Source Technology Center'>
[INFO ] [GL ] OpenGL renderer <b'Mesa DRI Intel(R) HD Graphics P630 (Kaby Lake GT2) '>
[INFO ] [GL ] OpenGL parsed version: 3, 0
[INFO ] [GL ] Shading version <b'1.30'>
[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 </dev/input/event6>
[INFO ] [Base ] Start application main loop
[INFO ] [MTD ] </dev/input/event6> range position X is 1266 - 5676
[INFO ] [MTD ] </dev/input/event6> range position Y is 1094 - 4762
[INFO ] [MTD ] </dev/input/event6> range touch major is 0 - 0
[INFO ] [MTD ] </dev/input/event6> range touch minor is 0 - 0
[INFO ] [MTD ] </dev/input/event6> range pressure is 0 - 255
[INFO ] [MTD ] </dev/input/event6> axes invertion: X is 0, Y is 0
[INFO ] [MTD ] </dev/input/event6> rotation set to 0
The following window shows!
Does anyone know what am I doing wrong? These are my permissions for /dev/input/event6
crwxrwxr-- 1 root input 13, 70 Sep 11 23:47 /dev/input/event6
Thank you very much!
According to the docs you have 2 possibilities:
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.
So you must change the name of the .kv to lowercase, and change the name of class App to end in App:
simplekivy3.kv
<Button>:
font_size: 40
size: 170,75
color: 0,1,0,1
<Widgets>:
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 的 .kv 文件并将它们导入 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:正确使用 Kivy 的 .kv 文件并将它们导入 Python 脚本


基础教程推荐
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01