Combo box value list not updating

Question:

I made an application which includes a graphics file Part1.ui and a python file
Part1.ui includes 2 combo boxes which are named cmb_District and cmb_Tehsil
I Added a list in cmb_District and i want to make comparison in cmb_District.currentText()
If currentText() is Lahore then i want to add items in cmb_Tehsil but nothing happen.

import sys
from PyQt5.uic import loadUi
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui, QtWidgets
import csv
import pandas as pd

def Import2dFile(filename):
    array = []
    with open(filename) as f:
        mylist = list(f)
        for line in mylist:
            array.append(line)
    return array
class Mainwindow(QMainWindow):
    def __init__(self):
        super(Mainwindow,self).__init__()
        loadUi("Part1.ui",self)
        #These 2 lines are used to put funnctions on close and minimize buttons.
        #self.MinimizeButton.clicked.connect(lambda: self.showMinimized())
        #self.CrossButton.clicked.connect(lambda: self.close())
        districts = ["Attock","Bahawalnagar","Bahawalpur","Bhakkar","Chakwal","Chiniot","Dera Ghazi Khan","Faislabad","Gujranwala","Gujrat","Hafizabad","Jehlam","Jhang","Kasur","Khanewal","khusab","Lahore","Layyah","Lodhran","Mandi Bahauddin","Mian Wali","Multan","Muzaffarabad","NankanaSab","Narowal","Okara","Pakpattan","Raheem Yar Khan","Rajanpur","Rawalpindi","Sahiwal","Sarghoda","Sheikhupura","Sialkot","Toba Tek Singh","Vehari"]
        print(len(districts))
        tehsils = Import2dFile("Tehsils.txt")
        print(tehsils)
        self.cmb_District.addItems(districts)
    
        if self.cmb_District.currentText() == "Lahore":
            print("ls")
            print(str(self.cmb_District.currentText()))
            self.cmb_Tehsil.addItems(tehsils)

app = QApplication(sys.argv)
window = Mainwindow()
window.show()
sys.exit(app.exec_())

When I use !="Lahore" condition instead of =="Lahore" It works fine.I am new in PyQt please tell me what i did wrong

Asked By: lokp

||

Answers:

You have to learn signals in python
In order to use signal in combobox

cmb_District.currentTextChanged.connect(self.action_on_tex_changed)

It is simple logic you have to learn on your own

Answered By: plop
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.