1
\$\begingroup\$

My program allows employee's to request python scripts from me and the development team. It allows the user to enter fields and a specific developer to complete the task. Then once they click on the request button it compiles all the fields and sends them in an email to the specified developer. (Sorry for the length)

import sys

try:
    from Tkinter import *
except ImportError:
    from tkinter import *

try:
    import ttk
    py3 = 0
except ImportError:
    import tkinter.ttk as ttk
    py3 = 1

import Script_Requestor_support
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import os
from tkinter import messagebox
from datetime import datetime

today = datetime.today().strftime('%d/%m/%Y')                                                                                                                                                                                                                                                                                                        

def vp_start_gui():
    '''Starting point when module is the main routine.'''
    global val, w, root
    root = Tk()
    Script_Requestor_support.set_Tk_var()
    top = Script_Requestor (root)
    Script_Requestor_support.init(root, top)
    root.mainloop()

w = None
def create_Script_Requestor(root, *args, **kwargs):
    '''Starting point when module is imported by another program.'''
    global w, w_win, rt
    rt = root
    w = Toplevel (root)
    Script_Requestor_support.set_Tk_var()
    top = Script_Requestor (w)
    Script_Requestor_support.init(w, top, *args, **kwargs)
    return (w, top)

def destroy_Script_Requestor():
    global w
    w.destroy()
    w = None


