简体   繁体   English

无效语法 - kivy

[英]invalid syntax - kivy

Learning kivy for the first time and faced a problem while starting .kv file.第一次学习kivy,在启动.kv文件时遇到问题。

main.py :主要.py

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout

class Grid(Widget):
    pass

class App(App):
    def build(self):
        return Grid()

if __name__ == '__main__':
    App().run()

file.kv :文件.kv

#:kivy 2.0.0
<Grid>:
    Label:
        text: "Hello World!"

error code :错误代码

Traceback (most recent call last):
  File "C:\Users\megaa\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\megaa\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "c:\Users\megaa\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\lib\python\debugpy\__main__.py", line 45, in <module>
    cli.main()
  File "c:\Users\megaa\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 444, in main        
    run()
  File "c:\Users\megaa\.vscode\extensions\ms-python.python-2020.12.424452561\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 285, in run_file    
    runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
  File "C:\Users\megaa\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 267, in run_path
    code, fname = _get_code_from_file(run_name, path_name)
  File "C:\Users\megaa\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 242, in _get_code_from_file
    code = compile(f.read(), fname, 'exec')
  File "d:\python\kivy\tutorial\tutorial2\file.kv", line 1
    <Grid>:
    ^
SyntaxError: invalid syntax

Remember that your.kv file is an design file.请记住,您的.kv 文件是设计文件。 we have to load it in the python script.我们必须在 python 脚本中加载它。

In order to do that, Please do following in main.py为此,请在main.py中执行以下操作

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder # new line added 

class Grid(Widget):
    pass

class App(App):
    def build(self):
        return Builder.load_file('file.kv') # change here

if __name__ == '__main__':
    App().run()

Now.kv file is loaded and should provide expected output. Now.kv 文件已加载,应提供预期的 output。

Happy Learning快乐学习

Edit 1:编辑1:

Remove brackets and it should be running.删除括号,它应该正在运行。 In file.kv文件.kv

#:kivy 2.0.0
Grid:
    GridLayout:
        cols: 1
        size: root.width, root.height

        Label:
            text: "Hello World!"

Edit 2: My main.py file:编辑 2:我的main.py文件:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder # new line added 

class Grid(Widget):
    pass

class App(App):
    def build(self):
        return Builder.load_file('file.kv') # change here

if __name__ == '__main__':
    App().run()

Output Kivy window: :[output_kivy_window][https.//imgur.com/a/819K20A] Output Kivy window::[output_kivy_window][https.//imgur.com/a/819K20A]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM