Python Forum
[Tkinter] Setting Binding to Entry created with a loop? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Setting Binding to Entry created with a loop? (/thread-22741.html)



Setting Binding to Entry created with a loop? - p_hobbs - Nov-25-2019

I'm not even really sure what to google to find what I'm looking for. So I haven't been able to find what I'm looking for.

So, I'm creating a series of Entry widgets using a loop.

    def efs_widgets(self):
        elems = ['Cu',
                 'Fe',
                 'S',
                 'SiO2',
                 'CaO',
                 'Al2O3',
                 'Fe3O4']
        row_count = 4
        for i in range(len(elems)):
            self.tb = "efs_" + elems[i] + "_Box"
            self.tb = Entry(self.frame2, width=18, bg="#E6EAEC")

            if (i%2)==0:
                self.tb.grid(row=row_count, column=1, padx=1, pady=5)                
            else:
                self.tb.grid(row=row_count, column=4, padx=1, pady=5)
                row_count = row_count+1
Then when I try to set a binding to the entry widget I get an error saying that there is no attribute

    def efs_form(self):
        def chb_click(event):
    
            self.avg_string = ""
            self.in_avg = ""
            if self.chb_var == 1:
                self.chb_var = 0
                self.avg_string = "NOT included in the Average."
                print(self.avg_string)
            else:
                self.chb_var = 1
                self.avg_string = "Included in the Average."
                self.in_avg = "X"
                print(self.avg_string)

        self.reset_form()
        self.efs_btn["bg"]='Red'
        self.db_conn = sqlite3.connect('EFS_Samples.db')
        self.c = self.db_conn.cursor()
        self.chb_var = 1
        self.next_box = True
        # Create Database Table
        self.c.execute(""" CREATE TABLE IF NOT EXISTS efs(
            _Timestamp text,
            Arrival_Time text,
            Sample_Time text,
            analyst text,
            cro text,
            cu text,
            fe text,
            s text,
            si text,
            ca text,
            al text,
            mag text,
            avg text        
            )""")
        self.efs_widgets()
        
        self.c.execute("SELECT * FROM efs")
        self.prev_sams = self.c.fetchmany(20) 
        
        self.efs_prev_lb.delete(0, END)
        self.efs_analyst_lb.delete(0, END)
        self.efs_cu_lb.delete(0, END)
        self.efs_avg_lb.delete(0, END)
        
        for sam in self.prev_sams:
            self.efs_prev_lb.insert(0, sam[2])
            self.efs_analyst_lb.insert(0, sam[3])
            self.efs_cu_lb.insert(0, sam[5])
            self.efs_avg_lb.insert(0, sam[12])
        
        self.efs_Cu_Box = ""
  
  #THIS IS WHERE I NEED THE HELP    
        #Events
        self.efs_arr_Time.bind("<FocusIn>", self.focus_in)
        self.efs_sam_Time.bind("<FocusIn>", self.focus_in)
        self.efs_analyst.bind("<<ComboboxSelected>>", self.focus_in)
        self.efs_cro.bind("<<ComboboxSelected>>", self.focus_in)
        self.efs_Cu_Box.bind("<FocusIn>", self.focus_in)
        self.efs_Fe_Box.bind("<FocusIn>", self.focus_in)
        self.efs_S_Box.Bind("<FocusIn>", self.focus_in)
        self.efs_SiO2_Box.Bind("<FocusIn>", self.focus_in)
        self.efs_CaO_Box.Bind("<FocusIn>", self.focus_in)
        self.efs_Al2O3_Box.Bind("<FocusIn>", self.focus_in)
        self.efs_Fe3O4_Box.Bind("<FocusIn>", self.focus_in)
        self.efs_avg_chb.bind("<Button-1>", chb_click)
        
        self.db_conn.commit()
        self.c.close()
        self.db_conn.close()
Can I get some help how I can approach this? Thanks. It would be much appreciated.


RE: Setting Binding to Entry created with a loop? - Larz60+ - Nov-25-2019

Quote:I get an error saying that there is no attribute
Please always post the error traceback, complete and unaltered, it contains valuable information that shows what led up to the error.