Tkinter import filedialog error

前端 未结 3 1979
伪装坚强ぢ
伪装坚强ぢ 2020-12-19 19:23

I\'m trying to use tkinter with python3 to open an image, see here a piece of code :

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# --- Python 3.4

from PI         


        
相关标签:
3条回答
  • 2020-12-19 19:55

    for above python3

    from tkinter.filedialog import askopenfilename

    0 讨论(0)
  • 2020-12-19 20:08

    tkinter.filedialog is for Python 3 only.

    From your attempts, it seems like you are using Python 2.x , try importing tkFileDialog

    Example -

    import tkFileDialog as filedialog
    

    Or alternatively, check why it ends up running Python 2.x , instead of Python 3.x .

    Tkinter module is only there in python 2 , python 3 has tkinter module, since when importing Tkinter it is successfully getting imported, but when importing tkinter it is failing to import it, we can be sure you end up running Python 2.x and not Python 3.

    You can do -

    import sys
    print(sys.version)
    print(sys.executable)
    

    to check the version of python currently running as well as the location of python (or python3) that is running.


    Most probably , the issue is occuring because even though you have python3 shebang line in your script, you are most probably running python <script.py> , this always causes python 2 to run.

    The aim of adding the python3 shebang line was to be able to run the script directly, without specifying the executable. For that you would need to do -

    1. Give executable permission to the script. (Use chmod u+x <script.py> )
    2. Then run the script as - ./<script.py>
    0 讨论(0)
  • 2020-12-19 20:10

    It should be from tkinter import filedialog alternatively you can try from tkinter import * or import tkinter.filedialog as fd . If it doesn't work like that, then you should try to reinstall python.

    0 讨论(0)
提交回复
热议问题