【发布时间】:2026-02-03 23:15:03
【问题描述】:
大家晚上好,请问我的这个关于传输数据以删除文本行的问题的帮助。
Sample.txt consist of:
one
two
three
four
five
这是我的代码:
import tkinter
from tkinter import *
from tkinter import filedialog
root = Tk()
root.title("Delete line")
def remove_line(fileName, lineToSkip):
with open(fileName, 'r') as read_file:
lines = read_file.readlines()
currentLine = 1
with open(fileName, 'w') as write_file:
for line in lines:
if currentLine == lineToSkip:
pass
else:
write_file.write(line)
currentLine += 1
Numbah = IntVar()
NumbahEntry = Entry(root,textvariable=Numbah,width="30").pack()
sample = remove_line("Sample.txt", Numbah.get())
AcceptButton = Button(root, text="Submit", command=sample,width="30", height="2",bg="lightgreen").pack()
root.mainloop()
remove_line("Sample.txt", 1) #右边数表示 删除哪一行
我正在尝试通过在输入框上输入一个表示要删除的行的数字来生成结果
【问题讨论】:
-
请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。
标签: python file user-interface tkinter text