I have wrote a program that lets the user open up their text file that would look something like this:
1
2
3
4
5
6
It would then let the user enter a name for the list such as "numbers" The program would then let the user choose where they want to save the list. The user would then click go and the program would turn their input into a list like this:
numbers = ['1', '2', '3', '4', '5', '6']
This is the code that I wrote:
import tkinter
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import asksaveasfilename
from tkinter.messagebox import showerror
from tkinter import filedialog
import sys
def program():
global saveName
with open(fname) as input_file:
longList = [line.strip() for line in input_file.readlines()]
finListName = listName.get()
finList = (finListName) + (" = ") + str(longList)
txtFile = open((saveName),"w")
txtFile.write(finList)
#Window
window = tkinter.Tk()
window.title("Python List Maker")
window.geometry("300x170")
#Label
fileSelectLBL = tkinter.Label(window, text="Please select your file:")
fileSelectLBL.pack()
#Button
def load_file():
global fname
fname = askopenfilename(filetypes=(("Text files", "*.txt"),
("All files", "*.*") ))
filename = tkinter.Button(window, text="Browse", command = load_file)
filename.pack()
#List Name
listNameLBL = tkinter.Label(window,text="Please enter what you want to call the list:")
listNameLBL.pack()
listName = Entry(window)
listName.pack()
#Save List
saveListLBL = tkinter.Label(window,text="Please select where you want to save the list:")
saveListLBL.pack()
def save_file():
global saveName
saveName = asksaveasfilename(filetypes=(("Text files", "*.txt"),
("All files", "*.*") ))
Savename = tkinter.Button(window, text="Browse", command = save_file)
Savename.pack()
#Go
goBTN = tkinter.Button(window, text="GO!", command = program)
goBTN.pack()
#Main Loop
window.mainloop()
Now although this code works, it is really messy. I am not that new to Python but my code is much longer and complex than what it could/should be. This was my first ever go at a program with a GUI, all of my other programs have just been run through the shell.
I am just looking for some general feedback on how I can improve this, and make it shorter and more efficient.