Почему canvas не определен? Подскажите пожалуйста

Пользователь

от tanya.isak.07 , в категории: Python , 8 месяцев назад
 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
from PIL import Image, ImageTk
from random import randint, choice
import tkinter as tk


def genir():
    with open('письма.txt', 'r', encoding="utf-8") as file:
        lines = file.readlines()
    text.delete(1.0, END)
    text.insert(1.0, choice(lines))


w = tk.Tk()
w.resizable(width=False, height=False)
w.title = ('87373378')
w.geometry('720x360')
w.image = tk.PhotoImage(file='лав.png')
bg_pisa = tk.Label(w, image=w.image)
bg_pisa.grid(row=0, column=0)

EnterText = tk.Entry(fg='white', bg='black', width=30)
EnterText.place(x=520, y=320)
text = tk.Text(width=25, height=5, bg="black", fg='white', wrap="word")
text.place(x=120, y=170)

btn = tk.Button(w, text="переключить", command=genir, width='20', height='2', fg='black', bg='white')
btn.place(x=220, y=130)

canvas = Canvas(w, width=200, height=200)
canvas.pack()

image = Image.open("тел.png")
photo = ImageTk.PhotoImage(image)
image = canvas.create_image(0, 0, anchor='nw', image=photo)
canvas.grid(row=2, column=1)

w.mainloop()






Ошибка: canvas = Canvas(w, width=200, height=200)

NameError: name 'Canvas' is not defined

Facebook Vk Ok Twitter LinkedIn Telegram Whatsapp Pocket

1 ответ

Пользователь

от ebudoragina , 6 месяцев назад

@tanya.isak.07 Думаю ошибка в NameError, попробуйте данный код

 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
from PIL import Image, ImageTk
from random import choice
import tkinter as tk
from tkinter import Canvas

def genir():
    with open('письма.txt', 'r', encoding="utf-8") as file:
        lines = file.readlines()
    text.delete(1.0, tk.END)
    text.insert(1.0, choice(lines))


w = tk.Tk()
w.resizable(width=False, height=False)
w.title = ('87373378')
w.geometry('720x360')
w.image = tk.PhotoImage(file='лав.png')
bg_pisa = tk.Label(w, image=w.image)
bg_pisa.grid(row=0, column=0)

EnterText = tk.Entry(fg='white', bg='black', width=30)
EnterText.place(x=520, y=320)
text = tk.Text(width=25, height=5, bg="black", fg='white', wrap="word")
text.place(x=120, y=170)

btn = tk.Button(w, text="переключить", command=genir, width='20', height='2', fg='black', bg='white')
btn.place(x=220, y=130)

canvas = Canvas(w, width=200, height=200)
canvas.pack()

image = Image.open("тел.png")
photo = ImageTk.PhotoImage(image)
image = canvas.create_image(0, 0, anchor='nw', image=photo)
canvas.grid(row=2, column=1)

w.mainloop()