2
\$\begingroup\$

I have made the changes that people have suggested from my previous question. I want to try and make it even tidier if possible. What can I change to make it more optimized? My imports are quite messy and I am not sure what ones I actually need, so if you can help with that, please do.

#Imports
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

class MyFileManager(object):
    def __init__(self, filename='', save_filename=''):
        self.filename = filename
        self.save_filename = save_filename


    def save_users_list(self, list_name):
        with open(self.filename, 'r') as file:
            users_list = [line.strip() for line in file.readlines()]

        with open(self.save_filename, 'w') as file:
            file.write('{} = {}'.format(list_name, str(users_list)))


    def get_filename(self):
        self.filename = askopenfilename(filetypes=(("Text files", "*.txt"),
                                                        ("All files", "*.*")))


    def get_save_file(self):
        self.save_filename = asksaveasfilename(filetypes=(("Text files", "*.txt"),
                                                          ("All files", "*.*")))

manager = MyFileManager()

#Window
window = tkinter.Tk()
window.title("Python List Maker")
window.geometry("300x170")

#Label
file_select_label = tkinter.Label(window, text="Please select your file:")
file_select_label.pack()

#Button
filename_button = tkinter.Button(window, text="Browse", command=manager.get_filename)
filename_button.pack()

#List Name
list_name_label = tkinter.Label(window,text="Please enter what you want to call the list:")
list_name_label.pack()
list_name = Entry(window)
list_name.pack()

#Save List
save_list_label = tkinter.Label(window,text="Please select where you want to save the list:")
save_list_label.pack()
save_filename_button = tkinter.Button(window, text="Browse", command=manager.get_save_file)
save_filename_button.pack()

#Go
go_button = tkinter.Button(window, text="Go!", command=lambda: manager.save_users_list(
                                                                    list_name.get()))
go_button.pack()

#Main Loop
window.mainloop()
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The PEP8 document I linked to in my answer on your previous question has a section around imports and how you should structure them. Some of the main points:

# This is ok.
from tkinter.filedialog import askopenfilename, asksavefilename

# System libraries, then 3rd party, then local
import sys
import tkinter
import mymodule

As a general guideline, it is not prudent to do: from module import *. This is because that single line imports ALL submodules in a module which, depending on how large the module is, can pull in a lot of extra, not needed stuff.

Now, I will not tell you which imports you don't need, mainly because I feel that the 'how' and 'what' ideas behind imports is something best learned on your own with some light prodding and guidance. I will say this, these two import statements are equivalent:

import tkinter
tkinter.filedialog.askopenfilename(...)

from tkinter import askopenfilename
askopenfilename(...)

Using the above ideas, you'll find you have a few not-needed imports. Also, a quick search-and-find in a text editor will show another import that is not needed (HINT: search for showerror).

As another rule of thumb: import only what you need and be as specific as is prudent.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.