单击提交按钮后如何清除或刷新 OptionMenu

How to clear or refresh OptionMenu after submit button clicked

按下提交按钮时,选项菜单不会刷新或清除。它将写入 excel sheet。文本框很清晰,只是下拉框不清晰。

我试过改变 '''entry1.delete(0, END)''' 到 '''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()

预期结果是信息被写入 excel sheet 并在按下提交按钮后刷新或清除。

您好,问题出在这里

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

但正确的方法是将 StringVar() 添加到 OptionMenu,然后添加所有声音。

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

当你提交时,你可以使用 StringVar()

的 set 方法设置你喜欢的值
entry3.delete(0, END)
entry4.delete(0, END)
named.set("Choose Name")
reasond.set("Choose Reason")