【发布时间】:2021-09-09 14:32:41
【问题描述】:
def write_lor():
with open("data.txt", 'r+') as filehandle:
filecontents = filehandle.readlines()
filehandle.seek(0)
new = (int(filecontents[0])+1)
filehandle.write(str(new))
filehandle.truncate()
这是一种基本的计数器类系统,可以保存为 txt。无论如何要在后面引用变量new?比如我的用法是:
button1 = Button(frame, text=("Left On Read:\n", **VARIABLE**), fg="black", bg="white", height = 5, width = 20, command=write_lor)
变量需要是上面函数中引用的变量new。
【问题讨论】:
-
您必须将其设为全局变量;在
write_lor返回后,无法访问局部变量new。 -
您可以从函数内部更新按钮内的文本。