单击按钮时如何清除tkinter标签显示

我正在开发一个微型应用程序,但是我一直想弄清楚每当我再次单击按钮时如何清除tkinter标签显示。尝试通过设置var [“ text”] =''使用功能Reset()来执行此操作,但是当我再次生成数据时,它只是继续在显示上添加数据。当我单击“执行”按钮时,也尝试重置显示标签,但无法正常工作。

感谢任何建议,谢谢。

from tkinter import Tk,StringVar,ttk,messagebox,IntVar
from tkinter import *
from PIL import ImageTk
from database import *


# def show_menu():

Data = get_database()
HEIGHT = 720
WIDTH = 1280

top1 = Tk()
top1.title("Menu Display")
canvas = Canvas(top1,height=HEIGHT,width=WIDTH)
canvas.pack()
# disables resizing of canvas
top1.resizable(False,False)


BG = ImageTk.PhotoImage(file="pic.jpg")


def qExit():
    qExit = messagebox.askyesno("Exit?","Do you want to exit?")
    if qExit > 0:
        top1.destroy()
        return

#Reset function for user to clear all the data > not working as intended to clear all fields
def Reset():
    stallinput.set("")
    dayinput.set("")
    menu_type.set("")
    menu_label['text'].set("") #This line doesn't reset the label field as intended 

menu_shown = ''
#function to format dict key,value pairs for display on GUI
def format_response(menu):
    global menu_shown       #assignment of menu_shown var before referencing below
    for food,price in menu.items():
        menu_shown = menu_shown + f'{food} ${price:4.2f} \n'  #formatted string for display

    print(menu_shown)
    return(menu_shown)

#function to index and extract data from main database
def get_menu():
    value0 = (stallinput.get())         #extracting user selected stall from dropdown box
    value1 = (dayinput.get())           #extracting user selected day from dropdown box
    time = (menu_type.get())            #extracting user selected menu type from dropdown box
    try:
        if value1 == "Monday":
            if value0 == "MacDonalds":
                if time == "Breakfast Menu":
                    menu = Data["Mon"]["MacDonalds"]["Breakfast Menu"]
                elif time == 'Lunch & Dinner Menu':
                    menu = Data['Mon']['MacDonalds']['Lunch & Dinner Menu']
            elif value0 == "Subway":
                menu = Data['Mon']['Subway']
            elif value0 == "KFC":
                menu = Data['Mon']['KFC']
            elif value0 == "Western":
                menu = Data['Mon']['Western']
            elif value0 == "Noodles store":
                menu = Data['Mon']['Noodles store']

        elif value1 == "Tuesday":
            if value0 == "MacDonalds":
                if time == 'Breakfast Menu':
                    menu = Data['Tue']['MacDonalds']['Breakfast Menu']
                elif time == 'Lunch & Dinner Menu':
                    menu = Data['Tue']['MacDonalds']['Lunch & Dinner Menu']
            elif value0 == "Subway":
                menu = Data['Tue']['Subway']
            elif value0 == "KFC":
                menu = Data['Tue']['KFC']
            elif value0 == "Western":
                menu = Data['Tue']['Western']
            elif value0 == "Noodles store":
                menu = Data['Tue']['Noodles store']

        elif value1 == "Wednesday":
            if value0 == "MacDonalds":
                if time == 'Breakfast Menu':
                    menu = Data['Wed']['MacDonalds']['Breakfast Menu']
                elif time == 'Lunch & Dinner Menu':
                    menu = Data['Wed']['MacDonalds']['Lunch & Dinner Menu']
            elif value0 == "Subway":
                menu = Data['Wed']['Subway']
            elif value0 == "KFC":
                menu = Data['Wed']['KFC']
            elif value0 == "Western":
                menu = Data['Wed']['Western']
            elif value0 == "Noodles store":
                menu = Data['Wed']['Noodles store']

        elif value1 == "Thursday":
            if value0 == "MacDonalds":
                if time == 'Breakfast Menu':
                    menu = Data['Thu']['MacDonalds']['Breakfast Menu']
                elif time == 'Lunch & Dinner Menu':
                    menu = Data['Thu']['MacDonalds']['Lunch & Dinner Menu']
            elif value0 == "Subway":
                menu = Data['Thu']['Subway']
            elif value0 == "KFC":
                menu = Data['Thu']['KFC']
            elif value0 == "Western":
                menu = Data['Thu']['Western']
            elif value0 == "Noodles store":
                menu = Data['Thu']['Noodles store']

        elif value1 == "Friday":
            if value0 == "MacDonalds":
                if time == 'Breakfast Menu':
                    menu = Data['Fri']['MacDonalds']['Breakfast Menu']
                elif time == 'Lunch & Dinner Menu':
                    menu = Data['Fri']['MacDonalds']['Lunch & Dinner Menu']
            elif value0 == "Subway":
                menu = Data['Fri']['Subway']
            elif value0 == "KFC":
                menu = Data['Fri']['KFC']
            elif value0 == "Western":
                menu = Data['Fri']['Western']
            elif value0 == "Noodles store":
                menu = Data['Fri']['Noodles store']

        elif value1 == "Saturday":
            if value0 == "MacDonalds":
                if time == 'Breakfast Menu':
                    menu = Data['Sat']['MacDonalds']['Breakfast Menu']
                elif time == 'Lunch & Dinner Menu':
                    menu = Data['Sat']['MacDonalds']['Lunch & Dinner Menu']
            elif value0 == "Subway":
                menu = Data['Sat']['Subway']
            elif value0 == "KFC":
                menu = Data['Sat']['KFC']
            elif value0 == "Western":
                menu = Data['Sat']['Western']
            elif value0 == "Noodles store":
                menu = Data['Sat']['Noodles store']

        elif value1 == "Sunday":
            if value0 == "MacDonalds":
                if time == 'Breakfast Menu':
                    menu = Data['Sun']['MacDonalds']['Breakfast Menu']
                elif time == 'Lunch & Dinner Menu':
                    menu = Data['Sun']['MacDonalds']['Lunch & Dinner Menu']
            elif value0 == "Subway":
                menu = Data['Sun']['Subway']
            elif value0 == "KFC":
                menu = Data['Sun']['KFC']
            elif value0 == "Western":
                menu = Data['Sun']['Western']
            elif value0 == "Noodles store":
                menu = Data['Sun']['Noodles store']

        menu_label['text'] = format_response(menu)

    except:
        messagebox.showinfo("Error!","Please select a stall,day and menu!")


