0

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

  • 对于每个条目,都会运行碳并将标签打印到屏幕上
  • 每次单击 button2 时,我都希望清除输入框,并希望所有 4 个标签(footprint_label、recommendation_label、no_recommendation_label、no_recognition_label)重新设置 - 再次运行 carbon 并将标签再次打印在同一位置

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

  • 每次运行程序时,新标签都会在窗口中向下移动。我希望这些保持不变
  • 如果在 carbon 函数中没有运行标签,我会得到一个 NameError。我不确定如何解决此名称错误。

文件“”,第 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()
4

0 回答 0