0

I am currently trying to code a programme with 3 comboboxes which are populated. For an example when the user picks "a" in the first combobox a new combobox should appear with new choices when "next" is pressed. Again the user puts in a value in the second combobox and presses "next", here a new combobox should appear with new choices. But somehow i cannot manage to show the third combobox.

Below the code is stated.

from tkinter import ttk
from tkinter import Tk

root = Tk()

root.geometry("400x400")

cmb = ttk.Combobox(root, width="10", values=['a','b'])

def checkcmbo():


    if cmb.get() == "a":

         cmb2 = ttk.Combobox(root, width="10", values=['aa','ab'])

         cmb2.place(relx="0.1",rely="0.2")

         if cmb2.get() == "aa":

             cmb3 = ttk.Combobox(root, width="10", values=['car1','car2','car3'])

             cmb3.place(relx="0.1",rely="0.3")

         if cmb2.get() == "ab":

             cmb3 = ttk.Combobox(root, width="10", values=['ship1','ship2','ship3'])

             cmb3.place(relx="0.1",rely="0.3")



    if cmb.get() == "b":

         cmb2 = ttk.Combobox(root, width="10", values=['ba','bb'])

         cmb2.place(relx="0.1",rely="0.2")

         if cmb2.get() == "ba":

             cmb3 = ttk.Combobox(root, width="10", values=['racecar1','racecar2','racecar3'])

             cmb3.place(relx="0.1",rely="0.3")

         if cmb2.get() == "ab":

             cmb3 = ttk.Combobox(root, width="10", values=['reaceship1','raceship2','raceship3'])

             cmb3.place(relx="0.1",rely="0.3")



cmb.place(relx="0.1",rely="0.1")

btn = ttk.Button(root, text="Next",command=checkcmbo)

btn.place(relx="0.5",rely="0.1")

root.mainloop()
5
  • 1
    When you press "next" button each time,it will create a new Combobox widget and assign it to your cmb2.So every time cmb2.get() is "". Commented Apr 14, 2020 at 11:03
  • 1
    You want something like that: dependant-combobox Commented Apr 14, 2020 at 12:09
  • 1
    You should bind <<ComboboxSelected>> event on cmb and cmb2 to change the options of cmb2 and cmb3 respectively inside the event callback. Commented Apr 14, 2020 at 12:19
  • @acw1668. Thank you for aswering. I am not quite sure i understand what you mean, but i think you are right. Can you elaborate a little. Thank you very much in advance. Commented Apr 14, 2020 at 17:00
  • When you bind <<ComboboxSelected>> event on a combobox, the event callback will be executed whenever an item of the combobox is selected. So you can populate another combobox in the event callback based on the selected item. Commented Apr 14, 2020 at 17:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.