class Script_Requestor:
    def __init__(self, top=None):
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85' 
        _ana2color = '#d9d9d9' # X11 color: 'gray85' 
        font10 = "-family {Courier New} -size 10 -weight normal -slant"  \
            " roman -underline 0 -overstrike 0"
        font11 = "-family {Segoe UI} -size 6 -weight normal -slant "  \
            "roman -underline 0 -overstrike 0"
        font9 = "-family {Segoe UI} -size 9 -weight normal -slant "  \
            "roman -underline 0 -overstrike 0"
        self.style = ttk.Style()
        if sys.platform == "win32":
            self.style.theme_use('winnative')
        self.style.configure('.',background=_bgcolor)
        self.style.configure('.',foreground=_fgcolor)
        self.style.configure('.',font="TkDefaultFont")
        self.style.map('.',background=
            [('selected', _compcolor), ('active',_ana2color)])

        top.geometry("460x495+619+303")
        top.resizable(0,0)
        top.title("Script Requestor")
        root.iconbitmap("myIcon.ico")
        top.configure(background="#d9d9d9")
        top.configure(highlightbackground="#d9d9d9")
        top.configure(highlightcolor="black")

        def reset_script():
            self.vE1.set('')
            self.vE2.set('')
            self.vE3.set('')
            self.vE4.set('')
            self.Text1.delete("1.0",END)

        def send_req():
            et1 = self.vE1.get() #job
            self.vE1.set('')
            et2 = self.vE2.get() #requestor_email
            self.vE2.set('')
            et3 = self.vE3.get() #date_requested
            self.vE3.set('')
            et4 = self.vE4.get() #due_date
            self.vE4.set('')
            checkbutton = checkb.get()
            if checkbutton == 1:
                et4 = "ASAP"
            te1 = self.Text1.get("1.0",END) #script_desc
            self.Text1.delete("1.0",END)

            if self.TCombobox1.get() == "Andrew Stafford":
                toaddr = ["", ""]
                fromaddr = ""
            elif self.TCombobox1.get() == "Dylan Logan":
                toaddr = ["", ""]
                fromaddr = ""
            elif self.TCombobox1.get() == "All":
                toaddr = ["", "", ""]
                fromaddr = ""
            elif self.TCombobox1.get() == "":
                messagebox.showinfo("Script Requestor", "No Developer Selected")
                return

            msg = MIMEMultipart()
            msg['From'] = fromaddr
            msg['To'] = ','.join(toaddr)
            msg['Subject'] = "Script Request %s - %s" % (today, et1)

            body = 'Job: %s\n\nRequestor: %s\n\nDate Requested: %s\n\nDue Date: %s\n\nScript Description: %s' % (et1, et2, et3, et4, te1)

            msg.attach(MIMEText(body, 'plain'))

            server = smtplib.SMTP('smtp.gmail.com', 587)
            server.starttls()
            server.login(fromaddr, "")
            text = msg.as_string()
            server.sendmail(fromaddr, toaddr, text)
            server.quit()

        def exit_script():
            os._exit(0)

        self.Labelframe1 = LabelFrame(top)
        self.Labelframe1.place(x=10, y=10, height=55, width=440)
        self.Labelframe1.configure(relief=GROOVE)
        self.Labelframe1.configure(foreground="black")
        self.Labelframe1.configure(text='''Developer''')
        self.Labelframe1.configure(background="#d9d9d9")
        self.Labelframe1.configure(highlightbackground="#d9d9d9")
        self.Labelframe1.configure(highlightcolor="black")
        self.Labelframe1.configure(width=440)

        developers = ["Andrew Stafford", "Dylan Logan", "All"]

        self.TCombobox1 = ttk.Combobox(top)
        self.TCombobox1.place(x=20, y=30, height=20, width=420)
        self.TCombobox1.configure(textvariable=Script_Requestor_support.combobox)
        READONLY = 'readonly'
        self.TCombobox1.configure(state=READONLY)
        self.TCombobox1.configure(values=developers)
        self.TCombobox1.configure(takefocus="")

        self.Label1 = Label(top)
        self.Label1.place(relx=0.08, rely=0.14, height=21, width=71)
        self.Label1.configure(activebackground="#f9f9f9")
        self.Label1.configure(activeforeground="black")
        self.Label1.configure(background="#d9d9d9")
        self.Label1.configure(disabledforeground="#a3a3a3")
        self.Label1.configure(foreground="#000000")
        self.Label1.configure(highlightbackground="#d9d9d9")
        self.Label1.configure(highlightcolor="black")
        self.Label1.configure(text='''Job Num''')

        self.vE1 = StringVar()
        self.Entry1 = Entry(top, textvariable=self.vE1)
        self.Entry1.place(x=100, y=70, height=20, width=344)
        self.Entry1.configure(background="white")
        self.Entry1.configure(disabledforeground="#a3a3a3")
        self.Entry1.configure(font=font9)
        self.Entry1.configure(foreground="#000000")
        self.Entry1.configure(highlightbackground="#d9d9d9")
        self.Entry1.configure(highlightcolor="black")
        self.Entry1.configure(insertbackground="black")
        self.Entry1.configure(selectbackground="#c4c4c4")
        self.Entry1.configure(selectforeground="black")

        self.Label2 = Label(top)
        self.Label2.place(relx=0.02, rely=0.2, height=21, width=91)
        self.Label2.configure(activebackground="#f9f9f9")
        self.Label2.configure(activeforeground="black")
        self.Label2.configure(anchor=E)
        self.Label2.configure(background="#d9d9d9")
        self.Label2.configure(disabledforeground="#a3a3a3")
        self.Label2.configure(foreground="#000000")
        self.Label2.configure(highlightbackground="#d9d9d9")
        self.Label2.configure(highlightcolor="black")
        self.Label2.configure(text='''Requestor Email''')

        self.vE2 = StringVar()
        self.Entry2 = Entry(top, textvariable=self.vE2)
        self.Entry2.place(x=100, y=100, height=20, width=344)
        self.Entry2.configure(background="white")
        self.Entry2.configure(disabledforeground="#a3a3a3")
        self.Entry2.configure(font=font9)
        self.Entry2.configure(foreground="#000000")
        self.Entry2.configure(highlightbackground="#d9d9d9")
        self.Entry2.configure(highlightcolor="black")
        self.Entry2.configure(insertbackground="black")
        self.Entry2.configure(selectbackground="#c4c4c4")
        self.Entry2.configure(selectforeground="black")

        checkb = IntVar()
        self.Checkbutton1 = Checkbutton(top)
        self.Checkbutton1.place(x=380, y=130, height=25, width=57)
        self.Checkbutton1.configure(activebackground="#d9d9d9")
        self.Checkbutton1.configure(activeforeground="#000000")
        self.Checkbutton1.configure(background="#d9d9d9")
        self.Checkbutton1.configure(disabledforeground="#a3a3a3")
        self.Checkbutton1.configure(foreground="#000000")
        self.Checkbutton1.configure(highlightbackground="#d9d9d9")
        self.Checkbutton1.configure(highlightcolor="black")
        self.Checkbutton1.configure(justify=LEFT)
        self.Checkbutton1.configure(text='''ASAP''')
        self.Checkbutton1.configure(variable=checkb)

        self.Label3 = Label(top)
        self.Label3.place(relx=0.04, rely=0.26, height=21, width=88)
        self.Label3.configure(activebackground="#f9f9f9")
        self.Label3.configure(activeforeground="black")
        self.Label3.configure(background="#d9d9d9")
        self.Label3.configure(disabledforeground="#a3a3a3")
        self.Label3.configure(foreground="#000000")
        self.Label3.configure(highlightbackground="#d9d9d9")
        self.Label3.configure(highlightcolor="black")
        self.Label3.configure(text='''Date Requested''')

        self.vE3 = StringVar()
        self.Entry3 = Entry(top, textvariable=self.vE3)
        self.Entry3.place(x=20, y=160, height=20, width=205)
        self.Entry3.configure(background="white")
        self.Entry3.configure(disabledforeground="#a3a3a3")
        self.Entry3.configure(font=font9)
        self.Entry3.configure(foreground="#000000")
        self.Entry3.configure(highlightbackground="#d9d9d9")
        self.Entry3.configure(highlightcolor="black")
        self.Entry3.configure(insertbackground="black")
        self.Entry3.configure(selectbackground="#c4c4c4")
        self.Entry3.configure(selectforeground="black")

        self.Label4 = Label(top)
        self.Label4.place(relx=0.51, rely=0.26, height=21, width=58)
        self.Label4.configure(activebackground="#f9f9f9")
        self.Label4.configure(activeforeground="black")
        self.Label4.configure(background="#d9d9d9")
        self.Label4.configure(disabledforeground="#a3a3a3")
        self.Label4.configure(foreground="#000000")
        self.Label4.configure(highlightbackground="#d9d9d9")
        self.Label4.configure(highlightcolor="black")
        self.Label4.configure(text='''Due Date''')

        self.vE4 = StringVar()
        self.Entry4 = Entry(top, textvariable=self.vE4)
        self.Entry4.place(x=240, y=160, height=20, width=205)
        self.Entry4.configure(background="white")
        self.Entry4.configure(disabledforeground="#a3a3a3")
        self.Entry4.configure(font=font9)
        self.Entry4.configure(foreground="#000000")
        self.Entry4.configure(highlightbackground="#d9d9d9")
        self.Entry4.configure(highlightcolor="black")
        self.Entry4.configure(insertbackground="black")
        self.Entry4.configure(selectbackground="#c4c4c4")
        self.Entry4.configure(selectforeground="black")

        self.Label5 = Label(top)
        self.Label5.place(relx=0.04, rely=0.37, height=16, width=58)
        self.Label5.configure(activebackground="#f9f9f9")
        self.Label5.configure(activeforeground="black")
        self.Label5.configure(background="#d9d9d9")
        self.Label5.configure(disabledforeground="#a3a3a3")
        self.Label5.configure(font=font11)
        self.Label5.configure(foreground="#000000")
        self.Label5.configure(highlightbackground="#d9d9d9")
        self.Label5.configure(highlightcolor="black")
        self.Label5.configure(text='''(DD/MM/YYYY)''')

        self.Label6 = Label(top)
        self.Label6.place(relx=0.52, rely=0.37, height=16, width=58)
        self.Label6.configure(activebackground="#f9f9f9")
        self.Label6.configure(activeforeground="black")
        self.Label6.configure(background="#d9d9d9")
        self.Label6.configure(disabledforeground="#a3a3a3")
        self.Label6.configure(font=font11)
        self.Label6.configure(foreground="#000000")
        self.Label6.configure(highlightbackground="#d9d9d9")
        self.Label6.configure(highlightcolor="black")
        self.Label6.configure(text='''(DD/MM/YYYY)''')

        self.Labelframe2 = LabelFrame(top)
        self.Labelframe2.place(x=10, y=200, height=225, width=440)
        self.Labelframe2.configure(relief=GROOVE)
        self.Labelframe2.configure(foreground="black")
        self.Labelframe2.configure(text='''Description''')
        self.Labelframe2.configure(background="#d9d9d9")
        self.Labelframe2.configure(highlightbackground="#d9d9d9")
        self.Labelframe2.configure(highlightcolor="black")
        self.Labelframe2.configure(width=440)

        self.Text1 = Text(top)
        self.Text1.place(x=20, y=220, height=195, width=420)
        self.Text1.configure(background="white")
        self.Text1.configure(font=font9)
        self.Text1.configure(foreground="black")
        self.Text1.configure(highlightbackground="#d9d9d9")
        self.Text1.configure(highlightcolor="black")
        self.Text1.configure(insertbackground="black")
        self.Text1.configure(selectbackground="#c4c4c4")
        self.Text1.configure(selectforeground="black")
        self.Text1.configure(width=414)
        self.Text1.configure(wrap=WORD)

        self.Button1 = Button(top)
        self.Button1.place(relx=0.02, rely=0.87, height=54, width=127)
        self.Button1.configure(activebackground="#d9d9d9")
        self.Button1.configure(activeforeground="#000000")
        self.Button1.configure(background="#d9d9d9")
        self.Button1.configure(disabledforeground="#a3a3a3")
        self.Button1.configure(foreground="#000000")
        self.Button1.configure(highlightbackground="#d9d9d9")
        self.Button1.configure(highlightcolor="black")
        self.Button1.configure(pady="0")
        self.Button1.configure(text='''Reset''', command=reset_script)

        self.Button2 = Button(top)
        self.Button2.place(relx=0.7, rely=0.87, height=54, width=127)
        self.Button2.configure(activebackground="#d9d9d9")
        self.Button2.configure(activeforeground="#000000")
        self.Button2.configure(background="#d9d9d9")
        self.Button2.configure(disabledforeground="#a3a3a3")
        self.Button2.configure(foreground="#000000")
        self.Button2.configure(highlightbackground="#d9d9d9")
        self.Button2.configure(highlightcolor="black")
        self.Button2.configure(pady="0")
        self.Button2.configure(text='''Request''', command=send_req)

        self.Button3 = Button(top)
        self.Button3.place(relx=0.37, rely=0.87, height=54, width=117)
        self.Button3.configure(activebackground="#d9d9d9")
        self.Button3.configure(activeforeground="#000000")
        self.Button3.configure(background="#d9d9d9")
        self.Button3.configure(disabledforeground="#a3a3a3")
        self.Button3.configure(foreground="#000000")
        self.Button3.configure(highlightbackground="#d9d9d9")
        self.Button3.configure(highlightcolor="black")
        self.Button3.configure(pady="0")
        self.Button3.configure(text='''Quit''', command=exit_script)






