Home > Net >  Python modules clashing (tkinter & pylab) leading to weird inconsistency in Pycharm Python console
Python modules clashing (tkinter & pylab) leading to weird inconsistency in Pycharm Python console

Time:10-23

I have simplified my problem. I have two files : test.py and ratios.py

ratios.py is just one line :

import pylab as pl

test.py is :

import tkinter as tk
import tkinter.font as font
import os
from ratios import *

def mainAlgo(username, cursor, code_groupe, tkWindow):
    root = tk.Tk()
    root.title("Title")
    root.resizable(False, False)

    myFont = font.Font(size=15)

    canvas = tk.Canvas(root, height=600, width=800)
    canvas.pack()

    background_image = tk.PhotoImage(file="dev/bg.png")
    background_label = tk.Label(root, image=background_image)
    background_label.place(relwidth=1, relheight=1)

    root.mainloop()

mainAlgo("", "", "", "")

If I launch test.py, everything works. If I modify test.py by removing the last line, and I then enter mainAlgo("", "", "", "") in the console like so I have the following error :

Backend TkAgg is interactive backend. Turning interactive mode on.
mainAlgo("", "", "", "")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:/Users/lh/Documents/Documents/Formation scolaire/Thèse/Travail/Algo/Version_Finale_Indep 12.00/test.py", line 18, in mainAlgo
    background_label = tk.Label(root, image=background_image)
  File "C:\Users\lh\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 3148, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Users\lh\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2572, in __init__
    self.tk.call(
_tkinter.TclError: image "pyimage1" doesn't exist

I am using Windows 10, Python 3.9, and PyCharm.

Isn't it weird? The two should be equivalent (at least it was taught to me this way)!

I would appreciate any help/advice.

[EDIT] I mention python modules clashing because if i don't import pylab, I don't have this inconsistency. However, I can't find a fix (I need to import a module which uses pylab !!).

CodePudding user response:

Adding master = root to tk.PhotoImage solved the problem. Changing python editor (VS Code and Pyzo) solved it too (without adding master = root) I don't really know why so any explanation is more than welcome.

  • Related