在应用程序关闭之前,如何单击按钮单击文件?

发布于 2025-01-21 03:35:32 字数 1520 浏览 2 评论 0原文

我正在尝试实现一个函数,该函数从文本文件和TKInter条目框的内容中读取整数,将它们组合在一起,并以TKInter标签显示组合的结果。然后,它应该在文本文件中写入组合的结果。我希望能够多次按下此功能,并每次将新值与输入框值组合在一起。该函数写入文本值,因此,当重新打开时,它可以保留计数的值。但是,我发现文本文件仅在关闭应用程序后才写入,因此,如果我尝试添加多个输入框值,它们仅与文本文件中的原始值结合在一起。能够在应用程序关闭之前写入文件可以解决此问题。

在进行了一些据说使用冲洗,FSONC和CLOSS的研究之后,并且将缓冲设置为0或1应该解决此问题,但是在关闭应用程序之前,这些问题都没有写给文件。

如何在应用程序关闭之前单击按钮上写入文件?

这是Python中的最小代码示例:

import os
from tkinter import *
class counter():
    def Add(self):
        count = int(self.WCount)
        new_count = count + int(self.Wtxtfld.get())
        self.Wlbl.configure(text="Total WCount: " + str(new_count))
        self.Wtxtfld.delete(0, END)
        with open('WCountSave', 'w') as f:
            f.write(str(new_count))
            f.flush() 
            os.fsync(f)
            f.close()
    def __init__(self):
        self.root = Tk()
        self.root.geometry("450x50")
        with open('WCountSave', 'r') as f:
            gotCount = f.read()
        self.WCount = gotCount
        self.Wlbl = Label(self.root, text="Total WCount: " + self.WCount, fg='light blue', bg='black',font=("Helvetica", 16))
        self.Wbtn = Button(self.root, text="Add", command=self.Add, font=("Arial 12 "), bg=("#5540D7"))
        self.Wtxtfld = Entry(self.root, text="entry 3", bd=5)
        self.Wlbl.place(x=200, y=0)
        self.Wbtn.place(x=150, y=0)
        self.Wtxtfld.place(x=0, y=0)
        self.root.mainloop()
a = counter()

WcountSave是一个.txt文件,其中包含以下内容:

0

I'm trying to implement a function that reads an integer from a text file and the contents of a tkinter entry box, combines them, and displays the result of the combination in a tkinter label. Then it should write the result of the combination in a text file. I want this function to be able to be pressed multiple times, and combine the new value in the text file with the entry box value each time. The function writes to a text value so when re-opened it can retain the value of the count. However, I've found that the text file is only written to once I close the application, so if I try to add multiple entry box values they are only ever combined with the original value in the text file. Being able to write to the file before the application closes would solve this issue.

After doing some research supposedly using flush, fsync, and close, as well as setting the buffering to 0 or 1 are supposed to solve this problem, but none of these wrote to the file before the application was closed.

How do I write to a file on button click before the application closes?

Here's a minimal code example in python:

import os
from tkinter import *
class counter():
    def Add(self):
        count = int(self.WCount)
        new_count = count + int(self.Wtxtfld.get())
        self.Wlbl.configure(text="Total WCount: " + str(new_count))
        self.Wtxtfld.delete(0, END)
        with open('WCountSave', 'w') as f:
            f.write(str(new_count))
            f.flush() 
            os.fsync(f)
            f.close()
    def __init__(self):
        self.root = Tk()
        self.root.geometry("450x50")
        with open('WCountSave', 'r') as f:
            gotCount = f.read()
        self.WCount = gotCount
        self.Wlbl = Label(self.root, text="Total WCount: " + self.WCount, fg='light blue', bg='black',font=("Helvetica", 16))
        self.Wbtn = Button(self.root, text="Add", command=self.Add, font=("Arial 12 "), bg=("#5540D7"))
        self.Wtxtfld = Entry(self.root, text="entry 3", bd=5)
        self.Wlbl.place(x=200, y=0)
        self.Wbtn.place(x=150, y=0)
        self.Wtxtfld.place(x=0, y=0)
        self.root.mainloop()
a = counter()

WCountSave is a .txt file with the following contents:

0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文