Erika Erika -4 years ago 121
Python Question

Tkinter -Python3 : the value of a color change into a list

I once posted a question that was very vague and so I decided to be more precise after some research and tutorials. I looked at examples from StackOverflow, and I assembled them together to see if I can get a favorable result. (COPYRIGHT:The code is not mine!)

Here is the layout:

I allow the person to enter a number that is also the name of a rule that will be used in the program.

I have a 1 row of rectangles that looks like a board of 8 columns. Each rectangle, when clicked, changes to another color. There should be a button "OK" that once the person has chose the rule and clicked some rectangles, it should be sent to the code. I would like that the changement of color equals to a value that enters in a list. So in other words, there are 8 values in a list to be entered into the code when the button 'OK' is clicked. For the moment, I would like to see the list that was entered to be posted on the canvas, under the board.

I don't how to add these values to the list, I am blocked when I try to name the values so that it can enter into the list easily, but it doesn't work. Thanks in advance for any advises and if it's still to vague, please let me know so that I can try to be more clear. ^^'

Here is the code that I assembled so far from examples:

from Tkinter import *

[couleur_resulat = \[\]
'black'==1
'white'==0

class One_Rectangle ():
def __init__(self, can, depart_x, depart_y, size):
self.can=can
self.id= self.can.create_rectangle((depart_x, depart_y, depart_x+size,depart_y+size), fill='white')
self.can.tag_bind(self.id, '<ButtonPress-1>', self.set_couleur)

self.couleur_change=True

def set_couleur(self, event=None):
self.couleur_change = not self.couleur_change
couleur='white'
if not self.couleur_change:
couleur='black'

self.can.itemconfigure(self.id, fill=couleur)


fenetre =Tk()

canvas= Canvas(fenetre,width=500, height=500, background='white')

canvas.pack(side=BOTTOM, padx=5, pady=5)

OR0=One_Rectangle (canvas,110,20, 20)
OR1=One_Rectangle (canvas,130,20, 20)
OR2=One_Rectangle (canvas,150,20, 20)
OR3=One_Rectangle (canvas,170,20, 20)
OR4=One_Rectangle (canvas,190,20, 20)
OR5=One_Rectangle (canvas,210,20, 20)
OR6=One_Rectangle (canvas,230,20, 20)
OR7=One_Rectangle (canvas,250,20, 20)
OR8=One_Rectangle (canvas,270,20, 20)

B1=Button(fenetre, text='Quitter', command=fenetre.quit).pack(side=RIGHT, padx=5, pady=5)
B2=Button(fenetre, text='Relancer').pack(side=LEFT, padx=5, pady=5) #Mettre une commande pour relancer le programme lorsque c'est termine
B3=Button(fenetre, text='OK').pack(side=RIGHT,padx=5, pady=5) #On doit mettre une commande qui recoit et envoie le numero au code GET?

text1= Label(fenetre, text= 'Regles (1-255):')
entr1= Entry(fenetre)
text1.pack(side= LEFT)
entr1.pack(side=RIGHT)


fenetre.mainloop()][1]


Here is what I receive


Answer Source

If I'm understanding your question, you're looking for a way to store the color values before showing them below (on the 'OK' click).

from Tkinter import *

# Create a default for each item (what they start with)
couleur_resulat = ['white' for i in range(8)]
'black'==1
'white'==0 

class One_Rectangle (): 
    def __init__(self, can, depart_x, depart_y, size, data, index):
        self.can=can
        self.id= self.can.create_rectangle((depart_x, depart_y, depart_x+size,depart_y+size), fill='white')
        self.can.tag_bind(self.id, '<ButtonPress-1>', self.set_couleur) 

        # The same object for all items
        self._data = data
        self._index = index # The index it should be placed

        self.couleur_change=True

    def set_couleur(self, event=None): 
        self.couleur_change = not self.couleur_change
        couleur='white' 
        if not self.couleur_change: 
            couleur='black'

        # This will fill the same list across all objects
        self._data[self._index] = couleur

        self.can.itemconfigure(self.id, fill=couleur)


fenetre =Tk()

canvas= Canvas(fenetre,width=500, height=500, background='white')

canvas.pack(side=BOTTOM, padx=5, pady=5) 


OR0=One_Rectangle (canvas,110,20, 20, couleur_resulat, 0)
OR1=One_Rectangle (canvas,130,20, 20, couleur_resulat, 1)
# ... For reach rectangle                              ^ Make sure to increment


def on_OK_pressed(*args, **kwargs):
    for couleur in couleur_resulat:
        print(couleur) # Do whatever you would like with the data

This way, we get to let python handle the list management. For each of the items, we have to make sure there is default information in order to have the proper index.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download