Python Forum
Linking Comboboxes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Linking Comboboxes
#21
Looks like there are multiple problems. The first problem is that you don't update the cboAddr combo box when you make a selection from the cboCust combo box. You execute this code once when work.__init()__ is called.
self.cboCust['values'] = (fill_custbox())
self.cboCust.bind('<<ComboboxSelected>>', fill_addrbox)
self.cboAddr['values'] = (fill_addrbox())  # Only time ever called.  Why the extra parenthesis?
However there is no code that sets cboAddr['values'] ever again. You may call fill_addrbox(), but the values are never used.

I would forget about returning the list of options and instead set the combo options inside the callback
        def fill_custbox(*args):   # THIS WORKS
            print('Enter fill_custbox()', args) # <- For debugging purposes
            conn = sqlite3.connect("service.db")
            cur = conn.cursor()
            cur.execute("SELECT DISTINCT cust FROM ticket Order BY cust")
            data = []
            for row in cur.fetchall():
                print(row[0])
                data.append(row[0])
            # return data
            self.cboCust['values'] = data
            conn.commit()  # <- These were never called.  was that an error?
            conn.close()
             
             
        def fill_addrbox(*args):   # THIS DOES NOT WORK
            print('Enter fill_addrbox()', args) # <- For debugging purposes
            conn = sqlite3.connect("service.db")
            cur = conn.cursor()
            cur.execute("SELECT distinct addr FROM ticket WHERE cust = '%s'" % (self.cboCust.get(),))
            self.cboAddr.delete(0, END)
            data = []
            for row in cur.fetchall():
                data.append(row[0])
                print(row[0])  #<-  If you add
            # return data
            self.cboAddr['values'] = data 
            conn.commit()  # <- Once again, these never get called.  Error?
            conn.close()
Now that code I mentioned at the top is changed to this:
self.cboCust.bind('<<ComboboxSelected>>', fill_addrbox)
fill_addrbox()
Reply
#22
(Feb-01-2021, 10:41 PM)deanhystad Wrote: Looks like there are multiple problems. The first problem is that you don't update the cboAddr combo box when you make a selection from the cboCust combo box. You execute this code once when work.__init()__ is called.
self.cboCust['values'] = (fill_custbox())
self.cboCust.bind('<<ComboboxSelected>>', fill_addrbox)
self.cboAddr['values'] = (fill_addrbox())  # Only time ever called.  Why the extra parenthesis?
However there is no code that sets cboAddr['values'] ever again. You may call fill_addrbox(), but the values are never used.

I would forget about returning the list of options and instead set the combo options inside the callback
        def fill_custbox(*args):   # THIS WORKS
            print('Enter fill_custbox()', args) # <- For debugging purposes
            conn = sqlite3.connect("service.db")
            cur = conn.cursor()
            cur.execute("SELECT DISTINCT cust FROM ticket Order BY cust")
            data = []
            for row in cur.fetchall():
                print(row[0])
                data.append(row[0])
            # return data
            self.cboCust['values'] = data
            conn.commit()  # <- These were never called.  was that an error?
            conn.close()
             
             
        def fill_addrbox(*args):   # THIS DOES NOT WORK
            print('Enter fill_addrbox()', args) # <- For debugging purposes
            conn = sqlite3.connect("service.db")
            cur = conn.cursor()
            cur.execute("SELECT distinct addr FROM ticket WHERE cust = '%s'" % (self.cboCust.get(),))
            self.cboAddr.delete(0, END)
            data = []
            for row in cur.fetchall():
                data.append(row[0])
                print(row[0])  #<-  If you add
            # return data
            self.cboAddr['values'] = data 
            conn.commit()  # <- Once again, these never get called.  Error?
            conn.close()
Now that code I mentioned at the top is changed to this:
self.cboCust.bind('<<ComboboxSelected>>', fill_addrbox)
fill_addrbox()
You sir are a genius or at least a lot smarter than me... that is exactly what I wanted it to do, thankyou so much!!! Big Grin
Reply
#23
Sorry, last line in my post should call fill_custbox to initialize the customer list, not fill_addrbox. fill_addrbox will get filled when you select a customer
Reply
#24
A comment on
    conn.commit()  # <- These were never called.  was that an error?
and
    conn.commit()  # <- Once again, these never get called.  Error?
You make no changes in the database, so "conn.commit()" simply has no effect. It is necessary when you make changes in the database (INSERT, DELETE, UPDATE) in order to make those changes permanent and to make them visible to other users of the database. Also, in the earlier versions of your program, both "conn.commit()" and "conn.close()" come after the return statement and were not executed as pointed out by @deanhystad in the comments. If time efficiency is important, open the connection once when starting the program and close it when exiting the program. If many users and high transaction rates, then open, read, close as in the final suggestion by @deanhystad but omit the "conn.commit()" statements.
Reply
#25
(Feb-02-2021, 09:06 AM)Serafim Wrote: A comment on
    conn.commit()  # <- These were never called.  was that an error?
and
    conn.commit()  # <- Once again, these never get called.  Error?
You make no changes in the database, so "conn.commit()" simply has no effect. It is necessary when you make changes in the database (INSERT, DELETE, UPDATE) in order to make those changes permanent and to make them visible to other users of the database. Also, in the earlier versions of your program, both "conn.commit()" and "conn.close()" come after the return statement and were not executed as pointed out by @deanhystad in the comments. If time efficiency is important, open the connection once when starting the program and close it when exiting the program. If many users and high transaction rates, then open, read, close as in the final suggestion by @deanhystad but omit the "conn.commit()" statements.
Hello, I knew they didn't do anything, I was cutting and pasting and hadn't gotten around to removing them yet but thank you for your observation.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can't get tkinter database aware cascading comboboxes to update properly dford 6 3,655 Jan-11-2022, 08:37 PM
Last Post: deanhystad
  Print Values from a Sequence of Entries / ComboBoxes MC2020 4 2,819 Mar-28-2020, 10:05 PM
Last Post: MC2020
  [PyQt] making dependant comboBoxes Hitsugaya 3 5,032 May-23-2019, 06:05 PM
Last Post: Alfalfa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020