Python kivy 在 kivy 中重写 python 的问题
我在以 kivy 格式重写 python 代码时遇到一些问题
这是“设置”类,它具有更改屏幕的方法。
class SettingsMenu(Screen):
def on_touch_down(self, touch):
if touch.button == 'right':
self.parent.transition.direction = "right"
self.parent.transition.duration = 0.6
self.parent.current = "MainMenu"
我想用这种方式(或类似的方式)用kivy重写它:
<SettingsMenu>
name: "SettingsMenu"
on_touch_down:
if button == "right":
root.parent.transition.direction = "right"
root.parent.transition.duration = 0.6
root.parent.current = "MainMenu"
我应该如何正确地做到这一点?
(编辑)这是完整的代码。我只是创建了两个屏幕,当我们在 SettingsMenu 屏幕上时,我想用鼠标右键切换回 MainMenu 屏幕
(SettingsMenu python 类中的评论 on_touch_down 可以正常工作,但是当我尝试使其在kivy方式,我用任何鼠标按钮切换屏幕,但需要的是鼠标右键)
Python:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.config import Config
Config.set('input', 'mouse', 'mouse,multitouch_on_demand')
class MainMenu(Screen):
pass
class SettingsMenu(Screen):
pass
# def on_touch_down(self, touch):
# if touch.button == 'right':
# self.parent.transition.direction = "right"
# self.parent.transition.duration = 0.6
# self.parent.current = "MainMenu"
class MenuManager(ScreenManager):
pass
main_kv = Builder.load_file("test.kv")
class THEApp(App):
def build(self):
return main_kv
THEApp().run()
这是Kivy文件(在复制粘贴过程中缩进可能会被破坏,但我没有遇到任何问题语法):
MenuManager:
MainMenu:
SettingsMenu:
<MainMenu>
name: "MainMenu"
FloatLayout:
size: root.width, root.height
Button:
text: "Button 1 on Main Menu Screen"
on_release:
root.manager.transition.direction = 'left'
root.manager.transition.duration = 0.6
root.manager.current = "SettingsMenu"
<SettingsMenu>
name: "SettingsMenu"
button: "right"
on_touch_down:
if root.button == "right": \
root.parent.transition.direction = "right"; \
root.parent.transition.duration = 0.6; \
root.parent.current = "MainMenu"
FloatLayout:
size: root.width, root.height
Label:
text: "Label 1 on SettingsMenu"
I have some problems with rewriting python code in kivy format
Here is the class "Settings" which has method to change screen.
class SettingsMenu(Screen):
def on_touch_down(self, touch):
if touch.button == 'right':
self.parent.transition.direction = "right"
self.parent.transition.duration = 0.6
self.parent.current = "MainMenu"
And I want to rewrite it in kivy this way (or something like that):
<SettingsMenu>
name: "SettingsMenu"
on_touch_down:
if button == "right":
root.parent.transition.direction = "right"
root.parent.transition.duration = 0.6
root.parent.current = "MainMenu"
How should I do it correctly?
(Edit) Here is the full code. I just create two screens and when we are on SettingsMenu screen, I want to switch back to MainMenu screen with right mouse button
(Commented on_touch_down in SettingsMenu python class works correctly, but when I try to make it in kivy way, I switch the screen with any mouse button, but desired was right mouse button)
Python:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.config import Config
Config.set('input', 'mouse', 'mouse,multitouch_on_demand')
class MainMenu(Screen):
pass
class SettingsMenu(Screen):
pass
# def on_touch_down(self, touch):
# if touch.button == 'right':
# self.parent.transition.direction = "right"
# self.parent.transition.duration = 0.6
# self.parent.current = "MainMenu"
class MenuManager(ScreenManager):
pass
main_kv = Builder.load_file("test.kv")
class THEApp(App):
def build(self):
return main_kv
THEApp().run()
This is the Kivy file (indentation may be broken during copy-paste, but I had no problems with syntax):
MenuManager:
MainMenu:
SettingsMenu:
<MainMenu>
name: "MainMenu"
FloatLayout:
size: root.width, root.height
Button:
text: "Button 1 on Main Menu Screen"
on_release:
root.manager.transition.direction = 'left'
root.manager.transition.duration = 0.6
root.manager.current = "SettingsMenu"
<SettingsMenu>
name: "SettingsMenu"
button: "right"
on_touch_down:
if root.button == "right": \
root.parent.transition.direction = "right"; \
root.parent.transition.duration = 0.6; \
root.parent.current = "MainMenu"
FloatLayout:
size: root.width, root.height
Label:
text: "Label 1 on SettingsMenu"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以为
kv
文件中的on_touch_down:
属性执行 python 代码/逻辑,但这有点愚蠢。我认为您的 kv 文件将像这样工作:在 kivy 语言中,您可以访问 on_的
args
。 方法(请参阅 文档)。因此上面的代码检查touch
参数的button
属性。请注意,
kv
文件中的if
语句必须位于单行上。这就是使用\
字符(转义结束行)和;
(分隔代码行)的原因。You can do python code/logic for an
on_touch_down:
attribute in akv
file, but it is a bit goofy. I think yourkv
file will work like this:In the
kivy
language, you can access theargs
of anon_<action>
method (see documentation). So the code above checks thebutton
attribute of thetouch
arg.Note that the
if
statement in thekv
file must be on a single line. That is the reason for the\
characters (to escape endlines) and the;
(to delimit the lines of code).