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

GTK+ avec C & C++ Discussion :

Problème avec Gtk Entry


Sujet :

GTK+ avec C & C++

  1. #1
    Futur Membre du Club Avatar de CreativeC
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2019
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2019
    Messages : 7
    Points : 6
    Points
    6
    Par défaut Problème avec Gtk Entry
    Bonjour,
    j'ai urgemment besoin d'aide avec GTK, je viens de découvrir cette bibliothèque hier, et je n'arrive pas à obtenir de texte non vide avec le composant Gtk Entry; j'ai passé pas mal de temps à arranger mon code et découvrir des manières alternative pour obtenir le texte dans le Gtk Entry, mais j'ai fait choux blanc...

    Voici ma fenêtre:


    Concrètement, le g_print et le printf lignes 60 et 61 sont vides quand bien même je mets du texte dans les deux cases avant de cliquer sur le bouton. En même temps l'erreur 701 au dessus ne s'exécute pas, donc Gtk arrive à trouver le widget.

    Peut être est ce dû au fait que je n'ai pas appris Gtk en suivant un tutoriel, et que j'ai loupé un truc basique, mais c'est pour faire une interface à un projet d'OCR (sans librairies !), donc je n'ai pas trop le temps de m'investir dans Gtk..

    Dans tout les cas merci d'avance à ceux qui prendront le temps de me répondre,

    CreativeC.

    PS: les codes
    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
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <unistd.h>
    #include <SDL2/SDL.h>
    #include <gtk/gtk.h>
     
    //#include "tools/bin/resize.h"
    //#include "separator/separator.h"
    #include "binarization/binarize.h"
    #include "SDL_Manager/import-export.h"
     
    //Safety test env
    #ifndef ENV_TEST
     
    void on_start(GtkButton *button, gpointer user_data);
     
    int main(int argc, char ** argv)
    {
        // Initializes GTK.
        gtk_init(NULL, NULL);
     
        // Loads the UI description and builds the UI.
        // (Exits if an error occurs.)
        GtkBuilder* builder = gtk_builder_new();
        GError* error = NULL;
        if (gtk_builder_add_from_file(builder, "plain.glade", &error) == 0)
        {
            g_printerr("Error loading file: %s\n", error->message);
            g_clear_error(&error);
            return 1;
        }
     
        // Gets the widgets.
        GtkWindow* window = GTK_WINDOW(gtk_builder_get_object(builder, "org.gtk.duel"));
        GtkButton* start_button = GTK_BUTTON(gtk_builder_get_object(builder, "bouton"));
     
        // Connects signal handlers.
        g_signal_connect(start_button, "clicked",G_CALLBACK(on_start), NULL);
        // Runs the main loop.
        gtk_main();
        return 0;
    }
    void on_start(GtkButton *button, gpointer user_data){
        GtkBuilder* builder = gtk_builder_new();
        GError* error = NULL;
        if (gtk_builder_add_from_file(builder, "plain.glade", &error) == 0)
        {
            g_printerr("Error loading file: %s\n", error->message);
            g_clear_error(&error);
            return 1;
        }
        GtkEntry* entry_in = GTK_ENTRY(gtk_builder_get_object(builder, "in"));
        GtkEntry* entry_out = GTK_ENTRY(gtk_builder_get_object(builder, "out"));
        const gchar * ptdr = gtk_entry_get_text( entry_in );
        char * inStr = (char*) ptdr;
        if (!ptdr)
          return 701;
        g_print(ptdr);
        printf("==%s\n",inStr);
        char * outStr = (char*) gtk_entry_get_text( entry_out );
        int x;
        int * px = &x;
        int y;
        int * py = &y;
        SDL_Surface * image = load_image(inStr, px, py);
        Uint8 * boardg = NULL;
        boardg = malloc(x * y * sizeof(Uint8));
        gray(image,boardg);
        Uint8 * board2 = NULL;
        board2 = malloc(x * y * sizeof(Uint8));
        binarize(boardg,board2,x,y);
        save(board2,x,y,outStr);
        printf("\npaula\n\n");
        //free(boardg);
        free(board2);
        //*/
        gtk_main_quit();
        //return 0;
    }
     
    #endif
    Code XML : 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
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Generated with glade 3.36.0 -->
    <interface>
      <requires lib="gtk+" version="3.12"/>
      <object class="GtkWindow" id="org.gtk.duel">
        <property name="name">org.gtk.duel</property>
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="title" translatable="yes">OCR Elysium</property>
        <child>
          <object class="GtkGrid">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkEntry" id="in">
                <property name="name">in</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="halign">center</property>
                <property name="valign">center</property>
              </object>
              <packing>
                <property name="left_attach">1</property>
                <property name="top_attach">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkEntry" id="out">
                <property name="name">out</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="halign">center</property>
                <property name="valign">center</property>
              </object>
              <packing>
                <property name="left_attach">1</property>
                <property name="top_attach">1</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="bouton">
                <property name="label" translatable="yes">Démarrer</property>
                <property name="name">bouton</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="halign">center</property>
                <property name="valign">center</property>
              </object>
              <packing>
                <property name="left_attach">1</property>
                <property name="top_attach">2</property>
              </packing>
            </child>
            <child>
              <placeholder/>
            </child>
            <child>
              <placeholder/>
            </child>
            <child>
              <placeholder/>
            </child>
          </object>
        </child>
        <child type="titlebar">
          <placeholder/>
        </child>
      </object>
    </interface>

  2. #2
    Membre éprouvé Avatar de balkany
    Homme Profil pro
    Touriste
    Inscrit en
    Juillet 2017
    Messages
    346
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Touriste

    Informations forums :
    Inscription : Juillet 2017
    Messages : 346
    Points : 977
    Points
    977
    Par défaut
    Je n'ai pas testé ton code, mais à la lecture, je pense que ton problème vient de ce que tu utilises deux GtkBuilders, donc deux instanciations de tes objets.
    Il faudrait que tu n'utilises que celui dans main(), puis que tu le passes comme donnée utilisateur dans on_start() :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    g_signal_connect (start_button, "clicked", G_CALLBACK (on_start), builder);

  3. #3
    Futur Membre du Club Avatar de CreativeC
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2019
    Messages
    7
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2019
    Messages : 7
    Points : 6
    Points
    6
    Par défaut
    Cela a marché, merci beaucoup !

  4. #4
    Expert confirmé
    Avatar de gerald3d
    Homme Profil pro
    Conducteur de train
    Inscrit en
    Février 2008
    Messages
    2 291
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 53
    Localisation : France, Côte d'Or (Bourgogne)

    Informations professionnelles :
    Activité : Conducteur de train
    Secteur : Transports

    Informations forums :
    Inscription : Février 2008
    Messages : 2 291
    Points : 4 941
    Points
    4 941
    Billets dans le blog
    5
    Par défaut
    Bonjour.

    Tout à fait d'accord avec balkany.

    J'ai lancé ton code. Voila les premières constatations. J'ai supprimer les fonctions de la SDL et de binarize pour aller au droit au problème :
    • Le builder doit être transmis au callback on_start ();,
    • La fenêtre principale ne gère pas sa fermeture lors de l'appui sur la croix,
    • on_start (); est de type void alors que tu tentes de renvoyer des valeurs de type int,
    • g_print(); attend un format de donnée. D'où le warning lors de la compilation. son utilisation est : g_print("%s", ptdr);,
    • Il faut libérer le builder une fois son utilisation terminée


    Voila ton code modifié en conséquence histoire que tu comprennes les tenants et aboutissants :
    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
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <unistd.h>
    #include <SDL2/SDL.h>
    #include <gtk/gtk.h>
    
    //#include "tools/bin/resize.h"
    //#include "separator/separator.h"
    //#include "binarization/binarize.h"
    //#include "SDL_Manager/import-export.h"
    
    //Safety test env
    #ifndef ENV_TEST
    
    int on_start(GtkButton *button, gpointer user_data);
    
    int main(int argc, char ** argv)
    {
        // Initializes GTK.
        gtk_init(NULL, NULL);
    
        // Loads the UI description and builds the UI.
        // (Exits if an error occurs.)
        GtkBuilder* builder = gtk_builder_new();
        GError* error = NULL;
        if (gtk_builder_add_from_file(builder, "plain.glade", &error) == 0)
        {
            g_printerr("Error loading file: %s\n", error->message);
            g_clear_error(&error);
            return 1;
        }
    
        // Gets the widgets.
        GtkWindow* window = GTK_WINDOW(gtk_builder_get_object(builder, "org.gtk.duel"));
        GtkButton* start_button = GTK_BUTTON(gtk_builder_get_object(builder, "bouton"));
    
        // Connects signal handlers.
        g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
        g_signal_connect(start_button, "clicked",G_CALLBACK(on_start), builder);
        // Runs the main loop.
        gtk_main();
    
        /* Libération du GtkBuilder */
        g_object_unref (builder);
    
        return 0;
    }
    int on_start(GtkButton *button, gpointer user_data){
      GtkBuilder* builder = (GtkBuilder*)user_data;;
    
        GtkEntry* entry_in = GTK_ENTRY(gtk_builder_get_object(builder, "in"));
        GtkEntry* entry_out = GTK_ENTRY(gtk_builder_get_object(builder, "out"));
        const gchar * ptdr = gtk_entry_get_text( entry_in );
        char * inStr = (char*) ptdr;
        if (!ptdr)
          return 701;
        g_print("%s", ptdr);
        printf("==%s\n",inStr);
        char * outStr = (char*) gtk_entry_get_text( entry_out );
        int x;
        int * px = &x;
        int y;
        int * py = &y;
        //    SDL_Surface * image = load_image(inStr, px, py);
        Uint8 * boardg = NULL;
        boardg = malloc(x * y * sizeof(Uint8));
        //    gray(image,boardg);
        Uint8 * board2 = NULL;
        board2 = malloc(x * y * sizeof(Uint8));
        //    binarize(boardg,board2,x,y);
        //    save(board2,x,y,outStr);
        printf("\npaula\n\n");
        //free(boardg);
        free(board2);
        //*/
        gtk_main_quit();
        return 0;
    }
    
    #endif

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème avec GTK Ada et les Drawing_Area
    Par Sigurd9 dans le forum Ada
    Réponses: 5
    Dernier message: 07/04/2012, 22h36
  2. Problème avec gtk.Label
    Par velight dans le forum GTK+ avec Python
    Réponses: 2
    Dernier message: 14/11/2009, 10h42
  3. Trois problèmes avec GTK+ sous windows xp qui n'ont pas de solutions
    Par smalldebian dans le forum GTK+ avec C & C++
    Réponses: 0
    Dernier message: 16/09/2009, 15h01
  4. Problème avec GTK radio button
    Par biscoto dans le forum GTK+ avec C & C++
    Réponses: 0
    Dernier message: 03/06/2008, 19h31
  5. Problème avec Gtk
    Par Feysal dans le forum GTK+ avec C & C++
    Réponses: 1
    Dernier message: 05/10/2006, 14h02

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