a) I have one combobox (existing text files in the dropdown) and I want to select the text file from the dropdown list and read the lines from it and transfer the selective line into another text file. b) I have one entry (entry2) and I want to create the text file by taking its name from that entry (entry2) and writing some data (from entry3) in the newly created text file. And at last I want to transfer the data from the newly created to text file to another text file.
The "another text file" mentioned in a) and b) above is nothing but the same file. So, basically either a) or b) is going to be the case. I am able to do perform a) and b) one at a time but I want to combine both, something like if combobox selection is there do a) or if entry2 is there do b).
Here is what I have tried, I am not sure whether if loop with combo1.get()==True and entry2.get()==True works or not.
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()
I hope the above question conveyed well. It would be glad, if anybody of you wouldn't mind just looking at it TIA :)enter code here
combo1.get()
and entry2.get()
will both return a string so none of them will be == True
. But strings will evaluate to True
if not empty, so you can write:
if combo1.get():
# etc.
The if
expression will evaluate to False
if string is empty == ""
and to True
if sting contains characters.
Also; don't create the root window as:
root=Toplevel()
because this will also create an instance of Tk()
. Instead use:
root = Tk()