Bonjour,
Alors je me permets de créer ce post afin de savoir si on pourrait m'aiguiller sur la façon de faire un équivalent de read_string() en ocaml mais en interface graphique avec gtk.
basé sur ce code venant d'un site (fonctionne impec)
Pour ceux que ça intéresse ce bout de code :
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 (* Initialisation de GTK. Voir la discussion plus loin. *) let _ = GMain.init () (* Fenêtre principale de l'application. *) let window = GWindow.window ~title:"Convertisseur RGB <=> HEXA" ~height:100 ~width:800 () (* Bouton qui souhaite le bonjour à l'utilisateur. *) let say_hello = GButton.button ~label:"Bonjour !" ~packing:window#add () (* Fonction qui affiche le message sur stdout. *) let print_hello () = let user = Glib.get_user_name () in Printf.printf "Bonjour %s !\n%!" (String.capitalize user) let _ = window#connect#destroy ~callback:GMain.quit; say_hello#connect#clicked ~callback:print_hello; window#show (); GMain.main ()
En gros le code m'affiche un bouton avec marqué bonjour, à chaque fois que l'on clique dessus renvoie la fonction print_hello qui affiche alors 'bonjour + nom de l'utilisateur de l'OS'.
Ce que j'aimerais faire c'est simplement pouvoir faire un formulaire afin de convertir du code hexa & rgb et vice versa.
Pour le moment j'ai démarré le programme qui s'occupe de la conversion hexa à rgb seulement voici le code :
Ps : je me doute bien qu'il y a moyen de réduire considérablement le code (fonctions récursives ? )
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 print_endline "Vous pouvez entrez votre code hexadécimal :"; print_string "#";; let lecture_string = read_line();; let n0 = ref 0;; let n1 = ref 0;; let n2 = ref 0;; let n3 = ref 0;; let n4 = ref 0;; let n5 = ref 0;; let rec verification a = if a < 6 then match lecture_string.[a] with |'0' -> begin match a with |0 -> n0 := 0 |1 -> n1 := 0 |2 -> n2 := 0 |3 -> n3 := 0 |4 -> n4 := 0 |5 -> n5 := 0 end; verification (a + 1) |'1' -> begin match a with |0 -> n0 := 1 |1 -> n1 := 1 |2 -> n2 := 1 |3 -> n3 := 1 |4 -> n4 := 1 |5 -> n5 := 1 end; verification (a + 1) |'2' -> begin match a with |0 -> n0 := 2 |1 -> n1 := 2 |2 -> n2 := 2 |3 -> n3 := 2 |4 -> n4 := 2 |5 -> n5 := 2 end; verification (a + 1) |'3' -> begin match a with |0 -> n0 := 3 |1 -> n1 := 3 |2 -> n2 := 3 |3 -> n3 := 3 |4 -> n4 := 3 |5 -> n5 := 3 end; verification (a + 1) |'4' -> begin match a with |0 -> n0 := 4 |1 -> n1 := 4 |2 -> n2 := 4 |3 -> n3 := 4 |4 -> n4 := 4 |5 -> n5 := 4 end; verification (a + 1) |'5' -> begin match a with |0 -> n0 := 5 |1 -> n1 := 5 |2 -> n2 := 5 |3 -> n3 := 5 |4 -> n4 := 5 |5 -> n5 := 5 end; verification (a + 1) |'6' -> begin match a with |0 -> n0 := 6 |1 -> n1 := 6 |2 -> n2 := 6 |3 -> n3 := 6 |4 -> n4 := 6 |5 -> n5 := 6 end; verification (a + 1) |'7' -> begin match a with |0 -> n0 := 7 |1 -> n1 := 7 |2 -> n2 := 7 |3 -> n3 := 7 |4 -> n4 := 7 |5 -> n5 := 7 end; verification (a + 1) |'8' -> begin match a with |0 -> n0 := 8 |1 -> n1 := 8 |2 -> n2 := 8 |3 -> n3 := 8 |4 -> n4 := 8 |5 -> n5 := 8 end; verification (a + 1) |'9' -> begin match a with |0 -> n0 := 9 |1 -> n1 := 9 |2 -> n2 := 9 |3 -> n3 := 9 |4 -> n4 := 9 |5 -> n5 := 9 end; verification (a + 1) |'A'|'a' -> begin match a with |0 -> n0 := 10 |1 -> n1 := 10 |2 -> n2 := 10 |3 -> n3 := 10 |4 -> n4 := 10 |5 -> n5 := 10 end; verification (a + 1) |'B'|'b' -> begin match a with |0 -> n0 := 11 |1 -> n1 := 11 |2 -> n2 := 11 |3 -> n3 := 11 |4 -> n4 := 11 |5 -> n5 := 11 end; verification (a + 1) |'C'|'c' -> begin match a with |0 -> n0 := 12 |1 -> n1 := 12 |2 -> n2 := 12 |3 -> n3 := 12 |4 -> n4 := 12 |5 -> n5 := 12 end; verification (a + 1) |'D'|'d' -> begin match a with |0 -> n0 := 13 |1 -> n1 := 13 |2 -> n2 := 13 |3 -> n3 := 13 |4 -> n4 := 13 |5 -> n5 := 13 end; verification (a + 1) |'E'|'e' -> begin match a with |0 -> n0 := 14 |1 -> n1 := 14 |2 -> n2 := 14 |3 -> n3 := 14 |4 -> n4 := 14 |5 -> n5 := 14 end; verification (a + 1) |'F'|'f' -> begin match a with |0 -> n0 := 15 |1 -> n1 := 15 |2 -> n2 := 15 |3 -> n3 := 15 |4 -> n4 := 15 |5 -> n5 := 15 end; verification (a + 1) | _ -> print_endline "echec du programme";; verification 0;; let rgb0 = !n0 * 16 + !n1;; let rgb1 = !n2 * 16 + !n3;; let rgb2 = !n4 * 16 + !n5;; print_string "Code RGB --> R : "; print_int rgb0; print_string " - G : "; print_int rgb1; print_string " - B : "; print_int rgb2; print_newline();;
Merci d'avance pour votre aide !
Partager