I haven't written any articles for a long time, because I started to take charge of a module. I am so busy that I am not a module, but I am just taking charge of a module. I have no experience in many things, the scalability and robustness of the Code, as well as some script problems and system interfaces, have puzzled me a lot of time and I have crossed the river by feeling the stone. However, after a while, I was satisfied with the results. Today, when I was modifying a bug, some testers mentioned that I had to enter only numbers in the entry and asked the old programmers. This is also a programming experience and skill, take a note here.
In GTK, entry or editable does not directly provide interfaces that only allow numbers to be input, but it hides a signal. By registering this signal, it is okay to filter out characters that are not numbers.
- # Include <GTK/GTK. h>
- Static void on_entry_insert_text (gtkwidget * entry, gcahr * new_text, int new_text_length, int * position)
- {
- If (new_text [0]> '9' | new_text [0] <'0 ')
- {
- New_text [0] = '/0 ';
- }
- Return;
- }
- Int main (INT argc, char ** argv)
- {
- G_type_init ();
- Gtk_init (& argc, & argv );
- Gtkwidget * window = NULL;
- Gtkwidget * vbox = NULL;
- Gtkwidget * entry = NULL;
- Window = gtk_window_new (gtk_window_toplevel );
- Gtk_window_set_title (gtk_window (window), "entry_test ");
- Vbox = gtk_vbox_new (false, 0 );
- Gtk_container_add (gtk_container (window), vbox );
- Entry = gtk_entry_new ();
- Gtk_vbox_pack_start (gtk_box (vbox), entry, false, false, 10 );
- G_signal_connect (g_object (entry), "insert-text", g_callback (on_entry_insert_text), null );
- Gtk_widget_show_all (winodw );
- Gtk_main ();
- Return 0;
- }
Filter strings by using an "insert-text" signal, so that you can only enter numbers.
~~ End ~~