0

estou aprendendo sobre o Tkinter no python e a faculdade me diz esse jeito de inserir uma imagem na janela:

1 from tkinter import *
2
3 def funcClicar():
4 print("Botão pressionado")
5
6 janelaPrincipal = Tk()
7 texto = Label(master = janelaPrincipal, text = "Minha janela exibida")
8 texto.pack()
9
10 pic = PhotoImage(file="logoEstacio.gif")
11 logo = Label(master = janelaPrincipal, image = pic)
12 logo.pack()
13
14 botao = Button(master = janelaPrincipal, text = 'Clique', command = funcClicar)
15 botao.pack()
16
17 janelaPrincipal.mainloop()
18

meu codigo:

    import Tkinter
from cgitb import text
from doctest import master
from tkinter import Button, Label, PhotoImage, Tk
from Tkinter import *

def funcClicar():
    print("Botao Pressionado!")

janelaPrincipal = Tk()
texto = Label(master = janelaPrincipal, text = "Minha Janela")
texto.place(x = 50, y = 100)
texto.pack


pic = PhotoImage(file="com001.jpg")
python = Label(master= janelaPrincipal, image = pic)
python.pack



botao = Button(master=janelaPrincipal, text="Clique", command= funcClicar)
botao.place(x=60, y=170)
botao.pack

janelaPrincipal.mainloop()

erro no meu codigo: File "C:\Users\Daniel\AppData\Local\Programs\Python\Python310\lib\tkinter_init_.py", line 4093, in init Image.init(self, 'photo', name, cnf, master, **kw) File "C:\Users\Daniel\AppData\Local\Programs\Python\Python310\lib\tkinter_init_.py", line 4038, in init self.tk.call(('image', 'create', imgtype, name,) + options) _tkinter.TclError: couldn't open "com001.jpg": no such file or directory PS C:\Users\Daniel\Desktop\programacao>

Obrigado!

1 Resposta 1

0

Da maneira como você está fazendo, o Python está esperando que a imagem esteja no mesmo diretório que o seu script. Logo, ele está procurando a imagem em C:\Users\Daniel\Desktop\programacao e não está encontrando.

Você pode solucionar este problema passando caminho completo da imagem. Exemplo: C:\Users\Daniel\Images\com001.jpg

2
  • boa noite Diógenes, acontece que a imagem esta no mesmo diretorio do script, nessa pasta programacao\Faculdade\Python\Tema 4.
    – Kennedy12
    14/05/2022 às 23:05
  • tentei colocar o diretório direto e também não resolveu :/
    – Kennedy12
    14/05/2022 às 23:05

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .