更改屏幕或单击按钮后重置上一屏幕

时间:2020-07-21 09:02:26

标签: python kivy kivy-language

对于我的游戏,我想在用户更改屏幕时重置屏幕。

我已经尝试了许多事情,例如使用时钟和进行更新功能,但它从未真正起作用。即使我在python中明确更改了布局,也不会更改。

例如,如果我的某个按钮在释放时被禁用,那么当我切换屏幕时,就不再应该禁用它了。这是我很久以来一直在想的事情,并且不太了解。

对于这个main.py,我有三个小文件,screen_manager.kv和main.kv。很抱歉这个菜鸟问题

main.py

$theme-colors: (
    'primary': #e28215,
    'secondary': #83b8f3,
    'tertiary': #1575e2,
    ...
);

screen_manager.kv

from kivy.app import App
from kivy.clock import Clock
from kivy.config import Config
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.widget import Widget

# Setup the window
Config.set('graphics', 'resizable', False)
width = 550
height = 550
Window.size = (width, height)

class OptionWindow(Screen):
    pass

class SecondWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass

class Quiz(Widget):
    def __init__(self, **kwargs):
        super(Quiz, self).__init__(**kwargs)

    def update(self, dt):
        pass

kv = Builder.load_file('screen_manager.kv')
class Application(App):
    CATEGORY = ''
    def build(self):
        game = Quiz()
        Clock.schedule_interval(game.update, 1.0 / 60.0)
        return kv

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

main.kv

# File name: screen_manager.kv
#:include main.kv
WindowManager:
    OptionWindow:
    SecondWindow:

<OptionWindow>:
    name: 'first'
    Button:
        text: "Reset SecondWindow"
        on_release:
            app.root.current = 'second'
            root.manager.transition.direction = "right"

<SecondWindow>:
    name: 'second'
    Quiz:
    FloatLayout:
        size: root.width, root.height
        pos: 0, 0
        Button:
            text: "Go back"
            pos_hint: {'x': 0.4, 'y': 0.2}
            size_hint: 0.6, 0.6
            on_release:
                app.root.current = 'first'
                root.manager.transition.direction = "right"

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您的代码提供了禁用Button的功能,但是您的代码中没有任何内容可以重新启用它。禁用Button后,它将保持这种状态,直到发生更改为止。您可以在SecondWindow中添加一些内容,以在每次显示button时重新启用SecondWindow。每当显示on_enter时,您可以使用on_pre_enterScreen来触发某些事情。像这样:

<SecondWindow>:
    name: 'second'
    on_pre_enter: quiz.ids.butt.disabled = False
    Quiz:
        id: quiz
    FloatLayout:
        size: root.width, root.height
        pos: 0, 0
        Button:
            text: "Go back"
            pos_hint: {'x': 0.4, 'y': 0.2}
            size_hint: 0.6, 0.6
            on_release:
                app.root.current = 'first'
                root.manager.transition.direction = "right"

请注意为id添加的Quiz和添加的on_pre_enter:。还需要一种引用Button的方法。我通过在该id上添加一个Button来做到这一点:

<Quiz>:
    FloatLayout:
        id: thelayout
        size: root.width, root.height
        canvas.before:
            Color:
                rgba: 1, 1, 1, 1
            Rectangle:
                pos: self.pos
                size: self.size
        Button:
            id: butt
            text: 'press me'
            pos_hint: {'x': 0.0, 'y': 0.2}
            size_hint: 0.3, 0.3
            on_release: self.disabled = True

现在,每次显示SecondWindow之前,都会触发on_pre_enter:,并重新启用“ Button”。