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 :

Problème avec Tkinter: AttributeError: 'function' object has no attribute 'tk'


Sujet :

Python

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2015
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2015
    Messages : 1
    Points : 1
    Points
    1
    Par défaut Problème avec Tkinter: AttributeError: 'function' object has no attribute 'tk'
    Bonjour,

    Je débute en Python et je me confronte à un problème avec mon programme.

    Je tente d'appeler une fonction avec 2 arguments mais le compilateur me renvoie une erreur.

    Voici le code qui pose problème:

    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
    def Pion_Rouge(X, Y):
     
        Pion_Rouge2_image = PhotoImage(file="Pion_Rouge.gif")
        Couleur_bg = '#%02x%02x%02x' % (0, 39, 232)
     
        Pion_Rouge2 = Label(jeu_facile2, image=Pion_Rouge2_image, bg=Couleur_bg)
        Pion_Rouge2.image = Pion_Rouge2_image
        Pion_Rouge2.place(x=99+X*63, y=360-Y*63)
     
    def jeu_facile():
     
        # Creation de la fenêtre niveau de difficulte en plein ecran + fond noir
        jeu_facile2 = Toplevel()
        jeu_facile2.attributes('-fullscreen', True)
        jeu_facile2['bg']='black'
     
        # Creation d'un widget Button (bouton quitter)
        BoutonQuitter_facile = Button(jeu_facile2, text ='Quitter', command = lambda: sur(jeu_facile2), bg="black", fg="white", height=2, width=9, font=("Helvetica", 15))
        BoutonQuitter_facile.place(x=650, y=33)
     
        Puissance4_image = PhotoImage(file="Puissance4.gif")
     
        Puissance4 = Label(jeu_facile2, image=Puissance4_image, bg='blue')
        Puissance4.image = Puissance4_image
        Puissance4.place(x=90, y=40)
     
        Pion_Rouge(2, 5)
    Et voici l'erreur que me renvoie le compilateur:

    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
    Exception in Tkinter callback
    Traceback (most recent call last):
      File "/usr/lib/python3.2/tkinter/__init__.py", line 1426, in __call__
        return self.func(*args)
      File "/home/pi/Docs/Puissance 4/Puissance4.py", line 76, in jeu_facile
        Pion_Rouge(1, 1)
      File "/home/pi/Docs/Puissance 4/Puissance4.py", line 83, in Pion_Rouge
        Pion_Rouge2 = Label(jeu_facile, image=Pion_Rouge2_image, bg=Couleur_bg)
      File "/usr/lib/python3.2/tkinter/__init__.py", line 2486, in __init__
        Widget.__init__(self, master, 'label', cnf, kw)
      File "/usr/lib/python3.2/tkinter/__init__.py", line 1978, in __init__
        BaseWidget._setup(self, master, cnf)
      File "/usr/lib/python3.2/tkinter/__init__.py", line 1956, in _setup
        self.tk = master.tk
    AttributeError: 'function' object has no attribute 'tk'
    Enfin, voici le code complet:

    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
    196
    197
    198
    199
    200
    # Jeu du puissance 4 automatise
    # Interface Homme-Machine
     
    # Start
     
    # ---------------------------------------------------------------------------------------------------------------------
     
    from tkinter import *
    import time
    # import Jeu
    # ---------------------------------------------------------------------------------------------------------------------
     
    # Place des pions rouges
     
    # ---------------------------------------------------------------------------------------------------------------------
     
    def Pion_Rouge(X, Y):
     
        Pion_Rouge2_image = PhotoImage(file="Pion_Rouge.gif")
        Couleur_bg = '#%02x%02x%02x' % (0, 39, 232)
     
        Pion_Rouge2 = Label(jeu_facile2, image=Pion_Rouge2_image, bg=Couleur_bg)
        Pion_Rouge2.image = Pion_Rouge2_image
        Pion_Rouge2.place(x=99+X*63, y=360-Y*63)
     
    def Pion_Jaune(X, Y):
     
        Pion_Jaune_image = PhotoImage(file="Pion_Jaune.gif")
        Couleur_bg = '#%02x%02x%02x' % (0, 39, 232)
     
        Pion_Jaune = Label(jeu_normal, image=Pion_Jaune_image, bg=Couleur_bg)
        Pion_Jaune.image = Pion_Jaune_image
        Pion_Jaune.place(x=99+X*63, y=360-Y*63)
    # ---------------------------------------------------------------------------------------------------------------------
     
    #def Queue()
     
    #	x=1
    #	if x==1:
    #		Queue.get(True)
    #		if Queue=="B":
     
    # ---------------------------------------------------------------------------------------------------------------------
     
    def sur(*args):
     
        # Creation de la fenêtre etes vous sur en plein ecran + fond noir
        sur = Toplevel(args[0])
        sur.attributes('-fullscreen', True)
        sur['bg']='black'
     
        # Creation d'un widget label : voulez vous vraiment quitter
        LabelEtesVousSur = Label(sur, text = 'Voulez-vous vraiment quitter le jeu en cours\net revenir au menu principal ?', fg="white", bg="black", font = "Helvetica 22 italic underline")
        LabelEtesVousSur.pack(side = TOP, pady = 40)
     
        # Creation d'un widget Button (bouton Non)
        BoutonNon = Button(sur, text ='Non', command = sur.destroy, bg="black", fg="white", height=2, width=13, font=("Helvetica", 15))
        BoutonNon.place(x=150, y=230)
     
        # Creation d'un widget Button (bouton Oui)
        BoutonOui = Button(sur, text ='Oui', command = args[0].destroy, bg="black", fg="white", height=2, width=13, font=("Helvetica", 15))
        BoutonOui.place(x=450, y=230)
    # ---------------------------------------------------------------------------------------------------------------------	
     
    def jeu_facile():
     
        # Creation de la fenêtre niveau de difficulte en plein ecran + fond noir
        jeu_facile2 = Toplevel()
        jeu_facile2.attributes('-fullscreen', True)
        jeu_facile2['bg']='black'
     
        # Creation d'un widget Button (bouton quitter)
        BoutonQuitter_facile = Button(jeu_facile2, text ='Quitter', command = lambda: sur(jeu_facile2), bg="black", fg="white", height=2, width=9, font=("Helvetica", 15))
        BoutonQuitter_facile.place(x=650, y=33)
     
        Puissance4_image = PhotoImage(file="Puissance4.gif")
     
        Puissance4 = Label(jeu_facile2, image=Puissance4_image, bg='blue')
        Puissance4.image = Puissance4_image
        Puissance4.place(x=90, y=40)
     
        Pion_Rouge(2, 5)
     
    # --------------------------------------------------------------------------------------------------------------------- 
     
    def jeu_normal():
     
        # Creation de la fenêtre niveau de difficulte en plein ecran + fond noir
        jeu_normal = Toplevel()
        jeu_normal.attributes('-fullscreen', True)
        jeu_normal['bg']='black'
     
        # Creation d'un widget Button (bouton quitter)
        BoutonQuitter_normal = Button(jeu_normal, text ='Quitter', command = lambda: sur(jeu_normal), bg="black", fg="white", height=2, width=9, font=("Helvetica", 15))
        BoutonQuitter_normal.place(x=650, y=33)
     
        Puissance4_image = PhotoImage(file="Puissance4.gif")
     
        Puissance4 = Label(jeu_normal, image=Puissance4_image, bg='blue')
        Puissance4.image = Puissance4_image
        Puissance4.place(x=90, y=40)
     
    # ---------------------------------------------------------------------------------------------------------------------	
     
    def jeu_difficile():
     
        # Creation de la fenêtre niveau de difficulte en plein ecran + fond noir
        jeu_difficile = Toplevel()
        jeu_difficile.attributes('-fullscreen', True)
        jeu_difficile['bg']='black'
     
        # Creation d'un widget Button (bouton quitter)
        BoutonQuitter_difficile = Button(jeu_difficile, text ='Quitter', command = lambda: sur(jeu_difficile), bg="black", fg="white", height=2, width=9, font=("Helvetica", 15))
        BoutonQuitter_difficile.place(x=650, y=33)
     
        Puissance4_image = PhotoImage(file="Puissance4.gif")
     
        Puissance4 = Label(jeu_difficile, image=Puissance4_image, bg='blue')
        Puissance4.image = Puissance4_image
        Puissance4.place(x=90, y=40)
    # ---------------------------------------------------------------------------------------------------------------------
     
    def menu_difficulte():
     
        # Creation de la fenêtre niveau de difficulte en plein ecran + fond noir
        niv = Toplevel()
        niv.attributes('-fullscreen', True)
        niv['bg']='black'
     
        # Creation d'un widget label : Choisir le niveau de difficulte
        LabelNiveauDifficulte = Label(niv, text = 'Choisir le niveau de difficulte', fg="white", bg="black", font = "Helvetica 30 italic underline")
        LabelNiveauDifficulte.pack(side = TOP, pady = 15)
     
        # Creation d'un widget Button (bouton Facile)
        BoutonFacile = Button(niv, text ='Facile', command = jeu_facile, bg="black", fg="white", height=2, width=18, font=("Helvetica", 15))
        BoutonFacile.pack(padx = 20, pady = 13)
     
        # Creation d'un widget Button (bouton Normal)
        BoutonNormal = Button(niv, text ='Normal', bg="black", command = jeu_normal, fg="white", height=2, width=18, font=("Helvetica", 15))
        BoutonNormal.pack(padx = 20, pady = 13)
     
        # Creation d'un widget Button (bouton Difficile)
        BoutonDifficile = Button(niv, text ='Difficile', command = jeu_difficile, bg="black", fg="white", height=2, width=18, font=("Helvetica", 15))
        BoutonDifficile.pack(padx = 20, pady = 13)
     
        # Creation d'un widget Button (bouton Retour)
        BoutonRetour = Button(niv, text = 'Retour au menu principal', command = niv.destroy, bg="black", fg="white", height=2, width=28, font= "Helvetica 13 italic")
        BoutonRetour.pack(padx = 20, pady = 18)
     
        # Creation d'un widget label pour afficher l'heure
        clock = Label(niv, font=('times', 20, 'bold'), bg='black', fg='white')
        clock.pack(fill=BOTH, expand=1)
     
        def tick2():
            s = time.strftime('%H:%M:%S')
            if s != clock["text"]:
                clock["text"] = s
            clock.after(200, tick2)
     
        tick2()
    # ---------------------------------------------------------------------------------------------------------------------    
     
    # Creation de la fenêtre principale en plein ecran + fond noir
    root = Tk()
    root.attributes('-fullscreen', True)
    root['bg']='black'
     
    import os
    def shutdown(*args):
        os.system("sudo halt")
     
    # Creation d'un widget label : Puissance 4
    LabelPuissance4 = Label(root, text = 'Puissance 4', fg="white", bg="black", font = "Helvetica 50 italic underline")
    LabelPuissance4.pack(side = TOP, pady = 20)
     
    # Creation d'un widget Button (bouton Jouer)
    BoutonJouer = Button(root, text ='Jouer', command = menu_difficulte, bg="black", fg="white", height=2, width=18, font=("Helvetica", 20))
    BoutonJouer.pack(padx = 20, pady = 40)
     
    # Creation d'un widget Button (bouton Quitter)
    BoutonQuitter = Button(root, text ='Quitter', command = shutdown, bg="black", fg="white", height=2, width=18, font=("Helvetica", 20))
    BoutonQuitter.pack(padx = 20, pady = 20)
     
    # Creation d'un widget label pour afficher l'heure
    clock = Label(root, font=('times', 20, 'bold'), bg='black', fg='white')
    clock.pack(fill=BOTH, expand=1)
     
    def tick():
        s = time.strftime('%H:%M:%S')
        if s != clock["text"]:
            clock["text"] = s
        clock.after(200, tick)
     
    tick()
     
    root.mainloop()
     
    # ---------------------------------------------------------------------------------------------------------------------
     
    # The End
    Cordialement.

  2. #2
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 313
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 313
    Points : 36 819
    Points
    36 819
    Par défaut
    Salut,

    Il n'est pas très malin d'avoir une fonction shutdown qui exécute:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    import os
    def shutdown(*args):
        os.system("sudo halt")
    Quand je lis:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    Traceback (most recent call last):
      File "/usr/lib/python3.2/tkinter/__init__.py", line 1426, in __call__
        return self.func(*args)
      File "/home/pi/Docs/Puissance 4/Puissance4.py", line 76, in jeu_facile
        Pion_Rouge(1, 1)
    Et que je vois que la fonction "jeu_facile" contient:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    def jeu_facile():
        ....
     
        Pion_Rouge(2, 5)
    C'est que vous ne montrez pas le source correspondant à l'erreur.
    De plus, quand on lit:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    def Pion_Rouge(X, Y):
     
        Pion_Rouge2_image = PhotoImage(file="Pion_Rouge.gif")
        Couleur_bg = '#%02x%02x%02x' % (0, 39, 232)
     
        Pion_Rouge2 = Label(jeu_facile2, image=Pion_Rouge2_image, bg=Couleur_bg)
    Ca devrait d'abord planter parce que la variable "jeu_facile2" n'est pas définie.

    Un peu de rigueur s'il vous plaît...

    - W

Discussions similaires

  1. Problème Pil : AttributeError: 'tuple' object has no attribute '
    Par shooter7223fr dans le forum Général Python
    Réponses: 9
    Dernier message: 28/10/2020, 12h18
  2. AttributeError "nonetype" object has no attribute
    Par Invité dans le forum Général Python
    Réponses: 2
    Dernier message: 14/12/2010, 20h49
  3. Selenium - AttributeError: 'module' object has no attribute
    Par bender1979 dans le forum Général Python
    Réponses: 4
    Dernier message: 09/11/2010, 22h03
  4. Probleme : AttributeError: 'tuple' object has no attribute
    Par MrGecko dans le forum Général Python
    Réponses: 1
    Dernier message: 27/05/2007, 09h59
  5. Réponses: 2
    Dernier message: 26/05/2006, 14h48

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