使用Kivy库保存屏幕的数据



我一直在写一个脚本来开发一个计算麻将游戏结果的应用程序。我创建了多个屏幕来输入玩家名称和分数等。在其中一个屏幕中,我正在输入分数,但没有保存。一旦我移动到其他屏幕,分数数据就会丢失。如何在屏幕中保存分数?

.py文件在下面看起来像

Window.size = (360, 600)
class MainScreen(Screen):
pass
class Player_Screen(Screen):
#pass
#def callback(self, text):
#self.ids.textbox.text = text
class ScreenManagement(ScreenManager):
shared_data = StringProperty("")
Mahjong = Builder.load_file('trial_app.kv')  # Specifying location of kv file

class MainApp(App):
def build(self):
return Mahjong
if __name__ == "__main__":
MainApp().run()

我的.kv文件在下面看起来像

#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import App kivy.app.App
ScreenManagement:
#title: title # so I can use it in the .py file
#score:score # same here
transition: FadeTransition()
MainScreen:
id: title
Player_Screen:
id: Players
name: 'Players'


<MainScreen>:
name: "main"
canvas.before:
Color:
rgba: 0.2, 0.5, 1, 0.5
Rectangle:
pos: self.pos
size: self.size
GridLayout:
cols : 1
Button:
text: "Mahjong"
background_normal:'Mahjong.JPG'
Button:
text: "Let's Play"
on_release: app.root.current = "Contents"
<Player_Screen>:
f_username: Name
BoxLayout:
orientation: 'vertical'
id:f_username
TextInput:
id: Name
hint_text:'Enter Player1'
on_text: root.manager.shared_data = self.text #root.player_name()
TextInput:
id: Name
hint_text:'Enter Player2'
on_text: root.manager.shared_data = self.text
TextInput:
id: Name
hint_text:'Enter Player3'
on_text: root.manager.shared_data = self.text
TextInput:
id: Name
hint_text:'Enter Player4'
on_text: root.manager.shared_data = self.text
Button:
text: 'Back'
on_press: self.background_color =  0,0,0,1
on_release: root.manager.current = 'Contents'
Button:
text: 'Next'
#on_press: root.callback(self.text)
on_press: root.save_data()
on_release: root.manager.current = 'Score'

首先请注意,您上次编辑的代码不是可复制的。有时候做这样的人是很重要的。

但是,如果我说得对,那么只要应用程序正在运行,您就希望在代码中的某个位置共享或使用输入字段中输入的数据。如果这是要求,您可以通过多种方式来实现。这里有这样一个:

要将所有数据集中存储在shared_data中,请将其设置为DictProperty

class ScreenManagement(ScreenManager):
shared_data = DictProperty(rebind = True)
# rebind is necessary for nested access in kvlang.

使用以下不同键在输入字段中输入数据,

<Player_Screen>:
f_username: Name
BoxLayout:
orientation: 'vertical'
id:f_username
TextInput:
id: Name
hint_text:'Enter Player1'
on_text: root.manager.shared_data["player1"] = self.text
TextInput:
id: Name
hint_text:'Enter Player2'
on_text: root.manager.shared_data["player2"] = self.text
...

存储后,您可以从GUI的ScreenManagement实例(恰好也是root)的任何位置访问它们。我添加了另一个屏幕来演示这一点。

class PlayerNameScreen(Screen):
pass

现在在kvlang中,对于该屏幕,

...
ScreenManagement:
#title: title # so I can use it in the .py file
#score:score # same here
transition: FadeTransition()
MainScreen:
id: title
Player_Screen:
id: Players
name: 'Players'
PlayerNameScreen:
name: "player_names"

<PlayerNameScreen>:
Label:
text: "\n".join(f"{player} : {name}" for player, name in root.manager.shared_data.items())
Button:
size_hint: None, None
size: "100dp", "50dp"
pos_hint: {"center_x" : 0.5, "center_y" : 0.2}
text: "Back"
on_release: root.manager.current = "Players"

相关内容

  • 没有找到相关文章

最新更新