对于我的游戏,我想在用户更改屏幕时重置屏幕。
我已经尝试了许多事情,例如使用时钟和进行更新功能,但它从未真正起作用。即使我在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"
感谢您的帮助
答案 0 :(得分:0)
您的代码提供了禁用Button
的功能,但是您的代码中没有任何内容可以重新启用它。禁用Button
后,它将保持这种状态,直到发生更改为止。您可以在SecondWindow
中添加一些内容,以在每次显示button
时重新启用SecondWindow
。每当显示on_enter
时,您可以使用on_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"
请注意为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”。