aaaggghhh_pain
aaaggghhh_pain

Reputation: 13

one instance of tkinter's entry get function isn't working while others around it work

In my code I have two .get functions referring to filled tkinter cells. They are entirely identical as far as I can tell. However, new_comp_name.get() works perfectly while new_comp_email.get() returns an empty value. A couple hours I give up and I thought I could get some help regarding this here. The code below is simplified but running it, I still encounter the same bizzar issue. I even resorted to restarting my computer but still, no luck. Any help would be much appreciated.

def newc_popup():
    compviewFresh()
    newc_popup = Toplevel()
    newc_popup.title("New Company")
#-----------fetch options list from types DB
    connection = sqlite3.connect('companyDB99.db')
    ###### end of connection ####

    query="SELECT type_name as class FROM types"
    r_set=connection.execute(query);
    my_list = [r for r, in r_set] # create a  list 

    options = tk.StringVar(newc_popup)

    comptypeSELECT =tk.OptionMenu(newc_popup, options, *my_list)
    #om1.grid(row=2,column=5)

#-----------

    comp_name_label = Label(newc_popup, text="Company Name")
    comp_name_label.grid(row=1, column=0)

    new_comp_name = Entry(newc_popup, width=50)
    new_comp_name.grid(row=1, column=1)

    comp_email_label = Label(newc_popup, text="Email Address")
    comp_email_label.grid(row=2, column=0)

    new_comp_email = Entry(newc_popup, width=50)
    new_comp_email.grid(row=2, column=1)

    comptypeSELECT_lable = Entry(newc_popup, width=50)
    comptypeSELECT_lable.grid(row=2, column=1)

    comptypeSELECT.grid(row=3, column=1,)

    

    def addComp():
        compviewFresh()    
        connection = sqlite3.connect('companyDB99.db')
        cursor = connection.cursor()
        print(new_comp_name.get())
        print(new_comp_email.get())

    addComp_btn = Button(newc_popup, text="Add Company", command=addComp)
    addComp_btn.grid(row=4, column=0, columnspan=2)

Upvotes: 1

Views: 40

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386342

Your call to .get is working fine. The problem is that you have two entries in the same place so you're not typing into the widget you think you're typing into.

Here's the problem:

new_comp_email.grid(row=2, column=1)
comptypeSELECT_lable.grid(row=2, column=1)

comptypeSELECT_label is an Entry, so when you think you are typing into new_comp_email you're actually typing into comptypeSELECT_label since it was added last and thus is on top of new_comp_email. Thus, new_comp_email is empty.

Upvotes: 1

Related Questions