能不能直接传输到下个文件?
import tkinter
from tkinter import CENTER
from easygui.boxes.fileopen_box import tk
import test_1
width = 300
height = 220
Login_window = tk.Tk()
screenwidth = Login_window.winfo_screenwidth()
screenheight = Login_window.winfo_screenheight()
Login_window.title("登录界面")
Login_window.resizable(False, False)
Login_window.geometry('%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2,
(screenheight - height) / 2 - 50))
show = tkinter.Button(Login_window, text='1', command=lambda: myfunc(), width=15,
height=8, bg="white", bd=0)
show.place(relx=0.73, rely=0.53, anchor=CENTER)
def myfunc():
name = "A"
secret = "123"
file_key = open('./data/use_key.txt', 'w')
file_key.write(name + '\n')
file_key.write(secret)
test_1.read()
Login_window.mainloop()
def read():
a = []
with open('./data/use_key.txt', "r") as f:
for line in f.readlines(): # 打开后逐行读取
a.append(line.strip('\n'))
print(a)
望采纳!!!点击回答右侧采纳即可!!
这可能是由于文件缓存的原因,可以尝试使用Python的os模块的os.fsync()函数来强制将文件缓存写入磁盘,以确保数据的及时更新。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 打开文件
fo = open("runoob.txt", "wb")
print "文件名为: ", fo.name
# 刷新缓冲区
fo.flush()
代码修改如下, 文件写完要关闭:
import tkinter
from tkinter import CENTER
from easygui.boxes.fileopen_box import tk
import test_1
width = 300
height = 220
Login_window = tk.Tk()
screenwidth = Login_window.winfo_screenwidth()
screenheight = Login_window.winfo_screenheight()
Login_window.title("登录界面")
Login_window.resizable(False, False)
Login_window.geometry('%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2,
(screenheight - height) / 2 - 50))
show = tkinter.Button(Login_window, text='1', command=lambda: myfunc(), width=15,
height=8, bg="white", bd=0)
show.place(relx=0.73, rely=0.53, anchor=CENTER)
def myfunc():
name = "A"
secret = "123"
file_key = open('./data/use_key.txt', 'w')
file_key.write(name + '\n')
file_key.write(secret)
file_key.close()
test_1.read()
Login_window.mainloop()
打印截图:
在文本的输出结束以后要及时地close文本,不然文本读取的永远是没有修改过的文本,close了以后内容才真正被修改,能被读取,望采纳!
文本打开需要关闭的,你可以用withopen不用关闭
使用os模块中的os.fsync()函数进行强制刷新