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
| #!/usr/bin/python
# -*- coding: utf-8 -*-
# Python 3.4
from __future__ import division
import os,sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
###################################################################################################
class Feuille( QWidget) :
def __init__(self, parent = None) :
super(Feuille, self).__init__( parent)
self.setObjectName("Feuille")
self.parent = parent
self.tableur = QTableWidget()
self.tableur.setRowCount(4)
self.tableur.setColumnCount(2)
tuple_entete = ("Prix", "Unité")
self.tableur.setHorizontalHeaderLabels(tuple_entete)
tuple_produit = ("Produit 1", "Produit 2", "Produit 3","Produit 4")
self.tableur.setVerticalHeaderLabels(tuple_produit)
for cpt in range(self.tableur.rowCount()):
prx = QTableWidgetItem(str(float(cpt * 10)))
prx.setTextAlignment(Qt.AlignCenter)
prx.setForeground(Qt.red)
self.tableur.setItem(cpt, 0, prx)
texte = QTableWidgetItem("")
texte.setTextAlignment(Qt.AlignCenter)
texte.setFlags(Qt.NoItemFlags) # non modifiable
self.tableur.setItem(cpt, 1, texte)
self.tableur.cellChanged.connect(self.control_saisie)
layout = QVBoxLayout()
layout.addWidget(self.tableur)
self.setLayout(layout)
def control_saisie(self):
caractere_autorise = (0,1,2,3,4,5,6,7,8,9,".")
txt = self.tableur.currentItem().text()
if txt in caractere_autorise :
pass
else :
txt = txt[0:-1]
self.tableur.setCurrentItem(float(txt))
#############################################################################
if __name__ == "__main__":
app = QApplication (sys.argv)
fp = Feuille()
fp.show()
sys.exit(app.exec_())
app.MainLoop() |
Partager