【问题标题】:How to refresh or update frame after value changes in text file using Python/Tkinter如何使用 Python/Tkinter 在文本文件中的值更改后刷新或更新框架
【发布时间】:2019-05-19 06:09:09
【问题描述】:

所以基本上我是从一个文本文件中读取一个值,该文件使用标签显示在配置文件框架上。 def view_value(self): self.user = self.controller.user self.view_value()

        with open(self.user + '.txt', "r") as f:
            value_line = 2
            for i, line in enumerate(f):
                if i == value_line:
                    self.value.set(line)
                    self.value.config(textvariable=line)

当我去不同的框架再次计算这个值时,它会用新计算的值更新文本文件。但是,当我使用后退按钮返回上一页时 - 旧值仍然存在。为了让新值出现,我需要重新打开/重新运行程序。

是否可以在不重新启动应用程序的情况下在页面上显示新更新的值?我尝试调用我的 view_value 方法来尝试更新值,还尝试从其他类配置标签,但无法使其正常工作。

我也意识到我的代码可能有一百万个错误,我对 Python 很陌生,所以很抱歉!

【问题讨论】:

  • 您在class Profileclass CalculateAllowance 中都使用了self.allowance。因此self.allowance 是绑定到该类的实例变量。将其设为controller 中的实例变量并使用controller.allowance 访问它。

标签: python python-3.x tkinter refresh


【解决方案1】:

您需要更新StringVar Profile.allowance 以更新Profile 页面中的显示。 简单的方法是在write_to_file()CalculateAllowance类的self.controller.show_frame(Profile)之前添加以下语句:

self.controller.frames[Profile].allowance.set(self.user_data[2])

您还需要修复代码中的以下问题:

  • 删除在Profile 类中的view_allowance() 内调用self.view_allowance(),因为它会导致无限递归问题。
  • 删除 view_allowance() 中的 self.holiday_allowance_amount.config(textvariable=line),因为它错误地将 textvariable 重新分配给字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    相关资源
    最近更新 更多