Reputation: 45
I have a script that outputs a series of images to a Notebook, which I have simplified below:
import os
import sys
import tkinter as tk
from tkinter import ttk
path = sys.path[0]
os.chdir(path)
def on_close():
root.quit()
root.destroy()
root = tk.Tk()
root.geometry('1250x550')
n = ttk.Notebook(root)
n.grid()
imgs = [img for img in os.listdir(path) if img.endswith('.png')]
for img in imgs:
f = ttk.Frame(n)
n.add(f, text=img)
photo = tk.PhotoImage(file=img)
label = ttk.Label(f, image=photo)
label.image = photo
label.grid(row=1, column=1, padx=(300,0))
root.wm_protocol('WM_DELETE_WINDOW', on_close)
root.mainloop()
When I run the script from the command line in Windows, the script works as is. If I change my code to root = tk.Toplevel()
, an extra window appears (ie. the implicit tk.Tk() window), which is what I expected.
However, when I run the above script from within Canopy, I get an error saying "pyimage doesn't exist". I can resolve this by changing my code to root = tk.Toplevel()
, and everything runs normally with no extra window.
Why is there a discrepancy when I run from Canopy? I've read questions where people needed to change root = tk.Toplevel()
when displaying images because they were somehow creating two root windows within their script. However, I don't believe that describes my situation, and doesn't explain why my script works from the command line but not Canopy.
Upvotes: 1
Views: 66
Reputation: 5810
By default, Canopy's (IPython) kernels are created in PyLab mode with a default Qt backend. For information about switching / disabling this, see https://support.enthought.com/hc/en-us/articles/204469880-Using-Tkinter-Turtle-or-Pyglet-in-Canopy-s-IPython-panel
Upvotes: 1