简体   繁体   English

如何实时更新 GUI 中文本文件中的变量? Python 和 tkinter

[英]How to update variables from a text file in a GUI in real time? Python and tkinter

I have a script that continuously updates numbers on a text file in the same directory.我有一个脚本可以不断更新同一目录中文本文件上的数字。 I made a GUI using tkinter to display these numbers.我使用 tkinter 制作了一个 GUI 来显示这些数字。 I'm having a hard time getting the GUI to update the numbers in real time.我很难让 GUI 实时更新数字。 It will display the numbers as they were when the GUI first started, but will not update the display as the text file changes.它将显示 GUI 首次启动时的数字,但不会随着文本文件的更改而更新显示。 Here's some basic code to demonstrate:这里有一些基本的代码来演示:

def read_files(file, line):
    old = open(f'{file}.txt', 'r').readlines()[line]
    new = old.replace('\n', '')
    return new

number = read_files('Statistics', 0)
number_label = Label(frame1, text=f'{number}')
number_label.grid(row=0, column=0)

The above code shows the number from the text file as it was when the GUI first opened.上面的代码显示了文本文件中的数字,就像 GUI 首次打开时一样。 However, it does not update the number as its value in the text file changes.但是,它不会随着文本文件中的值更改而更新数字。 I did some reading around and also tried the following:我做了一些阅读,还尝试了以下方法:

def read_files(file, line):
    old = open(f'{file}.txt', 'r').readlines()[line]
    new = old.replace('\n', '')
    return new

number = read_files('Statistics', 0)
number_label = StringVar()
number_label.set(number)
number_display = Label(frame1, text=f'{number_label.get()}')
number_display.grid(row=0, column=0)

This has the same effect.这具有相同的效果。 It shows the value retrieved from the text file at the moment the GUI was opened, but does not update it as the text file is updated.它显示在打开 GUI 时从文本文件中检索到的值,但不会随着文本文件的更新而更新它。 Any help is appreciated.任何帮助表示赞赏。

Since there is no complete code, take a look at this example:由于没有完整的代码,看一下这个例子:

from tkinter import *

root = Tk()

def update_gui():
    number = read_files('Statistics', 0) # Call the function
    number_display.config(text=number) # Change the text of the label, also same as text=f'{number}'
    root.after(1000,update_gui) # Repeat this function every 1 second

def read_files(file, line):
    old = open(f'{file}.txt', 'r').readlines()[line]
    new = old.replace('\n', '')
    return new

number_display = Label(root) # Empty label to update text later
number_display.grid(row=0, column=0)

update_gui() # Initial call to the function

root.mainloop()

Here the label is created outside the function but the text is not given, but inside the function we are repeating the function every 1 second, with after , and changing the text of the label with config method. Here the label is created outside the function but the text is not given, but inside the function we are repeating the function every 1 second, with after , and changing the text of the label with config method. I have avoided the use of StringVar() as it's of no use here, you can just store it in a normal variable and use it.我已经避免使用StringVar()因为它在这里没有用,你可以将它存储在一个普通变量中并使用它。


Here is a plagiarized look at how after should be used:以下是如何使用after抄袭

def after(self, ms, func=None, *args):
    """Call function once after given time.

    MS specifies the time in milliseconds. FUNC gives the
    function which shall be called. Additional parameters
    are given as parameters to the function call.  Return
    identifier to cancel scheduling with after_cancel."""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何实时更新 tkinter label 文本 - How to update tkinter label text in real time 如何使用pyown和tkinter在GUI中更新文本变量 - How to update text-variables in a GUI using pyown and tkinter 实时更新Tkinter Text小部件? - Update a Tkinter Text widget real time? 如何将实时STDOUT从导入模块重定向到python中的Tkinter Text Widget? - How to redirect in real time STDOUT from imported module to Tkinter Text Widget in python? 使用python和tkinter进行实时GUI的延迟 - Having some delays using python and tkinter for a real time GUI 在一个 GUI 中嵌入多个实时图形 Python Tkinter GUI - Embedding multiple real-time graphs in one Python Tkinter GUI 如何一次从多个 Tkinter GUI 窗口写入 Excel 文件? - How to write from multiple Tkinter GUI windows to an Excel file at a time? 如何实时更新txt中的Python个变量? - How do I update Python variables in txt in real time? 从 Arduino 读取模拟值并将其实时显示在 Tkinter GUI 上 - Read analog value from Arduino and display it on a Tkinter GUI in real time 在 Python 中,您如何让一个 GUI 不断启动,但又不断从数据库中获取数据以实时更新? - In Python, how would you have a GUI up constantly, but have it constantly fetch data from database to update in real time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM