Image reference path in Label Tkinter

Asked

Viewed 60 times

-2

I’m training POO in a personal project that I’m developing in showing a window called by a button that shows "samples" of text from a language, showing a flag and text from that language. The problem is that the text appears, however, the image I want to appear in the window is not shown, although the path is correct relative to the file main.py. In Pycharm, when you place your mouse on a path relative to a file that is image correctly it shows a pop-up with a preview of the image. I don’t know if I should make a reference from the position of the file ```main.pyoucommanding_init_.py```. It is structured in this way:

inserir a descrição da imagem aqui

Examples
main.py

from uteis.comandos_botao import *
from tkinter import *
import tkinter.ttk as ttk
from ttkthemes import ThemedStyle

root = Tk()

style_alt = ThemedStyle(root)
style_alt.set_theme('breeze')

botao = ttk.Button(root, text="Hindi", command=bt_hindi)
botao.grid(row=0, column=0)

root.mainloop()

useful __init__.py

from tkinter import *

from uteis.textos import *


class JanelaIdioma():
    def __init__(self, idioma, texto, bandeira):
        self.idioma = idioma
        self.texto = texto
        self.bandeira = bandeira

    def criar_janela(self):
        if self.idioma == "Hindi":
            self.texto = hindi

            root_hindi = Toplevel()

            bandeira = Label(root_hindi, image=self.bandeira)
            bandeira.grid(row=0, column=0)

            letreiro_hindi = Label(root_hindi, text=self.texto, font=("Arial", 36, "bold"))
            letreiro_hindi.grid(row=1, column=0)

command _init_.py

from uteis import *
from uteis.textos import *


def bt_hindi():
    bandeira_hindi = PhotoImage(file='img/india.png')
    janela = JanelaIdioma("hindi", hindi, bandeira_hindi).criar_janela()

texts __init__.py

hindi = "ऐसा कोई नहीं है जो खुद दर्द को प्यार करता हो, जो ऐसा करने के पीछे हो और चाहता हो, बस क्योकि यह दर्द होता है..."

I know it’s a lot of code and in case you’re confused, the entire project is on Github around here.

  • What is the result for PhotoImage(file='../img/india.png') and PhotoImage(file='../../img/india.png') ?

  • @Paulomarques 1- _tkinter.TclError: couldn't open "../img/india.png": no such file or directory 2- _tkinter.TclError: couldn't open "../../img/india.png": no such file or directory. The complete error exceeds the maximum number of characters, so I put in the image here.

1 answer

0


This seems to be the classic problem of 'the image does not appear on Tkinter' that occurs when you try to create an image within a function. Within the functions its variables are local, but who updates the screen of Tkinter is the root.mainloop() who has no access to these local variables. The best way to solve this problem is to define its variables that store the image as a class/object variable (static or dynamic)but if your function is not defined in a class you can use global so that your variable containing the image can be accessed by Tkinter update:

def bt_hindi():
    global bandeira_hindi
    bandeira_hindi = PhotoImage(file='img/india.png')
    janela = JanelaIdioma("hindi", hindi, bandeira_hindi).criar_janela()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.