0

I expect my code below to display the image from file 'apple.png' in the rootTwo Tkinter window. However when I press the button to open said window nothing shows. I can see the background is working in the secound window, just not the placement of the photo.

You can see in the photo's further down that the background works in the second window, and that the method of placing the image works in the first, so whats got to give...?

import tkinter as tk
from PIL import Image as img
from PIL import ImageTk as itk



def testWindowTwo ():  
    rootTwo = tk.Tk()
    
    rootTwo.geometry('900x300')
    rootTwo.resizable(False, False)
    rootTwo.title('Testing2')


    canvasTest = tk.Canvas(rootTwo, bg='red')
    canvasTest.pack()

    appleImgPre = img.open('apple.png')
    appleImgPre = appleImgPre.resize((50,50))
    appleImg = itk.PhotoImage(appleImgPre)

    canvasTest.create_image(75,75, image=appleImg)


    rootTwo.mainloop()




root = tk.Tk()

root.geometry('1280x720')
root.configure(bg='#398AD7')
root.title('Testing')

canvasMain = tk.Canvas(root, width=1920, height=1080, bg='red')
canvasMain.pack()

imgTestPre = img.open('apple.png')
imgTestPre = imgTestPre.resize((50,50))
imgTest = itk.PhotoImage(imgTestPre)

canvasMain.create_image(75, 75, image=imgTest)


myButton = tk.Button( width=10, height=5, text='test', command=testWindowTwo)
canvasMain.create_window(300, 300, window=myButton)

root.mainloop()

SECOND WINDOW:

[2

FIRST WINDOW ^^^^

IIlIll IIIlII
  • 51
  • 1
  • 6
  • 2
    Even with that problem fixed, this is likely to fail due to multiple calls to `Tk()`. You need to use `Toplevel()` instead to create additional windows. – jasonharper Nov 09 '20 at 16:34
  • Indeed. See [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) – martineau Nov 09 '20 at 16:40
  • @jasonharper Thank you so much! It was the multiple instances that was messing it up. – IIlIll IIIlII Nov 09 '20 at 16:41

0 Answers0