if __name__ == '__main__':
    vp_start_gui()
\$\endgroup\$
1
\$\begingroup\$

Welcome to Code Review.

  1. Do not do a wildcard import. It pollutes your program environment. Only import the methods/classes you ARE going to use.
  2. Rearrange all imports so that standard libraries are imported first, and any third-party packages are imported later
  3. Define constant/global variables in UPPER_CASE. Instead of having a constant value for today, you can declare it as a lambda so that it always returns the current date in case of long-running programs
  4. Do not reference onto global values of w, val etc. Pass them as function arguments.
  5. Give your variables better and intuitive names. w does not signify anything to a new developer.
  6. Instead of the following

    _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
    _fgcolor = '#000000'  # X11 color: 'black'
    _compcolor = '#d9d9d9' # X11 color: 'gray85'
    _ana1color = '#d9d9d9' # X11 color: 'gray85'
    _ana2color = '#d9d9d9' # X11 color: 'gray85'
    

    assign the same values together:

    _bgcolor = _compcolor = _ana1color = _ana2color = '#d9d9d9'  # X11 color: 'gray85'
    _fgcolor = '#000000'  # X11 color: 'black'
    

    and use them as such instead of magic strings:

    top.configure(background="#d9d9d9")
    top.configure(highlightbackground="#d9d9d9")
    
  7. Avoid usage/calling of protected members (os._exit(0)). Tkinter provides both .quit() and .destroy() methods. You may choose to destroy the root object.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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