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
   | from tkinter import *
import threading
import sys
import time
import queue
 
fin_thread_data_G = False
 
taille_data_G = 0
taille_lu_G = 0
trame_G = ""
nb_trame_G = 0
def Traitement(octet_P) :
	global taille_data_G
	global taille_lu_G
	global fifo_ihm_G
	global trame_G
	global nb_trame_G
 
	if taille_data_G == 0 :
		taille_data_G = 44 # taille de la chaine
		trame_G = ""
	if taille_lu_G < taille_data_G :
		taille_lu_G = taille_lu_G + 1
		trame_G = trame_G + chr(octet_P)
 
	if taille_data_G > 0 and taille_data_G == taille_lu_G :
		trame_G = trame_G + " " + str(nb_trame_G) + "\n"
 
		array_texte = []
		temp = trame_G.split("[texte")
		array_texte.append({"text":temp[0], "color":"grey"})
		tmp = temp[1].split("[/texte]")
		c = tmp[0].split("]")
		array_texte.append({"text":c[1], "color": c[0][1:]})
		array_texte.append({"text":tmp[1], "color":"grey"})
 
		nb_trame_G = nb_trame_G + 1
		taille_data_G = 0
		taille_lu_G = 0
		while fifo_ihm_G.full == True :
			time.sleep(1)
		fifo_ihm_G.put(array_texte)
 
 
def Thread_Data() :
	global fifo_data_G
	global fin_thread_data_G
 
	with open("data.txt", "rb") as file : # 98819 éléments
		octets = file.read()
		for octet in octets :
			while fifo_data_G.full == True :
				time.sleep(1)
			fifo_data_G.put(octet)
	file.close()
	print("\tfin Thread_Data")
	fin_thread_data_G = True
	return 0
 
 
tag = 0
def update() :
	global fifo_ihm_G
	global text
	global ihm
	global tag
 
	nb = 0
 
	while nb < 500 and fifo_ihm_G.empty() == False :
		try :
			texte = fifo_ihm_G.get(True, 0.0005)
			for t in texte :
				text.insert("end", t["text"])
				start = "end -%dc" % len(t["text"])
				end = "end -1c"
				text.tag_add(tag, start, end)
				text.tag_config(tag, foreground=t["color"])
				tag = tag + 1
			nb = nb + 1
		except queue.empty :
			print("empty %d" % nb)
	print("fifo:%d" % nb)
	text.see("end")
 
	ihm.after(100, update)
 
def Thread_IHM() :
	global text
	global ihm
 
	ihm = Tk()
	ihm.grid()
 
	text = Text(ihm);
	text.grid(row=0, column=0, sticky="NSEW")
	scrollbar = Scrollbar(ihm, command=text.yview)
	scrollbar.grid(row=0, column=1, sticky="NS")
 
	ihm.grid_rowconfigure(0, weight=1)
	ihm.grid_columnconfigure(0, weight=1)
	ihm.grid_columnconfigure(1, weight=1)
 
	ihm.after(100, update)
 
	ihm.mainloop()
 
def Thread_Parse() :
	while fin_thread_data_G == False or fifo_data_G.empty() == False :
		while fifo_data_G.empty() == True :
			time.sleep(1)
		octet = fifo_data_G.get(True, 0.5)
		Traitement(octet)
	print("\tfin Thread_Parse")
	return 0
 
def Main() :
	t_ihm = threading.Thread(target=Thread_IHM)
	t_data = threading.Thread(target=Thread_Data)
	t_parse = threading.Thread(target=Thread_Parse)
 
	t_ihm.start()
	t_data.start()
	t_parse.start()
 
	t_ihm.join()
	t_data.join()
	t_parse.join()
 
fifo_data_G = queue.Queue()
fifo_ihm_G = queue.Queue()
Main() | 
Partager