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

Tkinter Python Discussion :

Variable globale non initialisée


Sujet :

Tkinter Python

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2021
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 26
    Localisation : Gabon

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2021
    Messages : 1
    Points : 1
    Points
    1
    Par défaut Variable globale non initialisée
    Salut! Voici un code dont vous comprendrez la tache je crois...
    quand je l'execute j'ai une erreur que voici
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\Users\PC\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
        return self.func(*args)
      File "C:/Users/PC/PycharmProjects/pythonProject/test1/UnCaneva.py", line 10, in draw_lines
        canvas1.create_line(x1, y1, x2, y2, width=2, fill=col)
    NameError: name 'x1' is not defined



    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
     
    # coding:utf-8
    from tkinter import *
    from random import randrange
     
     
     
    def draw_lines():
        """Drawing of a line in canvas1"""
        global x1, x2, y1, y2, col
        canvas1.create_line(x1, y1, x2, y2, width=2, fill=col)
        # modification of coordinate for next line
        y1, y2 = y1 + 10, y2 + 10
     
     
    def change_col():
        """random modification of line color"""
        global col
        color = ["blue", "white", "green", "red", "orange", "purple", "cyan", "maroon", "yellow"]
        i = randrange(len(color))
        col = color[i]
     
     
    # widget principal
    draw_frame = Tk()
     
    # widgets du widget principal
    canvas1 = Canvas(draw_frame, bg="black", height=500, width=500)
    canvas1.pack(side=LEFT)
    button_quit = Button(draw_frame, text="Quit", command=draw_frame.quit)
    button_quit.pack(side=BOTTOM)
    button_draw_lines = Button(draw_frame, text="New line", command=draw_lines)
    button_draw_lines.pack()
    button_change_color = Button(draw_frame, text="Change color", command=change_col)
    button_change_color.pack()
    draw_frame.mainloop()
    #draw_frame.destroy()
     
    # __________ Main program _____________
     
    # this variable will be use global
    x1, y1, x2, y2 = 0, 0, 0, 0
    col = "dark green"

  2. #2
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 333
    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 333
    Points : 36 853
    Points
    36 853
    Par défaut
    Salut,

    Avec Python une variable (globale) est définie après qu'une instruction d'assignation (x1 = ...) ait été exécuté.
    Dans votre code, c'est à la ligne 42.
    Ce qui veut dire qu'elle n'a pas été exécutée... normal puisqu'après l'appel à mainloop.

    - W

Discussions similaires

  1. Variables globales non déclarées et 'extern'
    Par TomTom68 dans le forum C
    Réponses: 14
    Dernier message: 06/09/2011, 11h35
  2. [WD14] Variables globales non reconnues
    Par kitcarson23 dans le forum WinDev
    Réponses: 11
    Dernier message: 08/07/2011, 13h56
  3. Variable globale non merci
    Par hdgetnet dans le forum Débuter
    Réponses: 1
    Dernier message: 11/08/2010, 17h23
  4. Variable globale non accessible
    Par bolduc4 dans le forum Débuter avec Java
    Réponses: 2
    Dernier message: 10/05/2010, 01h23
  5. variable globale mal initialisée sous ie
    Par jesus144 dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 08/04/2008, 09h35

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