Python kivy 在 kivy 中重写 python 的问题

发布于 2025-01-12 13:25:24 字数 2342 浏览 0 评论 0原文

我在以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云仙小弟 2025-01-19 13:25:24

您可以为 kv 文件中的 on_touch_down: 属性执行 python 代码/逻辑,但这有点愚蠢。我认为您的 kv 文件将像这样工作:

<SettingsMenu>
    name: "SettingsMenu"
    on_touch_down:
        if args[1].button == "right": \
        root.parent.transition.direction = "right"; \
        root.parent.transition.duration = 0.6; \
        root.parent.current = "MainMenu"

在 kivy 语言中,您可以访问 on_args 。 方法(请参阅 文档)。因此上面的代码检查 touch 参数的 button 属性。

请注意,kv 文件中的 if 语句必须位于单行上。这就是使用 \ 字符(转义结束行)和 ; (分隔代码行)的原因。

You can do python code/logic for an on_touch_down: attribute in a kv file, but it is a bit goofy. I think your kv file will work like this:

<SettingsMenu>
    name: "SettingsMenu"
    on_touch_down:
        if args[1].button == "right": \
        root.parent.transition.direction = "right"; \
        root.parent.transition.duration = 0.6; \
        root.parent.current = "MainMenu"

In the kivy language, you can access the args of an on_<action> method (see documentation). So the code above checks the button attribute of the touch arg.

Note that the if statement in the kv 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文