我有主文件 main.py 和其他几个针对不同屏幕的设计文件。在主屏幕上,文本应根据货币价值而变化。但它没有改变,我不明白为什么
主.py
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.utils import get_color_from_hex
from kivy.core.text import LabelBase
from kivy.clock import Clock
Window.size = (430, 930)
class ShopScreen(Screen):
Builder.load_file("Shop.kv")
class ItemsScreen(Screen):
Builder.load_file("Items.kv")
class HomeScreen(Screen):
Builder.load_file("Home.kv")
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.money = 224
self.ids.coins.text = str(self.money)
Clock.schedule_interval(self.update_text, self.money)
def update_text(self, dt):
self.money += 1
self.ids.coins.text = str(self.money)
class WindowManager(FloatLayout):
pass
class MainApp(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
sm = ScreenManager()
sm.add_widget(ShopScreen(name='menu'))
sm.add_widget(ItemsScreen(name='items'))
sm.add_widget(HomeScreen(name='main'))
return WindowManager()
if __name__ == "__main__":
LabelBase.register(name="Press Start 2P",
fn_regular='./font/PressStart2P-Regular.ttf')
MainApp().run()
主.kv
#: import get_color_from_hex kivy.utils.get_color_from_hex
<WindowManager>:
MDBottomNavigation:
font_name: "./font/PressStart2P-Regular.ttf"
panel_color: get_color_from_hex("#1B1B1B")
text_color_active: 1, 1, 0, 1
MDBottomNavigationItem:
icon: 'home'
name: 'shop'
text: 'МАГАЗИН'
ShopScreen:
MDBottomNavigationItem:
icon: 'home'
name: 'main'
text: 'ГОЛОВНА'
HomeScreen:
MDBottomNavigationItem:
icon: 'home'
name: 'items'
text: 'ХЕЛПЕРИ'
ItemsScreen:
home.kv
<HomeScreen@Screen>:
BoxLayout:
orientation: 'vertical'
padding: 10
BoxLayout:
size_hint: 1, 0.1
padding: 40
MDLabel:
font_name: "Press Start 2P"
text: 'name'
MDLabel:
id: coins
font_name: "Press Start 2P"
text: '0'
halign: 'right'
BoxLayout:
orientation: 'vertical'
spacing: 20
Image:
size_hint: 1, 0.9
source: "boss.png"
Button:
font_name: "Press Start 2P"
text: 'Грати'
background_color: 0,0,0,0
size_hint: .9, 0.1
pos_hint: {'center_x': 0.5, 'center_y': 0.5}
canvas.before:
Color:
rgba: (27/255,27/255,27/255,1)
RoundedRectangle:
pos: self.pos
size: self.size
radius: [30]
BoxLayout:
orientation: 'vertical'
size_hint: 1, 0.4
padding: 40
spacing: 40
Label:
pos_hint: {'center_x': 0.15, 'center_y': 0.1}
size_hint: 1, 0.1
text: 'артефакти'
font_name: "Press Start 2P"
BoxLayout:
spacing: 40
size_hint: 1, 1
MainItem1:
MainItem2:
MainItem3:
如何在文本中显示变量 Money 的数据并更新它(如果可能)
在您的
HomeScreen
类中,您以 update_text
的延迟来调度 self.money
方法,该延迟最初设置为 224。这意味着 update_text
方法将每 224 秒调用一次,而不是每秒调用一次。
要每秒更新文本,您应该将其安排为 1 秒的间隔(或您喜欢的任何间隔),而不是使用
self.money
。
更新了 HomeScreen 类:
class HomeScreen(Screen):
Builder.load_file("Home.kv")
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.money = 224
self.ids.coins.text = str(self.money)
Clock.schedule_interval(self.update_text, 1) # Update every second
def update_text(self, dt):
self.money += 1
self.ids.coins.text = str(self.money)
这将每秒用
self.money
的更新值更新文本。
确保从
self.money
中删除 Clock.schedule_interval
参数。