##########################################################################################
# Create a Tkinter screen with all types of widgets
# - Upon submission: Obtain and display all values entered
# - Upon clear:      Clear all values or selections entered
##########################################################################################
from tkinter import *			            #import entire tkinter framework
from tkinter import messagebox              #import popup dialogs 

def submitEvent( ):                                         #method is called upon submit
    print("Thank you for your feedback, as follows:")       #print
    print(nameInput.get())                                  #get value entered in Entry field
    print(commentField.get('1.0',END))                      #get value entered in Text field                      
    print(sexInput.get())                                   #get value selected in Radio group
    if sglInput.get() =='1': print(sglCheckbox['text']  ,' ',end='')    #get value selected in Checkbox 
    if mrdInput.get() =='1': print(mrdCheckbox['text']  ,' ',end='')    #only display if selected = '1'
    if chldInput.get()=='1': print(chldCheckbox['text'] ,' ',end='')
    print()
    selected_idx = langListbox.curselection()               #get indexes of all selected in Listbox 
    for i in selected_idx :                                 #loop thru the list
        print(langListbox.get(i) +' ', end='')              #get value selected in Listbox 
    msgOutput.set("Thank you for your feedback")            #display message on screen
    messagebox.showinfo('Thanks', "Thank you for your feedback") #display a popup dialog
 
def clearEvent( ):                                          #method is called upon clear
    print("Your form has been cleared")                     #print
    nameInput.set('')                                       #clear value in Entry field
    commentField.delete('1.0',END)                          #clear value in Text field
    femaleRadio.select()                                    #reset radio group to female selected
    sglCheckbox.deselect()                                  #deselect all checkboxes
    mrdCheckbox.deselect()                                  
    chldCheckbox.deselect()
    langListbox.selection_clear(0,END)                      #clear all selected items in Listbox
    msgOutput.set("Cleared")                                #display message on screen                                
    
#### Create the window components ###########################################################

win = Tk( )					                    #call Tk( ) constructor to create a root window
win.title("All Tkinter window widgets")	        #set a window title
win.geometry("500x420")                         #window size

frame1 = Frame( win, bg='cyan', bd=20, highlightthickness=2, highlightbackground="blue")

langs  = ('Java','Python','JavaScript','C','C++','C#','Go','PHP','Swift','SQL') 

nameInput = StringVar()                         #create variables that capture the data entered
sexInput  = StringVar()                         
sglInput  = StringVar()
mrdInput  = StringVar()
chldInput = StringVar()
langInput = StringVar(value=langs)              #the same, + provide the values of the Listbox
msgOutput = StringVar()                         #the same, but for output

nameLabel    = Label(frame1, text="Enter you name  ", bg="cyan")        #create components
nameField    = Entry(frame1, width=40, textvariable=nameInput)

commentLabel = Label(frame1, text="Enter your comments", bg="cyan")

commentFrame  = Frame(frame1)                                           #create an inner frame
commentField  = Text( commentFrame, width=28, height=5)
commentScroll = Scrollbar(commentFrame, orient="vertical", command=commentField.yview)
commentField.config(yscrollcommand=commentScroll.set)
commentField.pack( side="left")
commentScroll.pack(side="right", fill="y")

genderLabel  = Label(frame1, text="Enter your gender", bg="cyan")
maleRadio    = Radiobutton(frame1, text="Male",     variable=sexInput, value="male",   bg='cyan')
femaleRadio  = Radiobutton(frame1, text="Female",   variable=sexInput, value="female", bg='cyan')

statusLabel  = Label(frame1, text="Enter marital status", bg="cyan")
sglCheckbox  = Checkbutton(frame1, text="Single",   variable=sglInput,  bg='cyan')
mrdCheckbox  = Checkbutton(frame1, text="Married",  variable=mrdInput,  bg='cyan') 
chldCheckbox = Checkbutton(frame1, text="Children", variable=chldInput, bg='cyan')

langLabel     = Label(frame1, text="Your favorite languages", bg="cyan")

langFrame     = Frame(frame1)
langListbox   = Listbox(langFrame, width=22, height=4,  listvariable=langInput, selectmode='extended') 
langScrollbar = Scrollbar(langFrame, orient="vertical", command=langListbox.yview)
langListbox.config(yscrollcommand=langScrollbar.set)
langListbox.pack(  side="left")
langScrollbar.pack(side="right", fill="y")

submitButton = Button(frame1, width=9, text="Submit", cursor='hand2', command=submitEvent)  #call submitEvent upon submit
clearButton  = Button(frame1, width=9, text="Clear",  cursor='hand2', command=clearEvent)   #call clearEvent upon clear

message      = Label(frame1, textvariable=msgOutput, bg="cyan")

femaleRadio.select()                                                #select a component                            
sglCheckbox.deselect()                                              #deselect a component
mrdCheckbox.deselect()
chldCheckbox.deselect()

#### Place components in window ###########################################################

nameLabel.grid(   row=0, column=0, pady=6, padx=5, sticky=E)        #place the components in the frame
nameField.grid(   row=0, column=1, pady=6, padx=5, columnspan=3)
commentLabel.grid(row=1, column=0, pady=6, padx=5, sticky=NE)
commentFrame.grid(row=1, column=1, pady=6, padx=5, columnspan=3)
genderLabel.grid( row=2, column=0, pady=6, padx=5, sticky=E)
maleRadio.grid(   row=2, column=1, pady=6, padx=5, sticky=W)
femaleRadio.grid( row=2, column=2, pady=6, padx=5, sticky=W)
statusLabel.grid( row=3, column=0, pady=6, padx=5, sticky=E)
sglCheckbox.grid( row=3, column=1, pady=6, padx=5, sticky=W)
mrdCheckbox.grid( row=3, column=2, pady=6, padx=5, sticky=W)
chldCheckbox.grid(row=3, column=3, pady=6, padx=5, sticky=W)
langLabel.grid(   row=4, column=0, pady=6, padx=5, sticky=NE)
langFrame.grid(   row=4, column=1, pady=6, padx=5, columnspan=2)
submitButton.grid(row=5, column=0, padx=5, pady=8, sticky=E)
clearButton.grid( row=5, column=1, padx=5, pady=5, sticky=W)
message.grid(     row=6, column=0, padx=5, pady=5, columnspan=6, sticky=W)

frame1.pack()                                                       #make the frame visible
win.mainloop()                                                      #make the window visible