I am having an issue creating dependent dropdowns based on dictionary values, I am very new to Python but have experience with SAS, SQL, VBA, and minor HTML. I was able to import an Excel table into Python for use and when I print it it comes out like this:
{'one':['Blue', 'Green'],'two':['Green','Red'],'three':['Orange']}
What I am trying to do is turn that into two drop-downs, the first having the options:
one
two
three
and then the associated values populate the second drop-down. For example, if "two" is selected, I want "Green" and "Red" to be the available options.
I am using Python 3.12, Pandas, and Qt6 (Qt5 code examples did not work) to do this.
I have tried searching online for multiple days and, unfortunately, keep running into various errors. I am unable to post the code I am using as this is for work and thus proprietary (I am actually working from home and posting this question from my home PC as my work blocks most sites). I was able to pull the key into the first dropdown, but that is as far as I have successfully gotten. I understand that without the code it makes it more difficult to answer completely, I hope that if someone knows a basic way to use that dictionary to solve this issue I should be able to reverse engineer it to work with my existing code.
you can use addItems()
method to add you options to your combobox
so your code will be like this
self.data = {'one':['Blue', 'Green'],'two':['Green','Red'],'three':['Orange']}
self.combobox = QComboBox()
self.combobox.addItems(data.keys())
self.combobox2 = QComboBox()
well data.keys()
return list of keys in your dict
and you can set signal like this when selected item changed
self.combobox.currentTextChanged.connect(self.myfunc)
and myfunc
will be like this
def myfunc(self,selectedText):
self.combobox2.additems(self.data[selectedText])
so when first combobox change the second will be change but you should make sure to clear second combobox befor set any item to it to do this you can use clear
method like this
def myfunc(self,selectedText):
self.combobox2.clear()
self.combobox2.additems(self.data[selectedText])
this is full example i made to see how its work
from PyQt6.QtWidgets import QComboBox, QMainWindow, QApplication, QWidget, QVBoxLayout
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.data = {'one':['Blue', 'Green'],'two':['Green','Red'],'three':['Orange']}
self.combobox = QComboBox()
self.combobox.addItems(self.data.keys()) # self.data.keys() returns -> ['one', 'two', 'three']
self.combobox2 = QComboBox()
# i added this line becuse first key is selected by defulte
self.combobox2.addItems(self.data[self.combobox.currentText()])
layout = QVBoxLayout()
layout.addWidget(self.combobox)
layout.addWidget(self.combobox2)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
self.combobox.currentTextChanged.connect(self.changeSecondCombobox)
def changeSecondCombobox(self,selectedText):
self.combobox2.clear()
self.combobox2.addItems(self.data[selectedText])
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec()