Problèmes inattendus avec tkinter !
Percysledge
Messages postés
5
Statut
Membre
-
yg_be Messages postés 23437 Date d'inscription Statut Contributeur Dernière intervention -
yg_be Messages postés 23437 Date d'inscription Statut Contributeur Dernière intervention -
Salut à tous. J'ai ici le code d'un programme en python avec un canva tkinter ,ce programme est censé affiché une ligne parallèle à la précédente quand j'appuie le bouton 'New line' et changer la couleur de la ligne suivante quand j'appui sur le bouton 'Change color'...enfin vous comprendrez le code je crois ! Mais quand j'appuie sur mon bouton 'New line' rien ne passe dans la fenêtre et j'ai une erreur dans la console...
Voici l'erreur et le code plus bas. Votre aide me serai très utiles . Merci :)
Voici l'erreur et le code plus bas. Votre aide me serai très utiles . Merci :)
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
# 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"
Configuration: Windows / Edge 88.0.705.68