cannot import name 'ImageTK' - python 3.5

前端 未结 7 1667
忘掉有多难
忘掉有多难 2020-12-17 16:02

I am trying to load in an image from the same folder as the one my python script is in.

# create a class called Person
# create init method
# 2 attributes (         


        
相关标签:
7条回答
  • 2020-12-17 16:21

    For Debian/Ubuntu:

    Python 2

    sudo apt-get install python-imaging python-pil.imagetk
    

    Python 3

    sudo apt-get install python3-pil python3-pil.imagetk
    

    For Archlinux:

    sudo pacman -S python-pillow  
    

    It will install the package and you can use it: from PIL import ImageTk

    0 讨论(0)
  • 2020-12-17 16:24

    I tried this to install Pillow itself and it works well, i didn't use sudo.

    $ pip install Pillow --user
    

    Source for the main installation guide: here

    Feel free to edit my answer/correct me.

    0 讨论(0)
  • 2020-12-17 16:25

    You will have to change the code like this:

    import PIL
    from PIL import ImageTk
    from PIL import Image
    

    It should work fine!

    0 讨论(0)
  • 2020-12-17 16:35
    sudo apt-get install python3-pil.imagetk
    

    It worked for me!

    0 讨论(0)
  • 2020-12-17 16:36

    Got it figure out! You must import them separately and not on one line.

    from PIL import Image
    from PIL import ImageTk
    

    Insead of

    from PIL import Image, ImageTk
    
    0 讨论(0)
  • 2020-12-17 16:39

    Python does act up a little when importing TkImage and Image together. You need to import the PIL first and then import TkImage and Image individually like below-

    import PIL
    from PIL import TkImage
    from PIL import Image
    

    This should work fine. You can also check if the pillow is installed in your system correctly by using command prompt like below-

    1. Open Command Prompt
    2. Type python
    3. Once you are in python, just type :
    import PIL
    
    1. If this command doesn't throw any errors, then you are good to follow the above method of importing TkImage and Image separately.
    0 讨论(0)
提交回复
热议问题