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

Python Discussion :

tkinter message erreur - AttributeError: 'NoneType' [Python 3.X]


Sujet :

Python

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé Avatar de lagratteCchouette
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    190
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 190
    Par défaut tkinter message erreur - AttributeError: 'NoneType'
    Bonjour à tous,

    Je suis en python 3 et suis entrain de structurer mes codes pour mieux écrire.
    Je souhaite bien structurer mon code de l’interface tkinter.
    Pourriez-vous s'il vous plaît m'aider, merci beaucoup par avance.

    Voici mon main.py et j'ai désactivé # certaines linges de commande pour comprendre ce qui se passe car j'ai ce message à la ligne 61, j'ai cherché... mais

    Traceback (most recent call last):
    File "C:\PythonDeveloppement\main.py", line 61, in <module>
    mainframe.config(menu=menubar) # Configuration et print menu
    AttributeError: 'NoneType' object has no attribute 'config'
    main.py

    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
     
    # -*- coding: utf-8 -*-
    from varfunction import *
     
    def set_frame():
    #================== Création de la fenêtre principale ================
        fenetre_pce =Tk() # Fenêtre prinicpale
        fenetre_pce.title("Menesis... se souvenir !")
        screen_x = int(fenetre_pce.winfo_screenwidth())   # Fonction centrer fenêtre
        screen_y = int(fenetre_pce.winfo_screenheight())
        fenetre_pce_x = 1260
        fenetre_pce_y = 768
        pos_x = (screen_x // 2) - (fenetre_pce_x // 2)
        pos_y = (screen_y // 2) - (fenetre_pce_y // 2)
        geo = "{}x{}+{}+{}".format(fenetre_pce_x, fenetre_pce_y, pos_x, pos_y)   # Attention le 1er facteur est x ensuite +
        fenetre_pce.geometry(geo)
        fenetre_pce.resizable(width=True,height=True) # Fenêtre modifiable True or False
        fenetre_pce.iconbitmap("img/search.ico") ### Changement de l'icone de la fenêtre [l'icone doit être placé dans le répertoire racine de l'application ou un autre répertoire img/...]
     
     
    if __name__ == '__main__':
        mainframe = set_frame()
     
        #=================== Création d'un menu ==============================
        menubar = Menu(mainframe,borderwidth=20, relief=GROOVE) # Création de la FRAME pour placer les menus dans le constructeur  ==> fenetre =Tk() 
        menuFichier = Menu(menubar, tearoff=0) # Création du menu "Fichier"
        menubar.add_cascade(label="Fichier", menu=menuFichier) # Rattachement des commandes au menu "Fichier"
        menuFichier.add_command(label="Créer")
        menuFichier.add_command(label="Ouvrir",)
        menuFichier.add_command(label="Editer")
        menuFichier.add_separator() # Ligne de séparation
        #menuFichier.add_command(label="Quitter",command=mainframe.quit)
     
        menuEdition = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Edition", menu=menuEdition)
        menuEdition.add_command(label="Couper")
        menuEdition.add_command(label="Copier")
        menuEdition.add_command(label="Coller")
        menuEdition.add_command(label="Créer")
     
        menuAuteur = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Auteur", menu=menuAuteur)
        #menuAuteur.add_command(label="Créer", command=write_auteur)
        #menuAuteur.add_command(label="Consulter")
     
        menuCitation = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Citation", menu=menuCitation)
        #menuCitation.add_command(label="Créer", command=write_citation)
        #menuCitation.add_command(label="Consulter", command=result_citation)
     
        menuLien = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="jw.org", menu=menuLien)
        menuLien.add_command(label="Jw", command=open_jw_org)
        menuLien.add_command(label="Bibliothèque", command=open_jw_wol)
     
     
        menuAide = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Aide", menu=menuAide)
        menuAide.add_command(label="A propos") 
     
        mainframe.config(menu=menubar) # Configuration et print menu
        #----------------------------------------------------------------
     
     
        #Citation accueil
        lblcitation = Label(mainframe, text=" Recherches et connaissance", font=("Arial", 14,"bold"))
        lblcitation.pack()
     
        # Fond d'écran accueil
        frontimage = ImageTk.PhotoImage(Image.open("img/connaissance_1260-720.jpg") ) # image de fond page d'accueil
        frontimagelabel = Label(mainframe, image=frontimage)
        frontimagelabel.pack(expand=1)  
     
        #== Fermeture de la boucle principale ==
        mainframe.mainloop() # Boucle principale

  2. #2
    Membre très actif

    Homme Profil pro
    Bidouilleur
    Inscrit en
    Avril 2016
    Messages
    721
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Bidouilleur

    Informations forums :
    Inscription : Avril 2016
    Messages : 721
    Billets dans le blog
    1
    Par défaut
    Salut.

    set_frame retourne None, mais il faut qu'elle retourne ? Je te laisse deviner.

  3. #3
    Membre confirmé Avatar de lagratteCchouette
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    190
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 190
    Par défaut None
    Merci beaucoup bistouille, effectivement j'avais oublié le return au sortir de la fonction.

  4. #4
    Membre confirmé
    Avatar de ChMuX
    Homme Profil pro
    SysOps DevOps AWS
    Inscrit en
    Mai 2012
    Messages
    167
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : SysOps DevOps AWS

    Informations forums :
    Inscription : Mai 2012
    Messages : 167
    Par défaut
    Ta fonction ne retourne rien car tu n'as pas mis la directive "return" à la fin donc forcement

  5. #5
    Membre confirmé Avatar de lagratteCchouette
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    190
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 190
    Par défaut Return
    merci je viens tout juste de m'en apercevoir grâce à vos remarques.

    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
     
    # -*- coding: utf-8 -*-
    from varfunction import *
     
    def set_frame():
     
        #================== Création de la fenêtre principale ================
        fenetre_pce =tk.Tk() # Fenêtre prinicpale
        fenetre_pce.title("Menesis... se souvenir !")
        screen_x = int(fenetre_pce.winfo_screenwidth())   # Fonction centrer fenêtre
        screen_y = int(fenetre_pce.winfo_screenheight())
        fenetre_pce_x = 1260
        fenetre_pce_y = 768
        pos_x = (screen_x // 2) - (fenetre_pce_x // 2)
        pos_y = (screen_y // 2) - (fenetre_pce_y // 2)
        geo = "{}x{}+{}+{}".format(fenetre_pce_x, fenetre_pce_y, pos_x, pos_y)   # Attention le 1er facteur est x ensuite +
        fenetre_pce.geometry(geo)
        fenetre_pce.resizable(width=True,height=True) # Fenêtre modifiable True or False
        fenetre_pce.iconbitmap("img/search.ico") ### Changement de l'icone de la fenêtre [l'icone doit être placé dans le répertoire racine de l'application ou un autre répertoire img/...]
        return fenetre_pce # ne pas oublier de retourner 
     
    if __name__ == '__main__':
        mainframe = set_frame()
     
        #=================== Création d'un menu ==============================
        menubar = Menu(mainframe,borderwidth=20, relief=GROOVE) # Création de la FRAME pour placer les menus dans le constructeur  ==> fenetre =Tk() 
        menuFichier = Menu(menubar, tearoff=0) # Création du menu "Fichier"
        menubar.add_cascade(label="Fichier", menu=menuFichier) # Rattachement des commandes au menu "Fichier"
        menuFichier.add_command(label="Créer")
        menuFichier.add_command(label="Ouvrir",)
        menuFichier.add_command(label="Editer")
        menuFichier.add_separator() # Ligne de séparation
        #menuFichier.add_command(label="Quitter",command=mainframe.quit)
     
        menuEdition = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Edition", menu=menuEdition)
        menuEdition.add_command(label="Couper")
        menuEdition.add_command(label="Copier")
        menuEdition.add_command(label="Coller")
        menuEdition.add_command(label="Créer")
     
        menuAuteur = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Auteur", menu=menuAuteur)
        #menuAuteur.add_command(label="Créer", command=write_auteur)
        #menuAuteur.add_command(label="Consulter")
     
        menuCitation = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Citation", menu=menuCitation)
        #menuCitation.add_command(label="Créer", command=write_citation)
        #menuCitation.add_command(label="Consulter", command=result_citation)
     
        menuLien = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="jw.org", menu=menuLien)
        menuLien.add_command(label="Jw", command=open_jw_org)
        menuLien.add_command(label="Bibliothèque", command=open_jw_wol)
     
     
        menuAide = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Aide", menu=menuAide)
        menuAide.add_command(label="A propos") 
     
        mainframe.config(menu=menubar) # Configuration et print menu
        #----------------------------------------------------------------
     
     
        #Citation accueil
        lblcitation = Label(mainframe, text=" Recherches et connaissance", font=("Arial", 14,"bold"))
        lblcitation.pack()
     
        # Fond d'écram accueil
        frontimage = ImageTk.PhotoImage(Image.open("img/connaissance_1260-720.jpg") ) # image de fond page d'accueil
        frontimagelabel = Label(mainframe, image=frontimage)
        frontimagelabel.pack(expand=1)  
     
        #== Fermeture de la boucle principale ==
        mainframe.mainloop() # Boucle principale

  6. #6
    Membre confirmé Avatar de lagratteCchouette
    Profil pro
    Inscrit en
    Mars 2006
    Messages
    190
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2006
    Messages : 190
    Par défaut remerciemments

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

Discussions similaires

  1. Réponses: 0
    Dernier message: 04/02/2019, 10h02
  2. Réponses: 4
    Dernier message: 18/05/2016, 18h04
  3. Réponses: 3
    Dernier message: 23/01/2016, 20h34
  4. Réponses: 2
    Dernier message: 06/07/2012, 19h38
  5. [Kylix] message erreur à l'ex
    Par JlouisI dans le forum EDI
    Réponses: 5
    Dernier message: 08/03/2003, 17h47

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