1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
from PyQt4 import QtCore
from PyQt4 import QtGui
import psycopg2
import sys
class COMBOBOX(QtGui.QWidget):
def __init__(self, ):
super(COMBOBOX, self).__init__()
self.initUI()
def initUI(self):
###Widget
self.lbl = QtGui.QLabel("TESTING", self)
self.lbl.move(50, 100)
self.lbl2 = QtGui.QLabel("index", self)
self.lbl2.move(50, 120)
self.combo = QtGui.QComboBox(self)
self.combo.setGeometry(50,20,250,20)
self.combo.activated[str].connect(self.sirdSelection)
self.setGeometry(300, 300, 400, 150)
self.setWindowTitle('QtGui.QComboBox')
self.show()
##Connexion
try:
conn = psycopg2.connect("dbname='postgis_21_sample' user='postgres' host='localhost' password='postgresql'")
except:
print "I am unable to connect to the database"
curs = conn.cursor()
##Remplissage Cb
self.listeSIRD= curs.execute("SELECT srtext FROM spatial_ref_sys;")
self.listeSIRD= curs.fetchall()
for row in self.listeSIRD :
self.combo.addItem(str(row))
##Count les lignes dans la table
self.listeNB= curs.execute("SELECT COUNT(srtext) FROM spatial_ref_sys;")
self.listeNB= curs.fetchall()
for row in self.listeNB :
print "Nombre de ligne compter dans la tabel postgis :" , self.listeNB
#Count combobox
comboNB = self.combo.count()
print"Nombre de ligne compter dans la combo : " ,comboNB
conn.close()
def sirdSelection(self,sird):
#Selection index
comboIndex =self.combo.currentIndex()
comboIndex= comboIndex+1
print"Index de la ligne selectionner : " , comboIndex
self.lbl.setText(sird)
self.lbl.adjustSize()
def main():
app = QtGui.QApplication(sys.argv)
ex = COMBOBOX()
sys.exit(app.exec_())
if __name__ == '__main__':
main() |
Partager