如何同步页面更新? (舰队)

问题描述 投票:0回答:1

我有代码:

def main(page: Page):
    global views_count
    vf = open("views", "r+")
    views_count = int(vf.readline().replace("\n", ''))
    def route_change(e: ft.RouteChangeEvent):
        global views_count
        global vf
        views = ft.Container(ft.Text(f"Views: {views_count + 1}"))
        page.update()

    page.add(views)
            views_count += 1
            vf = open("views", "w")
            vf.close()
            vf = open("views", "a+")
            vf.write(str(views_count))
            vf.close()
            vf = open("views", "r")
            views_count = int(vf.readline().replace("\n", ''))
            page.update()

一切正常,但例如,如果用户 1 访问该网站,他看到值 1,然后用户 2 访问该网站并看到值 2,但用户 1 仍将具有值 1。如何更新实时值? (无需重新加载页面)。预先感谢,抱歉英语不好

我尝试将值写入文件(在示例中),并运行页面更新周期:

while True:
    page.update()

但页面只是停止响应点击。

python python-3.x web flet
1个回答
0
投票

我花了很多时间寻找如何解决这个问题。 尝试这个: def main(页: 页): 全局浏览次数 vf = open("视图", "r+") 视图计数 = int(vf.readline().replace(" “,'')) def route_change(e: ft.RouteChangeEvent): 全局浏览次数 全局室颤 视图 = ft.Container(ft.Text(f"视图: {views_count + 1}")) 页面.update()

#Add the clean method so that the page is cleaned, update it and load its 
#content again with add
page.clean()
page.update()

page.add(views)
        views_count += 1
        vf = open("views", "w")
        vf.close()
        vf = open("views", "a+")
        vf.write(str(views_count))
        vf.close()
        vf = open("views", "r")
        views_count = int(vf.readline().replace("\n", ''))
        page.update()

希望能解决您的问题

问候

© www.soinside.com 2019 - 2024. All rights reserved.