0

I have a requirement where i need to show relevant values in second QComboBox. Ex : If i select South from first QComboBox i.e. Region, second QComboBox i.e. cities should display only cities which are part of South. I went through lot of examples on google or youtube, in all the places they are giving data manually. But my data comes from Tableau Server(Projects, Reports) , so it should not be manual. In this example i created a dummy csv file(Original source is Tableau Server) to try. I am using QT designer to create UI file. names of comboboxes are region and cities Please find code. Sample data looks like this Region,Cities South,Hyderabad South,Bangalore North,Delhi North,Varanasi North,Simla

This is the error message i see when i run code

Traceback (most recent call last):
  File "C:\combo.py", line 31, in <module>
    welcome = onescreen()
  File "C:\combo.py", line 21, in __init__
    self.relaventcities(self.region.currentText())
TypeError: relaventcities() takes 1 positional argument but 2 were given


import sys
from PyQt5.uic import loadUi
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import  QApplication, QWidget, QDialog, 
QStackedWidget
import pandas as pd

data = pd.read_csv("C:/Users/Pavan P/Desktop/comborelevent.csv")
print(data)
region = data['Region']
cities = data['Cities']
#print(region)
#print(cities)

class onescreen(QDialog):
    def __init__(self):
        super(onescreen,self).__init__()
        loadUi("relevent.ui", self)

    def regiondata(self):
        self.region.addItems(region)
    def citiesdata(self):
        self.cities.addItems(cities)
    def sample(self):
        #self.region.addItems(region)
        # self.cities.addItems(cities)
        self.region.currentTextChanged.connect(self.relaventcities)
        # self.relaventcities(self.region.currentText())
        self.region.addItems(self.regiondata)
    def relaventcities(self, text):
        self.cities.clear()
        rps = self.citiesdata(text)
        if isinstance(rps, list):
            self.cities.addItems(rps)
        else:
            self.cities.addItems(rps)



app = QApplication(sys.argv)
welcome = onescreen()
widget = QStackedWidget()
widget.addWidget(welcome)
widget.setFixedHeight(500)
widget.setFixedWidth(800)
widget.show()
try:
    sys.exit(app.exec_())
except Exception as e:
    print(e)
  • @ThePyGuy , Thanks for the reply, i have been trying to implement that in my code but facing issues. what is elements here – Pavan Kumar Jul 29 '21 at 08:56
  • Change function definition from `def relaventcities(self)` to `def relaventcities(self, v)` then try. – ThePyGuy Jul 29 '21 at 09:01
  • Can you please check above code? i don't know where i am going wrong – Pavan Kumar Jul 29 '21 at 09:05
  • For that, you need to provide the [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) to run and test your code, that includes the content of `relevent.ui` file and a small sample from `comborelevent.csv` data file. You can edit the question and add all the necessary detials. – ThePyGuy Jul 29 '21 at 09:08
  • i think i am not allowed to attach files – Pavan Kumar Jul 29 '21 at 09:21
  • updated code in main question, please review it once – Pavan Kumar Jul 29 '21 at 09:42

0 Answers0