Partage
  • Partager sur Facebook
  • Partager sur Twitter

Tkinter 'function' object has no attribute 'get'

ttk Entry Textvariable

Sujet résolu
    2 décembre 2020 à 11:29:03

    Bonjour,

    Je suis en train de reprendre mon code python avec tkinter afin d'utiliser la bibliothèque ttk pour un design plus sympa.

    Mais je me heurte à un point que je ne n'arrive pas à comprendre. C'est un formulaire de saisie de "thème".

    J'utilise un Entry (ligne 39) avec un textvariable (ligne 41)

    Voici mon code :

    def eTheme(*args):
        e = textinput2.get()
        print(e)
        return(e)
    
    
    # Main form for entering themes in the tb_theme database ---
    def form_theme():
        """ Formulaire principal de saisie des thèmes dans la base de données tb_theme """
        # Global declaration in order to be able to use the variables outside the function
        global tpl_theme, entry_theme, textinput2
    
        # Style ttk pour des boutons plus design
        style = ttk.Style()
        # Style bouton
        style.configure("BW.TButton", foreground = "black")
        # Style label
        style.configure("BW.TLabel", foreground='Black', background = 'Gray79')
    
        # création de la fenêtre
        tpl_theme = Toplevel()
        tpl_theme.title(" Bienvenue dans la saisie des thèmes")
        screen_x = int(tpl_theme.winfo_screenwidth())
        screen_y = int(tpl_theme.winfo_screenheight())
        tpl_theme_x = 400
        tpl_theme_y = 125
        pos_x = (screen_x // 2) - (tpl_theme_x // 2)
        pos_y = (screen_y // 2) - (tpl_theme_y // 2)
        geo = "{}x{}+{}+{}".format(tpl_theme_x, tpl_theme_y, pos_x, pos_y)
        tpl_theme.geometry(geo)
        tpl_theme.resizable(width=False, height=False)
        tpl_theme.configure(bg='Gray79')
    
        # Champs de saisie ---
        theme_label = ttk.Label(tpl_theme, text="Thème", style = "BW.TLabel", font=("Arial", 11, "bold"))
        theme_label.place(x=30, y=15)
    
        # text variable
        textinput2 = StringVar()
        # Entry
        entry_theme = ttk.Entry(tpl_theme, textvariable=textinput2, width="55")
        entry_theme.place(x=30, y=37)
        # ----
    
        rec_btn = ttk.Button(tpl_theme, text="Enregistrer", style = "BW.TButton", command=lambda: rec_theme(eTheme.get('1.0', END)))
        rec_btn.place(x=195, y=85)
     

    Mais j'ai ce message d'erreur, alors que j'ai bien fait ma fonction en ligne 1 :

        rec_btn = ttk.Button(tpl_theme, text="Enregistrer", style = "BW.TButton", command=lambda: rec_theme(eTheme.get('1.0', END)))
    AttributeError: 'function' object has no attribute 'get'





    -
    Edité par lagratteCchouette 2 décembre 2020 à 11:32:05

    • Partager sur Facebook
    • Partager sur Twitter
      2 décembre 2020 à 13:27:28

      Bonjour,

      Ben oui : eTheme.get() dans la définition de rec_btn

      eTheme n'est pas un objet de type entry box mais une fonction

      et comme le dit le message l'objet fonction n'a pas d'attribut get

      ce que tu veux faire c'est entry_theme.get(), car entry_theme est bien une entry box

      -
      Edité par Phil_1857 2 décembre 2020 à 13:28:42

      • Partager sur Facebook
      • Partager sur Twitter
        2 décembre 2020 à 14:52:35

        merci beaucoup

        mais voici le message après avoir changé mon code, qu'est-ce qui m’échappe ? :

            # text variable
            textinput2 = StringVar()
            # Entry
            entry_theme = ttk.Entry(tpl_theme, textvariable=textinput2, width="55")
            entry_theme.place(x=30, y=37)
            # ----
        
            rec_btn = ttk.Button(tpl_theme, text="Enregistrer", style = "BW.TButton", command=lambda: rec_theme(entry_theme.get('1.0', END)))
            rec_btn.place(x=195, y=85)
        


        Le message d'erreur :

            rec_btn = ttk.Button(tpl_theme, text="Enregistrer", style = "BW.TButton", command=lambda: rec_theme(entry_theme.get('1.0', END)))
        TypeError: get() takes 1 positional argument but 3 were given



        • Partager sur Facebook
        • Partager sur Twitter
          2 décembre 2020 à 16:02:25

          lagratteCchouette a écrit:

          mais voici le message après avoir changé mon code, qu'est-ce qui m’échappe ? :

          Que la méthode .get d'une Entry n'est pas celle d'un widget Text

          • Partager sur Facebook
          • Partager sur Twitter
            2 décembre 2020 à 17:20:46

            Vous êtes super, mais je ne comprends toujours pas.

            J'ai un formulaire en Toplevel pour saisir des thèmes dans un base de données Sqlite3. Je vous mets le code dans son ensemble à la fin. J'ai un bouton rec_btn pour enregister ma saisie dans la BdD SQLITE 3.

            Mais quand j'écris ceci (ligne 67):

            rec_btn = ttk.Button(tpl_theme, text="Enregistrer", style = "BW.TButton", command=lambda: rec_theme(entry_theme.get("1.0", END)))

            J'ai ce message :

                rec_btn = ttk.Button(tpl_theme, text="Enregistrer", style = "BW.TButton", command=lambda: rec_theme(entry_theme.get("1.0", END)))
            TypeError: get() takes 1 positional argument but 3 were given

            Et quand j'écris ceci pour évaluer une autre approche (ligne68) :

            rec_btn = ttk.Button(tpl_theme, text="Enregistrer", style="BW.TButton", command=lambda: rec_theme(eTheme))

            J'ai ce mesage :

                rec_btn = ttk.Button(tpl_theme, text="Enregistrer", style = "BW.TButton", command=lambda: rec_theme(entry_theme.get("1.0", END)))
            TypeError: get() takes 1 positional argument but 3 were given

            Voici mon code dans son ensemble, car là je n'arrive pas à comprendre.

            Pourriez m'expliquer comment je dois faire, s'il vous plaît ! Merci beaucoup par avance.

            # Function for saving themes in the tb_theme database ---
            def rec_theme(theme_theme):
                """ Fonction d'enregistrement des thèmes dans la base de données tb_theme """
                try:
                    connection = sqlite3.connect('mnesis.db')  # Connection to the Database
                    cursor = connection.cursor()  # cursor creation
                    new_theme = (cursor.lastrowid, theme_theme)  # Retrieving button values
                    # les (?,?,?) represent the secure values which are entered in the intermediate variable
                    cursor.execute('INSERT INTO tb_theme VALUES(?,?)', new_theme)
            
                except Exception as e:
                    print("ERREUR", e)
                    connection.rollback()
                finally:
                    cursor.close()
                    connection.commit()  # Validate the registration in the database
                    connection.close()  # Closing the connection
            
            
            def eTheme(*args):
                global textinput2
                e = textinput2.get()
                print(e)
                return(e)
            
            
            # Main form for entering themes in the tb_theme database ---
            def form_theme():
                """ Formulaire principal de saisie des thèmes dans la base de données tb_theme """
                # Global declaration in order to be able to use the variables outside the function
                global tpl_theme, entry_theme, textinput2
            
                # Style ttk pour des boutons plus design
                style = ttk.Style()
                # Style bouton
                style.configure("BW.TButton", foreground = "black")
                # Style label
                style.configure("BW.TLabel", foreground='Black', background = 'Gray79')
                # Style Entry
                style.configure("EntryStyle.TEntry", Foreground="blue",	background=[("active","red")], fieldbackground="red")
            
                # création de la fenêtre
                tpl_theme = Toplevel()
                tpl_theme.title(" Bienvenue dans la saisie des thèmes")
                screen_x = int(tpl_theme.winfo_screenwidth())
                screen_y = int(tpl_theme.winfo_screenheight())
                tpl_theme_x = 400
                tpl_theme_y = 125
                pos_x = (screen_x // 2) - (tpl_theme_x // 2)
                pos_y = (screen_y // 2) - (tpl_theme_y // 2)
                geo = "{}x{}+{}+{}".format(tpl_theme_x, tpl_theme_y, pos_x, pos_y)
                tpl_theme.geometry(geo)
                tpl_theme.resizable(width=False, height=False)
                tpl_theme.configure(bg='Gray79')
            
                # Champs de saisie ---
                theme_label = ttk.Label(tpl_theme, text="Thème", style = "BW.TLabel", font=("Arial", 11, "bold"))
                theme_label.place(x=30, y=15)
            
                # text variable
                textinput2 = StringVar()
                # Entry
                entry_theme = ttk.Entry(tpl_theme, textvariable=textinput2, width="55")
                entry_theme.place(x=30, y=37)
                # ----
            
                rec_btn = ttk.Button(tpl_theme, text="Enregistrer", style = "BW.TButton", command=lambda: rec_theme(entry_theme.get("1.0", END)))
                #rec_btn = ttk.Button(tpl_theme, text="Enregistrer", style="BW.TButton", command=lambda: rec_theme(eTheme))
                rec_btn.place(x=195, y=85)


            -
            Edité par lagratteCchouette 2 décembre 2020 à 17:32:36

            • Partager sur Facebook
            • Partager sur Twitter
              2 décembre 2020 à 18:33:19

              La méthode get d'un objet ttk.Entry ne prend pas d'arguments.
              • Partager sur Facebook
              • Partager sur Twitter

              Blond, bouclé, toujours le sourire aux lèvres...

                2 décembre 2020 à 18:51:15

                heu je suis un peu perdu, comment devrais-je l'écrire dans ce cas ?
                • Partager sur Facebook
                • Partager sur Twitter
                  2 décembre 2020 à 19:06:16

                  lagratteCchouette a écrit:

                  heu je suis un peu perdu, comment devrais-je l'écrire dans ce cas ?


                  Si cette méthode ne prends pas d'arguments, on n'en met pas.
                  • Partager sur Facebook
                  • Partager sur Twitter
                    2 décembre 2020 à 19:08:25

                    lagratteCchouette a écrit:

                    heu je suis un peu perdu, comment devrais-je l'écrire dans ce cas ?

                    bidule.get()


                    • Partager sur Facebook
                    • Partager sur Twitter

                    Blond, bouclé, toujours le sourire aux lèvres...

                      2 décembre 2020 à 19:55:03

                      Un grand très très grand merci à tous pour votre patience, c'était tout bête. Je finissais par me mélanger les yeux.

                      @loupsolitaire gracias

                      Voici mon code pour la communauté.

                      # Function for saving themes in the tb_theme database ---
                      def rec_theme(theme_theme):
                          """ Fonction d'enregistrement des thèmes dans la base de données tb_theme """
                          try:
                              connection = sqlite3.connect('mnesis.db')  # Connection to the Database
                              cursor = connection.cursor()  # cursor creation
                              new_theme = (cursor.lastrowid, theme_theme)  # Retrieving button values
                              # les (?,?,?) represent the secure values which are entered in the intermediate variable
                              cursor.execute('INSERT INTO tb_theme VALUES(?,?)', new_theme)
                      
                          except Exception as e:
                              print("ERREUR", e)
                              connection.rollback()
                          finally:
                              cursor.close()
                              connection.commit()  # Validate the registration in the database
                              connection.close()  # Closing the connection
                      
                              # Delete Entry after recs
                              entry_theme.delete('0', END)
                      
                      
                      def eTheme(*args):
                          e = textinput2.get()
                          return(e)
                      
                      
                      # Main form for entering themes in the tb_theme database ---
                      def form_theme():
                          """ Formulaire principal de saisie des thèmes dans la base de données tb_theme """
                          # Global declaration in order to be able to use the variables outside the function
                          global tpl_theme, entry_theme, textinput2
                      
                          # Style ttk pour des boutons plus design
                          style = ttk.Style()
                          # Style bouton
                          style.configure("BW.TButton", foreground = "black")
                          # Style label
                          style.configure("BW.TLabel", foreground='Black', background = 'Gray79')
                          # Style Entry
                          style.configure("EntryStyle.TEntry", Foreground="blue",	background=[("active","red")], fieldbackground="red")
                      
                          # création de la fenêtre
                          tpl_theme = Toplevel()
                          tpl_theme.title(" Bienvenue dans la saisie des thèmes")
                          screen_x = int(tpl_theme.winfo_screenwidth())
                          screen_y = int(tpl_theme.winfo_screenheight())
                          tpl_theme_x = 400
                          tpl_theme_y = 125
                          pos_x = (screen_x // 2) - (tpl_theme_x // 2)
                          pos_y = (screen_y // 2) - (tpl_theme_y // 2)
                          geo = "{}x{}+{}+{}".format(tpl_theme_x, tpl_theme_y, pos_x, pos_y)
                          tpl_theme.geometry(geo)
                          tpl_theme.resizable(width=False, height=False)
                          tpl_theme.configure(bg='Gray79')
                      
                          # Champs de saisie ---
                          theme_label = ttk.Label(tpl_theme, text="Thème", style = "BW.TLabel", font=("Arial", 11, "bold"))
                          theme_label.place(x=30, y=15)
                      
                          # text variable
                          textinput2 = StringVar()
                          # Entry
                          entry_theme = ttk.Entry(tpl_theme, textvariable=textinput2, style ="EntryStyle.TEntry", width="55")
                          entry_theme.place(x=30, y=37)
                          # ----
                      
                          rec_btn = ttk.Button(tpl_theme, text="Enregistrer", style = "BW.TButton", command=lambda: rec_theme(entry_theme.get()))
                          rec_btn.place(x=195, y=85)
                      



                      -
                      Edité par lagratteCchouette 2 décembre 2020 à 19:57:00

                      • Partager sur Facebook
                      • Partager sur Twitter

                      Tkinter 'function' object has no attribute 'get'

                      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                      • Editeur
                      • Markdown