You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现Kivy Gridlayout中Pandas Dataframe数据的动态定时刷新

解决方案

核心思路

  • 不用单独声明每个Label,遍历生成数据Label的同时将其存入实例属性的列表,后续直接通过索引更新对应Label的text属性即可
  • 单独封装数据刷新方法,每次执行时先重新读取最新的Excel文件生成DataFrame,再遍历更新所有数据Label的文本
  • 沿用你熟悉的Clock.schedule_interval机制定时调用刷新方法,和图片更新逻辑完全一致

完整修改代码

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
import pandas as pd
from kivy.clock import Clock

class InfoPage(GridLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.cols = 1
        self.size_hint = (0.9,0.9)
        self.pos_hint = {"center_x": 0.5, "center_y":0.5}
        self.spacing = 10
        self.bind(minimum_height = self.setter('height'))

        # 预定义表头字段和对应的Excel列名,顺序要对应
        self.columns = [
            ("Clouds", "Clouds"),
            ("Cloud Cover Type", "Cloud Cover Type"),
            ("Icons", "Icons"),
            ("Precipication %", "Precipication %"),
            ("Time", "Time"),
            ("Temperature", "Temperature"),
            ("Fells like Temperature", "Feels like temp"),
            ("Air Pressure", "Air Pressure"),
            ("Humidity", "Humidity"),
            ("Dew Point", "Dew Point"),
            ("UV Index", "UV Index"),
            ("Cloud Coverage %", "Cloud Coverage"),
            ("Visibility", "Visibility"),
            ("Wind Speed", "Wind Speed"),
            ("Wind Direction", "Wind Direction"),
            ("Wind Gusts", "Wind Gusts")
        ]

        self.weather_data_grid = GridLayout(size_hint=(1,None), size=(Window.width, Window.height + 200), row_default_height=15, cols=16)
        # 生成表头
        for header, _ in self.columns:
            self.weather_data_grid.add_widget(Label(text=header))
        
        # 初始化数据Label并存入列表,方便后续更新
        self.data_labels = []
        init_df = pd.read_excel("Weather_data.xlsx")
        for _, col_name in self.columns:
            label = Label(text=str(init_df.loc[1, col_name]))
            self.data_labels.append(label)
            self.weather_data_grid.add_widget(label)

        self.add_widget(self.weather_data_grid)

        # 定时刷新,间隔30秒,可根据需求调整
        Clock.schedule_interval(self.update_weather_data, 30)

    def update_weather_data(self, dt):
        # 重新读取最新的Excel数据
        latest_df = pd.read_excel("Weather_data.xlsx")
        # 按索引逐个更新Label的文本
        for idx, (_, col_name) in enumerate(self.columns):
            self.data_labels[idx].text = str(latest_df.loc[1, col_name])


class SkitouringApp(App):
    def build(self):
        self.screen_manager = ScreenManager()
        
        self.info_page = InfoPage()
        screen = Screen(name="Info")
        screen.add_widget(self.info_page)
        self.screen_manager.add_widget(screen)
        
        return self.screen_manager
    
if __name__ == "__main__":
    Skitour_app = SkitouringApp()
    Skitour_app.run()

补充说明

如果你需要展示多行数据,只需要把self.data_labels改成二维列表,每行生成一组Label存进去,刷新的时候按行和列的索引遍历更新即可,逻辑完全一致。

内容的提问来源于stack exchange,提问作者Andy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 23:06:05