gpt4 book ai didi

android - 绑定(bind)多个语句以在 kivy 中尝试表达式

转载 作者:行者123 更新时间:2023-11-30 02:03:39 25 4
gpt4 key购买 nike

我想对 kv 文件中的 kivy button 按钮执行与以下代码相同的操作

try:
print"first action"
print"second action"
except:
print"there was an error"

这是我想要在其中执行该按钮的 kv 文件和按钮我收到错误消息:

on_release: 
try: \
print("first action")
#first action is working but second is not working
print('second action'):

except:\
print("it is not working")

它给我这样的错误:

[INFO   ] [Logger      ] Record log in /root/.kivy/logs/kivy_15-06-28_29.txt
[INFO ] [Kivy ] v1.9.0
[INFO ] [Python ] v2.7.3 (default, Jan 2 2013, 16:53:07)
[GCC 4.7.2]
[INFO ] [Factory ] 173 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_gif, img_pygame, img_pil (img_ffpyplayer ignored)
[INFO ] [Window ] Provider: pygame(['window_egl_rpi'] ignored)
[WARNING] [WinPygame ] Video: failed (multisamples=2)
[WARNING] [WinPygame ] trying without antialiasing
[INFO ] [GL ] OpenGL version <2.1 Mesa 8.0.5>
[INFO ] [GL ] OpenGL vendor <VMware, Inc.>
[INFO ] [GL ] OpenGL renderer <Gallium 0.4 on SVGA3D; build: RELEASE; >
[INFO ] [GL ] OpenGL parsed version: 2, 1
[INFO ] [GL ] Shading version <1.20>
[INFO ] [GL ] Texture max size <8192>
[INFO ] [GL ] Texture max units <16>
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [Text ] Provider: pygame
Traceback (most recent call last):
File "main.py", line 128, in <module>
ExampleApp().run()
File "/usr/local/lib/python2.7/dist-packages/kivy/app.py", line 797, in run
self.load_kv(filename=self.kv_file)
File "/usr/local/lib/python2.7/dist-packages/kivy/app.py", line 594, in load_kv
root = Builder.load_file(rfilename)
File "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 1749, in load_file
return self.load_string(data, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 1796, in load_string
parser = Parser(content=string, filename=fn)
File "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 1185, in __init__
self.parse(content)
File "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 1291, in parse
rule.precompile()
File "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 1053, in precompile
x.precompile()
File "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 1053, in precompile
x.precompile()
File "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 1053, in precompile
x.precompile()
File "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 1053, in precompile
x.precompile()
File "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 1053, in precompile
x.precompile()
File "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 1053, in precompile
x.precompile()
File "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 1051, in precompile
x.precompile()
File "/usr/local/lib/python2.7/dist-packages/kivy/lang.py", line 976, in precompile
self.co_value = compile(value, self.ctx.filename or '<string>', mode)
File "./example.kv", line 180
print("second action")
^
SyntaxError: invalid syntax

那么有什么解决方案吗?在 try 语句后做多件事?

最佳答案

我认为 kv lang 不支持异常。将代码移至 Python 类:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder

Builder.load_string("""
<Test>:
Button:
text: "..."
on_press: root.react()
""")

class Test(FloatLayout):
def react(self):
try:
print("first action")
print("second action")
except:
print("there was an error")

class TabbedPanelApp(App):
def build(self):
return Test()

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

简单地打印几次很容易:

Builder.load_string("""
<Test>:
Button:
text: "..."
on_press:
print("first action")
print("second action")
""")

关于android - 绑定(bind)多个语句以在 kivy 中尝试表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31102475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com