stallinput = StringVar()    #Var for selected stall input
dayinput = StringVar()    #Var for selected day input
menu_type = StringVar()      #Var for selected menu_type input


bg_label = Label(top1,image=BG,bg='white')
bg_label.place(relwidth=1,relheight=1)

#topframe for dropdown box inputs
topframe = Frame(top1,bg='#99CCFF',bd=5)
topframe.place(relx=0.5,rely=0.05,relwidth=0.75,relheight=0.25,anchor='n')

stall_label = Label(topframe,text="Select a stall",bg = '#99CCFF',font=("Century",24,'bold'))
stall_label.place(rely=0.05,relwidth=0.4,relheight=0.25)

stall_select = ttk.Combobox(topframe,textvariable=stallinput,state='readonly',\
font=('arial',14,'bold'),width=30)
stall_select['values'] = (' ','MacDonalds','Subway','KFC','Western','Noodles Store')
stall_select.current(0)
stall_select.place(relx=0.4,relwidth=0.3,relheight=0.25)

day_label = Label(topframe,text="Select a day",'bold'))
day_label.place(rely=0.35,relheight=0.25)

day_select = ttk.Combobox(topframe,textvariable=dayinput,width=30)
day_select['values'] = (' ','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday')
day_select.current(0)
day_select.place(relx=0.4,rely=0.35,relheight=0.25)

time_label = Label(topframe,text="Select a menu",'bold'))
time_label.place(rely=0.65,relheight=0.25)

time_select = ttk.Combobox(topframe,textvariable=menu_type,width=30)
time_select['values'] = (' ','Breakfast Menu','Lunch & Dinner Menu')
time_select.current(0)
time_select.place(relx=0.4,rely=0.65,relheight=0.25)

**#Reset fields button
btnReset = Button(topframe,text="Reset",height=4,width=20,font=('Century',20),command=Reset)
btnReset.place(relx=0.75,relwidth=0.25,relheight=0.25)

#Trying to make the display label reset everytime I click on go again
go_btn = Button(topframe,text="Display Menu",command=get_menu)
go_btn.place(relx=0.75,relheight=0.25)**


#lower_frame to display menu and prices
lower_frame = Frame(top1,bd=10)
lower_frame.place(relx=0.5,relheight=0.6,anchor='n')

#Menu display
menu_label = Label(lower_frame,20,'bold'))
menu_label.place(relwidth=1,relheight=0.9)

btnExit = Button(lower_frame,text="Exit Application",'italic'),command=qExit,relief=RAISED)
btnExit.place(relx=0.5,rely=0.85,relheight=0.15,anchor='n')

top1.mainloop()
msgl88 回答:单击按钮时如何清除tkinter标签显示

您可以这样做:

label_name.configure(text="")

对不起,如果我误解了你。

本文链接:https://www.f2er.com/3124779.html

大家都在问