如何将现有的文本文件数据转移到另一个文本文件或创建并写入文本文件并将数据转移到另一个文本文件?
How to transfer the existing text file data to another text file or create and write the text file and transfer the data to another text file?
a) 我有一个组合框(下拉列表中的现有文本文件),我想 select 下拉列表中的文本文件并从中读取行并传输 selective行到另一个文本文件。
b) 我有一个条目 (entry2),我想通过从该条目 (entry2) 获取名称并在新创建的文本文件中写入一些数据(来自 entry3)来创建文本文件。最后我想将数据从新创建的文本文件传输到另一个文本文件。
上面a)和b)中提到的"another text file"只不过是同一个文件。所以,基本上 a) 或 b) 都会是这种情况。我可以一次执行 a) 和 b) 一个,但我想将两者结合起来,比如如果组合框 selection 在那里做 a) 或者如果 entry2 在那里做 b).
这是我尝试过的方法,我不确定如果使用 combo1.get()==True 和 entry2.get()==True 循环是否有效。
from tkinter import *
from tkinter import Button
from tkinter import font
from tkinter.ttk import Combobox
import os
root=Toplevel()
root.state('zoomed')
bold20= font.Font(family='Times', size=18)
bold15= font.Font(family='Times', size=15)
bold30= font.Font(family='Times', size=30, weight='bold')
boldunderline30= font.Font(family='Times', size=30, weight='bold', underline=True)
topFrame = Frame(root, width=10000, height=500, relief= "raised", borderwidth=3)
topFrame.pack(expand=True, fill='both')
label4= Label(topFrame, text="Choose from the existing files:", font=bold20)
label4.grid(row=3, column=3, padx=400, pady=10, sticky=W)
label8= Label(topFrame, text="Store it in new file:", font=bold20)
label8.grid(row=7, column=3, padx=400, pady=10, sticky=W)
label7= Label(topFrame, text="New File:", font=bold20)
label7.grid(row=6, column=3, padx=400, pady=10, sticky=W)
entry3=Entry(topFrame)
entry3.grid(row=7, column=3, padx=750, pady=10, sticky=W)
OC_data=os.listdir("C:\Fourth Term @ Dal\Project\Collaboration\Sensitivity analysis\GUI\Cash Models\Operating Characteristics")
combo1= Combobox(topFrame, values=OC_data, width="30")
combo1.grid(row=3, column=3, padx=750, pady=10, sticky=W)
entry_field_variable = StringVar()
entry2 = Entry(topFrame, textvariable=entry_field_variable)
entry2.grid(row=6, column=3, padx=750, pady=10, sticky=W)
def save():
file_name1= combo1.get()
file_name2= entry2.get()
if combo1.get()==True:
existing_file= open("C:\Fourth Term @ Dal\Project\Collaboration\Sensitivity analysis\GUI\Cash Models\Operating Characteristics\"+file_name1, 'r')
line1=existing_file.readlines()
with open('C:\Fourth Term @ Dal\Project\Collaboration\Sensitivity analysis\GUI\Cash Models\Other data\IIL & Cap.txt', 'w') as i:
i.write(str(line1[0][10:]))
i.close()
if entry2.get()==True:
with open('C:\Fourth Term @ Dal\Project\Collaboration\Sensitivity analysis\GUI\Cash Models\Operating Characteristics\'+file_name2+ '.txt', 'w') as f:
f.write("Capacity: " + entry3.get())
f.close()
new_file=open("C:\Fourth Term @ Dal\Project\Collaboration\Sensitivity analysis\GUI\Cash Models\Operating Characteristics\"+file_name2 + '.txt', 'r')
line2=new_file.readlines()
with open('C:\Fourth Term @ Dal\Project\Collaboration\Sensitivity analysis\GUI\Cash Models\Other data\IIL & Cap.txt', 'w') as i:
i.write(str(line2[0][10:]))
i.close()
savebutton = Button(topFrame, borderwidth=2, bg="skyblue", text="Save info.", font=bold20, width=10, pady=5, command=save)
savebutton.grid(row=14, column=3, padx=700, pady=30, sticky=W)
root.mainloop()
希望上面的问题表达的好。如果你们中的任何人不介意只看一下 TIA,它会很高兴 :)enter code here
combo1.get()
和 entry2.get()
都会 return 一个字符串,因此 none 将是 == True
。但是如果不为空,字符串将计算为 True
,因此您可以这样写:
if combo1.get():
# etc.
如果字符串为空 == ""
,if
表达式将计算为 False
,如果字符串包含字符,则为 True
。
还有;不要将根 window 创建为:
root=Toplevel()
因为这也会创建 Tk()
的一个实例。而是使用:
root = Tk()
a) 我有一个组合框(下拉列表中的现有文本文件),我想 select 下拉列表中的文本文件并从中读取行并传输 selective行到另一个文本文件。 b) 我有一个条目 (entry2),我想通过从该条目 (entry2) 获取名称并在新创建的文本文件中写入一些数据(来自 entry3)来创建文本文件。最后我想将数据从新创建的文本文件传输到另一个文本文件。
上面a)和b)中提到的"another text file"只不过是同一个文件。所以,基本上 a) 或 b) 都会是这种情况。我可以一次执行 a) 和 b) 一个,但我想将两者结合起来,比如如果组合框 selection 在那里做 a) 或者如果 entry2 在那里做 b).
这是我尝试过的方法,我不确定如果使用 combo1.get()==True 和 entry2.get()==True 循环是否有效。
from tkinter import *
from tkinter import Button
from tkinter import font
from tkinter.ttk import Combobox
import os
root=Toplevel()
root.state('zoomed')
bold20= font.Font(family='Times', size=18)
bold15= font.Font(family='Times', size=15)
bold30= font.Font(family='Times', size=30, weight='bold')
boldunderline30= font.Font(family='Times', size=30, weight='bold', underline=True)
topFrame = Frame(root, width=10000, height=500, relief= "raised", borderwidth=3)
topFrame.pack(expand=True, fill='both')
label4= Label(topFrame, text="Choose from the existing files:", font=bold20)
label4.grid(row=3, column=3, padx=400, pady=10, sticky=W)
label8= Label(topFrame, text="Store it in new file:", font=bold20)
label8.grid(row=7, column=3, padx=400, pady=10, sticky=W)
label7= Label(topFrame, text="New File:", font=bold20)
label7.grid(row=6, column=3, padx=400, pady=10, sticky=W)
entry3=Entry(topFrame)
entry3.grid(row=7, column=3, padx=750, pady=10, sticky=W)
OC_data=os.listdir("C:\Fourth Term @ Dal\Project\Collaboration\Sensitivity analysis\GUI\Cash Models\Operating Characteristics")
combo1= Combobox(topFrame, values=OC_data, width="30")
combo1.grid(row=3, column=3, padx=750, pady=10, sticky=W)
entry_field_variable = StringVar()
entry2 = Entry(topFrame, textvariable=entry_field_variable)
entry2.grid(row=6, column=3, padx=750, pady=10, sticky=W)
def save():
file_name1= combo1.get()
file_name2= entry2.get()
if combo1.get()==True:
existing_file= open("C:\Fourth Term @ Dal\Project\Collaboration\Sensitivity analysis\GUI\Cash Models\Operating Characteristics\"+file_name1, 'r')
line1=existing_file.readlines()
with open('C:\Fourth Term @ Dal\Project\Collaboration\Sensitivity analysis\GUI\Cash Models\Other data\IIL & Cap.txt', 'w') as i:
i.write(str(line1[0][10:]))
i.close()
if entry2.get()==True:
with open('C:\Fourth Term @ Dal\Project\Collaboration\Sensitivity analysis\GUI\Cash Models\Operating Characteristics\'+file_name2+ '.txt', 'w') as f:
f.write("Capacity: " + entry3.get())
f.close()
new_file=open("C:\Fourth Term @ Dal\Project\Collaboration\Sensitivity analysis\GUI\Cash Models\Operating Characteristics\"+file_name2 + '.txt', 'r')
line2=new_file.readlines()
with open('C:\Fourth Term @ Dal\Project\Collaboration\Sensitivity analysis\GUI\Cash Models\Other data\IIL & Cap.txt', 'w') as i:
i.write(str(line2[0][10:]))
i.close()
savebutton = Button(topFrame, borderwidth=2, bg="skyblue", text="Save info.", font=bold20, width=10, pady=5, command=save)
savebutton.grid(row=14, column=3, padx=700, pady=30, sticky=W)
root.mainloop()
希望上面的问题表达的好。如果你们中的任何人不介意只看一下 TIA,它会很高兴 :)enter code here
combo1.get()
和 entry2.get()
都会 return 一个字符串,因此 none 将是 == True
。但是如果不为空,字符串将计算为 True
,因此您可以这样写:
if combo1.get():
# etc.
如果字符串为空 == ""
,if
表达式将计算为 False
,如果字符串包含字符,则为 True
。
还有;不要将根 window 创建为:
root=Toplevel()
因为这也会创建 Tk()
的一个实例。而是使用:
root = Tk()