Search code examples
pythontkinteroptionmenu

How to clear or refresh OptionMenu after submit button clicked


The OptionsMenu will not refresh or clear when the Submit button is pressed. It will write to the excel sheet. The text boxes do clear, just not the drop down.

I have tried changing '''entry1.delete(0, END)''' to '''named.delete(0,END)'''

from tkinter import *
import pandas as pd
import tkcalendar as tkc

def submit_fields():
    path = 'C:\Python32\RequestForm.xlsx'
    df1 = pd.read_excel(path)
    SeriesA = df1['Name']
    SeriesB = df1['Reason']
    SeriesC = df1['startDate']
    SeriesD = df1['endDate']
    A = pd.Series(named.get())
    B = pd.Series(reasond.get())
    C = pd.Series(entry3.get())
    D = pd.Series(entry4.get())
    SeriesA = SeriesA.append(A)
    SeriesB = SeriesB.append(B)
    SeriesC = SeriesC.append(C)
    SeriesD = SeriesD.append(D)
    df2 = pd.DataFrame({"Name":SeriesA, "Reason":SeriesB, "startDate":SeriesC, "endDate":SeriesD})
    df2.to_excel(path, index=False)
    entry1.delete(0, END)
    entry2.delete(0, END)
    entry3.delete(0, END)
    entry4.delete(0, END)


master = Tk()
master.geometry('250x250')
master.title("Request Form")

# Name dropdown menu
Names1A = ["Jarrod","Jeremy", "Joe", "John", "Rob", "Spencer"]
Label(master, text="Your Name").grid(row=0)
named = StringVar()
named.set("Choose Name")
drop = OptionMenu(master,named, *Names1A)
drop.grid(row=0, column=1)

# Reason dropdown menu
Reason1A = ["Vacation","Training", "Field Visit", "Expo", "Other"]
Label(master, text="Reason").grid(row=1)
reasond = StringVar()
reasond.set("Choose Reason")
drop1 = OptionMenu(master,reasond, *Reason1A)
drop1.grid(row=1, column=1)

# Dates
Label(master, text="Start Date").grid(row=2)
Label(master, text="End Date").grid(row=3)


entry1 = Entry(master)
entry2 = Entry(master)
entry3 = Entry(master)
entry4 = Entry(master)

#entry1.grid(row=0, column=1, pady=4)
#entry2.grid(row=1, column=1, pady=4)
entry3.grid(row=2, column=1, pady=4)
entry4.grid(row=3, column=1, pady=4)


Button(master, text='Quit', command=master.destroy).grid(row=4,column=0, pady=4)
Button(master, text='Submit', command=submit_fields).grid(row=4,column=1, pady=4)

mainloop()

Expected results are that the information is written to the excel sheet and refreshed or cleared out after the submit button is pressed.


Solution

  • hi the problem is here

     #Name dropdown menu
     Names1A = ["Jarrod","Jeremy", "Joe", "John", "Rob", "Spencer"]
     Label(master, text="Your Name").grid(row=0)
     named = StringVar()
     named.set("Choose Name")
     drop = OptionMenu(master,named, *Names1A)
     drop.grid(row=0, column=1)
    

    but the correct way is to add a StringVar() to OptionMenu and after add all your voices.

    # Name dropdown menu
     Label(master, text="Your Name").grid(row=0)
     named = StringVar()
     drop = OptionMenu(master,named, "Choose Name","Jarrod","Jeremy", "Joe", "John", "Rob", "Spencer")
     named.set("Choose Name")
     drop.grid(row=0, column=1)
    

    and when you submit,you can set your prefer value using set method of StringVar()

    entry3.delete(0, END)
    entry4.delete(0, END)
    named.set("Choose Name")
    reasond.set("Choose Reason")