首页 > 解决方案 > 如何重置在封闭范围内分配之前引用的 tkinter 中的标签

问题描述

除非单击“退出”,否则我希望我的管道(碳和透明)运行。

目前我遇到的可以使用帮助的问题是:

文件“”,第 101 行,在 clear_widget_text Recommendation_label.config(text = '') NameError: free variable 'recommendation_label' 在封闭范围内分配之前引用

 #objective: allow user to input a string 
        #run carbon() on the string after "subtmit" button is clicked
        #allow a reset with "try another item" button 
        #run carbon again 
    
    
    import gensim
    import os
    from filea import *
    from fileb import *
    import tkinter as tk
    #from tkinter import ttk
    from PIL import ImageTk,Image 
    
    #creates pop-up window
    window = tk.Tk()
    window.title('Title')
    window.geometry("900x900+10+20")
    
    #icon
    window.iconbitmap('filepath')
    
    '''
    #dynamic background image
    def resize_image(event):
        new_width = event.width
        new_height = event.height
        image = copy_of_image.resize((new_width, new_height))
        photo = ImageTk.PhotoImage(image)
        label.config(image = photo)
        label.image = photo #avoid garbage collection
    
    
    image = Image.open('filepath')
    copy_of_image = image.copy()
    photo = ImageTk.PhotoImage(image)
    label = tk.Label(canvas, image = photo)
    
    label.bind('<Configure>', resize_image)
    label.pack(fill=tk.BOTH, expand = tk.YES)
    '''
    
    #user input
    #extract user input
    text = tk.StringVar()
    entry = tk.Entry(window,textvariable=text, bg='white',fg='black', bd=5)
    entry.pack()
    entry.place(x=10, y=150)
    entry_label=tk.Label(window, text= "Please enter your item", fg='black', font=("Arial", 16))
    entry_label.place(x=10, y=120)
    
    def carbon():
        food_choice = entry.get()
        try:
            item = recognize_item(food_choice)
        
            #item footprint calculated in another module
            footprint_label=tk.Label(window, text= f"Estimated footprint of {food_choice} is : {item['Footprint']} kg of CO2 per kg", fg='black', font=("Arial", 16))
            footprint_label.place(x=0, y=190)
            footprint_label.pack()
            #recommendations
            user_recommendation = recommendation(item['FullName'])
            if len(user_recommendation) != 0:
                #position needs to be dynamic
                recommendation_label=tk.Label(window, text= "We recommend these alternatives: \n"+f"{user_recommendation[['FullName','Footprint']]}", fg='black', font=("Helvetica", 16))
                recommendation_label.place(x=10, y=230)
                recommendation_label.pack()
            else:  
                # There are no items in the cluster that have a lower footprint!
                no_recommendation_label = tk.Label(window, text= "Good Choice!", fg='red', font=("Helvetica", 16))
                no_recommendation_label.place(x=10, y=300) 
                no_recommendation_label.pack()
        except:
            no_recognition_label=tk.Label(window, text= "Sorry, we didn't recognize the item. Try again", fg='red', font=("Helvetica", 16))
            no_recognition_label.place(x=30, y=80)
            no_recognition_label.pack()
        
        finally:
     
            def clear_text():
                entry.delete(0, 'end')
            def clear_widget_text():
                footprint_label.config(text = '')
                recommendation_label.config(text = '')
                no_recommendation_label.config(text = '')
                no_recognition_label.config(text = '')
            def clear():
                clear_text()
                clear_widget_text()
                
            button_try2 = tk.Button(window, text="Try another item", command = clear)
            button_try2.pack()
            button_try2.place(x=45, y=180) 
    
    
    button_submit = tk.Button(window, text="Submit item", command = carbon)
    button_submit.pack()
    button_submit.place(x=220, y=150)  
    
    #quit
    button_quit = tk.Button(window, text='Quit', command=window.quit)
    button_quit.place(x=100, y=600)
    button_quit.pack()
    
    window.mainloop()

标签: pythontkintertkinter-label

解决方案


推荐阅读