0

Below is some simple Python/Tkinter code meant to display an image (google.png) stored in the local directory on a tk.Canvas. The code runs without errors, but the image which I hoped would display in the tk.Canvas (triggered by clicking the Run button, which calls Run_it()), does not show.

I'm running Python 3.8.5 (64-bit) on Ubuntu 20.04 LTS on a Microsoft Surface Pro (first gen).

My code:

import tkinter as tk
from PIL import ImageTk, Image

######## event handlers ##########

#Event handler for the 'Close' button
def close_it():
  exit(0)

#Event handler for the 'Run' button
def run_it():
  img = Image.open("google.png")
  #img = img.resize((250, 250), Image.ANTIALIAS) 
  img = ImageTk.PhotoImage(img)
  canvas.create_image(1, 1, image = img, anchor = tk.NW)

######### main ###########

#Create the root window
window = tk.Tk()
window.title("Simple HTTP Image Browser")

#Two frames:
display_frame = tk.Frame(master = window)
run_close_frame = tk.Frame(master = window)

display_frame.pack(side = tk.TOP, fill = tk.BOTH)
run_close_frame.pack(side = tk.TOP)

#Canvas for image display
canvas = tk.Canvas(master = display_frame, width = 600, height = 600)
canvas.pack(side = tk.LEFT, fill = tk.Y)

#Two buttons
run_button = tk.Button(text = "Run", master = run_close_frame, command = run_it)
close_button = tk.Button(text = "Close", master = run_close_frame, command = close_it)

run_button.pack(side = tk.LEFT)
close_button.pack(side = tk.LEFT)

window.mainloop()

martineau
  • 119,623
  • 25
  • 170
  • 301
Rene Reitsma
  • 31
  • 1
  • 6

0 Answers0