Python Tkinter 导入文件对话框错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31669945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tkinter import filedialog error
提问by Rémi Vennereau
I'm trying to use tkinter with python3 to open an image, see here a piece of code :
我正在尝试将 tkinter 与 python3 一起使用来打开图像,请参阅此处的一段代码:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# --- Python 3.4
from PIL import Image
import Tkinter as tk
from Tkinter import filedialog
import numpy as np
import os
var = 'n'
# Importing the image to correct
while var != 'o' :
var = raw_input("Press \"o\" to open the image to correct\n")
var = var.lower()
root = tk.Tk()
root.withdraw()
path = filedialog.askopenfilename()
image_test = Image.open(path)
I have installed python3-tk, and I have the demo window when I write
我已经安装了python3-tk,我写的时候有演示窗口
python3 -m tkinter
in the terminal. I tried several combinations that did not work :
在终端。我尝试了几种不起作用的组合:
import tkinter as tk
from tkinter import filedialog
gives
给
ImportError : No module named tkinter
,
,
import Tkinter as tk
from Tkinter import filedialog
gives
给
ImportError : cannot import name filedialog
I tried with _tinker , FileDialog, file_dialog, but I always have "ImportError : cannot import name filedialog". Any clue ?
我尝试过 _tinker 、 FileDialog、file_dialog,但我总是有“ImportError : cannot import name filedialog”。任何线索?
采纳答案by Anand S Kumar
tkinter.filedialog
is for Python 3 only.
tkinter.filedialog
仅适用于 Python 3。
From your attempts, it seems like you are using Python 2.x
, try importing tkFileDialog
从您的尝试来看,您似乎正在使用Python 2.x
,请尝试导入tkFileDialog
Example -
例子 -
import tkFileDialog as filedialog
Or alternatively, check why it ends up running Python 2.x , instead of Python 3.x .
或者,检查为什么它最终运行 Python 2.x 而不是 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.
Tkinter
模块只存在于 python 2 中,python 3 有tkinter
模块,因为在导入Tkinter
时成功导入,但在导入时导入tkinter
失败,我们可以确定您最终运行的是 Python 2.x 而不是 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.
检查当前运行的python版本以及正在运行的python
(或python3
)的位置。
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.
最有可能的是,问题的发生是因为即使python3
您的脚本中有shebang 行,您也很可能正在运行python <script.py>
,这总是会导致 python 2 运行。
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 -
添加python3
shebang 行的目的是能够直接运行脚本,而无需指定可执行文件。为此,你需要做 -
- Give executable permission to the script. (Use
chmod u+x <script.py>
) - Then run the script as -
./<script.py>
- 授予脚本可执行权限。(使用
chmod u+x <script.py>
) - 然后将脚本运行为 -
./<script.py>
回答by alec_djinn
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.
应该from tkinter import filedialog
或者你可以尝试from tkinter import *
或import tkinter.filedialog as fd
。如果它不起作用,那么您应该尝试重新安装python。
回答by tulsi kumar
for above python3
对于以上python3
from
tkinter.filedialogimport
askopenfilename
from
tkinter.filedialogimport
askopenfilename