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

2024-04-26 13:56:09 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我尝试过很多事情,比如使用时钟和制作更新功能,但它从未真正起作用。布局没有改变,即使我在python中显式地改变了它

例如,如果我有一个按钮在释放时被禁用,当我改变屏幕时,它就不应该再被禁用了。这个东西在我的脑海中已经存在很长时间了,我不太明白

这个main.py、screen_manager.kv和main.kv有三个小文件。很抱歉这个问题

main.py

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()

屏幕管理器.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"

main.kv

<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:
            text: 'press me'
            pos_hint: {'x': 0.0, 'y': 0.2}
            size_hint: 0.3, 0.3
            on_release: self.disabled = True

谢谢你的帮助


Tags: fromposimportselfsize屏幕mainmanager
2条回答

您的代码提供了禁用Button的功能,但代码中没有任何内容可以重新启用它。一旦Button被禁用,它就会一直保持这种状态,直到有东西改变它。您可以在SecondWindow中添加一些内容,以便在每次显示SecondWindow时重新启用button。您可以使用on_enteron_pre_enter在显示Screen时触发某些事件。像这样:

<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"

注意为Quiz添加的id和添加的on_pre_enter:。还需要一种引用Button的方法。为此,我在Button上加了一个id

<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:,并重新启用“按钮”

相关问题 更多 >