0

I would like to have my pipeline (carbon and clear) running unless "Quit" is clicked.

  • for every entry, carbon is run and the labels are printed to the screen
  • every time button2 is clicked, I would like the entry box to clear and all the 4 labels (footprint_label, recommendation_label,no_recommendation_label,no_recognition_label) to reset -run carbon again and have the labels printed again in the same position

Currently the issues I run into that I could use help with are:

  • the new labels keep moving down the window every time the program is run. I'd like these to stay constant
  • if a label is not run in the carbon function, I get a NameError. I'm not sure how to fix this name error.

File "", line 101, in clear_widget_text recommendation_label.config(text = '') NameError: free variable 'recommendation_label' referenced before assignment in enclosing scope

 #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()
  • 1
    Please focus on only one question at a time, and try to reduce the code down to a [mcve] specifically for that specific problem. Also, please try to use text instead of images unless the problem is specifically caused by the use of an image. – Bryan Oakley Jun 20 '21 at 04:30
  • Don't define functions inside other functions, as you do under . Define them in the beginning and just call clear() after finally. Also when you define variables inside a function, they are local variables and can not be accessed by other functions. You can either pass them as arguments or make them global. Just do another tutorial to functions and it will get clearer. – steTATO Jun 22 '21 at 10:05

0 Answers0