-1

I am running a simple Tkinter example to load a window with an image. When the code is executed, a window is opened but of a very small size. When I resize the window, it's empty. The image is not loaded.

I am using Pycharm. I experienced the same with IDLE also.

Python 3.7.7

macOS Mojave version 10.14.4

tkinter version 8.6

Based on https://www.python.org/download/mac/tcltk/, my OS version, python version and tkinter version are compatible.

Here is my code

from tkinter import *
from tkinter import ttk

root = Tk()
label=ttk.Label(root, text="Hello TTK")
logo=PhotoImage("python_logo.gif")
label.image= logo
label.config(image= label.image)
label.pack()
root.mainloop()

The image file is stored in the same folder as the python code, and my logo object has the following info

tkinter.PhotoImage object at 0x10146a150
Abin John Thomas
  • 159
  • 2
  • 13

1 Answers1

0

The following code worked.

from tkinter import *
from tkinter import ttk
import os

root = Tk()
label=ttk.Label(root, text="Hello TTK")


data_folder = os.getcwd()
file_to_open=data_folder+"/python_logo.gif"

logo=PhotoImage(file=file_to_open,master=root)
label.image= logo
label.config(image= label.image)
label.pack()
root.mainloop()
Abin John Thomas
  • 159
  • 2
  • 13
  • 1
    You don't need to call `os.getcwd()` if it is a local file. Just remove the leading "/" from the filename (eg: file_to_open = "python_logo.gif") – Bryan Oakley May 20 '20 at 15:55