IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

wxPython Discussion :

Connaitre la couleur d'un élément graphique wxPython


Sujet :

wxPython

  1. #1
    Candidat au Club
    Homme Profil pro
    Directeur de projet
    Inscrit en
    Mai 2015
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Directeur de projet
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2015
    Messages : 2
    Points : 2
    Points
    2
    Par défaut Connaitre la couleur d'un élément graphique wxPython
    Bonjour,

    Est-il possible de connaitre la couleur d'un élément créé dans une fenêtre wxPython?
    J'ai crée une trame contenant 12 000 carrés construit avec la fonction DrawRectangle.
    Si j'arrive bien a changer la couleur de chacun d'après son id avec la fonction FloodFill, je n'arrive pas à lire sa couleur de fond.

    J'ai déjà réalisé une fonction similaire sous Tkinter en utilisant la fonction coul=can.itemcget(id,'fill')

    Une autre piste serait de trouvé la couleur d'un pixel dont je connais la position dans ma fenêtre.
    Dans les essais que j'ai fait jusqu'à maintenant je n'ai que trouvé la couleur d'un pixel en fonction de sa position dans mon écran et non dans ma fenetre.

    Si quelqu'un a une solution, je suis preneur.
    Merci

  2. #2
    Candidat au Club
    Homme Profil pro
    Directeur de projet
    Inscrit en
    Mai 2015
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Directeur de projet
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2015
    Messages : 2
    Points : 2
    Points
    2
    Par défaut
    Voici une petite partie du code pour test.

    Clic gauche permet de noircir, clic droit de blanchir une case.

    Le TextCtrl permet de sélectionner la case à noircir, le bouton OK noirci la case.

    Le but étant de savoir quelles cases sont noires, de l'enregistrer et de le restituer.
    Pour celà il faut déterminer la couleur de la case.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
     
    #!/usr/bin/python
    # -*- coding: iso-8859-15 -*-
     
    import wx
     
    class MyCanvas(wx.Frame):
        def __init__(self, titre):
            wx.Frame.__init__(self, parent = None, id = 1, title = titre, size = (1200, 1000))
     
            self.conteneur = wx.Panel(self, 1, size = self.GetClientSize())
     
            # Initialise les différents Sizer
            vbox = wx.BoxSizer(wx.VERTICAL)
     
            self.bordure1 = wx.BoxSizer(wx.VERTICAL)
            self.Commandes = wx.BoxSizer(wx.VERTICAL)
     
            # Création du Widget wxStaticBox 
            self.staticBox1 = wx.StaticBox ( self.conteneur, -1, 'Dessin' )
            self.staticBoxSizer1 = wx.StaticBoxSizer ( box = self.staticBox1, orient=wx.VERTICAL )
     
            # Ecran "Map"
            self.sizer_Map = wx.BoxSizer(orient=wx.VERTICAL)
     
            # Création du panel "P_Map"
            global P_Map
            P_Map = Interface_Map(self.conteneur)
            self.sizer_Map.AddSizer(P_Map, 1, border=0, flag=wx.EXPAND)
            self.staticBoxSizer1.AddSizer(self.sizer_Map, 1, border=0, flag=wx.EXPAND)
     
          #-----------------------------------------------------------
          # Configure the box sizers
            vbox .Add(self.Commandes)
            self.bordure1.Add(self.staticBoxSizer1, 1, wx.EXPAND | wx.ALL, 10)
            self.bouton_ok=wx.Button(self,wx.ID_OK)
            self.Commandes.Add(self.bouton_ok, flag=wx.ALIGN_RIGHT | wx.ALL, border=10)
            self.Cellule=wx.TextCtrl(self,-1,"123", style=wx.TE_RIGHT)
            self.Commandes.Add(self.Cellule)
     
            self.bouton_ok.Bind(wx.EVT_BUTTON,P_Map.Id_Noir)
     
          # Attach everything
            self.conteneur.SetSizerAndFit (self.bordure1)
            vbox.Add(self.conteneur, 1, wx.EXPAND)
     
     
     
          # Rendu final du sizer
            self.SetSizer(vbox)
            self.pdc = wx.PseudoDC()
     
     
           # self.cel=self.Cellule.GetValue()
     
     
     
     
     
    #-------------------------------------------------------------------------
    class Interface_Map(wx.ScrolledWindow):
        """Création de l'interface """
    #-------------------------------------------------------------------------
        def __init__(self, conteneur):
     
            wx.ScrolledWindow.__init__(self, conteneur)
     
            # Map "Dessin" :
            self.SetBackgroundColour("white") # Couleur 'Blanc'
     
            # create a PseudoDC to record our drawing
            self.pdc = wx.PseudoDC()
            self.OnAddGrille(self.pdc)
     
            # Evenements
            self.Bind(wx.EVT_PAINT, self.OnPaint)
            self.Bind(wx.EVT_ERASE_BACKGROUND, lambda x:None)
            self.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseMove)
            self.Bind(wx.EVT_LEAVE_WINDOW, self._onLeaveWindow)
            self.Bind(wx.EVT_ENTER_WINDOW, self._onEnterWindow)
     
            # vars for handling mouse clicks
            self.dragid = -1
            self.lastpos = (0,0)
     
     
     
        #*********************************************************
     
     
        def Id_Noir (self,evt):
     
            # Recherche de la position
            position = self.pdc.GetIdBounds(int(fen.Cellule.GetValue()))
     
            self.pdc.SetBrush(wx.Brush("black"))
            self.pdc.FloodFill(position[0], position[1], "white", style=wx.FLOOD_SURFACE)
            self.RefreshRect(position, False)
     
     
     
        def OnPaint(self, event):
            # Create a buffered paint DC.  It will create the real
            # wx.PaintDC and then blit the bitmap to it when dc is
            # deleted.
            dc = wx.BufferedPaintDC(self)
            # use PrepateDC to set position correctly
            self.PrepareDC(dc)
            # we need to clear the dc BEFORE calling PrepareDC
            bg = wx.Brush(self.GetBackgroundColour())
            dc.SetBackground(bg)
            dc.Clear()
            self.pdc.DrawToDC(dc)
     
        #---------------------------------------------------------
        def _onLeaveWindow(self, event):
            self.dragidLeft = 0
            self.dragidRight = 0
     
        def _onEnterWindow(self, event):
            self.dragidLeft = 0
            self.dragidRight = 0
     
        def OnMouseMove(self, event):
     
            self.cel=fen.Cellule.GetValue()         
     
            if event.LeftDown():
                self.dragidLeft = 1
     
            if event.RightDown():
                self.dragidRight = 1
     
            if wx.EVT_MOTION and (self.dragidLeft or self.dragidRight):
     
                x,y = event.GetPosition()
                l = self.pdc.FindObjects(x, y, 1)
                for id in l:
                    if not self.pdc.GetIdGreyedOut(id):
                        self.dragid = id
                        break
                if self.dragidLeft: 
                    self.pdc.SetBrush(wx.Brush("black"))
                    self.pdc.FloodFill(x, y, "white", style=wx.FLOOD_SURFACE)
     
                if self.dragidRight: 
                    self.pdc.SetBrush(wx.Brush("white"))
                    self.pdc.FloodFill(x, y, "black", style=wx.FLOOD_SURFACE)
                r = self.pdc.GetIdBounds(self.dragid)
     
                self.RefreshRect(r, False)
     
     
            if event.LeftUp():
                self.dragidLeft = 0
                self.dragid = -1
            if event.RightUp():
                self.dragidRight = 0
                self.dragid = -1
     
        #---------------------------------------------------------
        def OffsetRect(self, r):
            xView, yView = self.GetViewStart()
            xDelta, yDelta = self.GetScrollPixelsPerUnit()
            r.OffsetXY(-(xView*xDelta),-(yView*yDelta))
     
        #---------------------------------------------------------
     
     
        def OnAddGrille(self,dc):
                self.pDC = dc
                self.pDC.SetPen(wx.Pen('#d4d4d4'))
                self.pDC.BeginDrawing()
                self.pDC.SetPen(wx.Pen("grey",style=wx.TRANSPARENT))
                self.pDC.SetBrush(wx.Brush("white", wx.SOLID))
                taille=10
                for y in range(100):
                    for x in range(120):
                        posX=x*(taille)
                        posY=y*(taille)
                        id = wx.NewId()
                        self.pDC.SetId(id)
                        self.pDC.DrawRectangle(posX,posY,taille,taille)
                        r = wx.Rect(posX,posY,taille,taille)
                        self.pDC.SetIdBounds(id,r)
                        self.pDC.SetId
                self.pDC.EndDrawing()
                return self.pDC
     
    class MonApp(wx.App):
        def OnInit(self):
            global fen
            fen = MyCanvas("Création Trame")
            fen.Show(True)
            self.SetTopWindow(fen)
            return True
     
    app = MonApp()
    app.MainLoop()
    Fichiers attachés Fichiers attachés

Discussions similaires

  1. FAQ : Comment alterner les couleurs de chaque élément d'une liste déroulante ?
    Par yoghisan dans le forum Balisage (X)HTML et validation W3C
    Réponses: 6
    Dernier message: 04/06/2007, 14h15
  2. [XPath]Connaitre la Position() de l'élément parent
    Par virgul dans le forum XSL/XSLT/XPATH
    Réponses: 2
    Dernier message: 18/04/2007, 11h28
  3. Réponses: 6
    Dernier message: 22/12/2005, 11h32
  4. Réponses: 11
    Dernier message: 15/04/2005, 15h42
  5. [JList] changer la couleur d'UN élément
    Par youb dans le forum Composants
    Réponses: 1
    Dernier message: 31/03/2005, 11h31

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo