{"id":1591,"date":"2020-12-10T09:49:15","date_gmt":"2020-12-10T09:49:15","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1591"},"modified":"2021-01-06T04:05:32","modified_gmt":"2021-01-06T04:05:32","slug":"tkinter-open-file-dialog","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-open-file-dialog\/","title":{"rendered":"Tkinter Open File Dialog"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to show an open file dialog in Tkinter applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-tkinter-open-file-dialog-functions'>Introduction to the Tkinter Open File Dialog functions <a href=\"#introduction-to-the-tkinter-open-file-dialog-functions\" class=\"anchor\" id=\"introduction-to-the-tkinter-open-file-dialog-functions\" title=\"Anchor for Introduction to the Tkinter Open File Dialog functions\">#<\/a><\/h2>\n\n\n\n<p>When developing a Tkinter application that deals with the file system, you need to provide a dialog that allows file selections.<\/p>\n\n\n\n<p>To do that, you can use the <code>tkinter.filedialog<\/code> module. The following steps show how to display an open file dialog:<\/p>\n\n\n\n<p>First, import the <code>tkinter.filedialog<\/code> module:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> filedialog <span class=\"hljs-keyword\">as<\/span> fd<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, call the <code>fd.askopenfilename()<\/code> function to show a dialog that allows a single file selection:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">filename = fd.askopenfilename()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>askopenfilename()<\/code> function returns the file name that you selected.<\/p>\n\n\n\n<p>The <code>askopenfilename()<\/code> also supports other useful options including the initial directory displayed by the dialog or filtering files by their extensions.<\/p>\n\n\n\n<p>The following program displays a button: <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"302\" height=\"182\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Open-File-Dialog.png\" alt=\"\" class=\"wp-image-1592\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Open-File-Dialog.png 302w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Open-File-Dialog-300x181.png 300w\" sizes=\"auto, (max-width: 302px) 100vw, 302px\" \/><\/figure><\/div>\n\n\n\n<p>If you click the button, it&#8217;ll open a file dialog:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"663\" height=\"463\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Open-File-Dialog-Example.png\" alt=\"\" class=\"wp-image-1593\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Open-File-Dialog-Example.png 663w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Open-File-Dialog-Example-300x210.png 300w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/><\/figure><\/div>\n\n\n\n<p>After you select a file, the program will show the full path of the selected file:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"166\" height=\"152\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Open-File-Dialog-MessageBox.png\" alt=\"\" class=\"wp-image-1594\"\/><\/figure><\/div>\n\n\n\n<p>The program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> filedialog <span class=\"hljs-keyword\">as<\/span> fd\n<span class=\"hljs-keyword\">from<\/span> tkinter.messagebox <span class=\"hljs-keyword\">import<\/span> showinfo\n\n<span class=\"hljs-comment\"># create the root window<\/span>\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">'Tkinter Open File Dialog'<\/span>)\nroot.resizable(<span class=\"hljs-literal\">False<\/span>, <span class=\"hljs-literal\">False<\/span>)\nroot.geometry(<span class=\"hljs-string\">'300x150'<\/span>)\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">select_file<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    filetypes = (\n        (<span class=\"hljs-string\">'text files'<\/span>, <span class=\"hljs-string\">'*.txt'<\/span>),\n        (<span class=\"hljs-string\">'All files'<\/span>, <span class=\"hljs-string\">'*.*'<\/span>)\n    )\n\n    filename = fd.askopenfilename(\n        title=<span class=\"hljs-string\">'Open a file'<\/span>,\n        initialdir=<span class=\"hljs-string\">'\/'<\/span>,\n        filetypes=filetypes)\n\n    showinfo(\n        title=<span class=\"hljs-string\">'Selected File'<\/span>,\n        message=filename\n    )\n\n\n<span class=\"hljs-comment\"># open button<\/span>\nopen_button = ttk.Button(\n    root,\n    text=<span class=\"hljs-string\">'Open a File'<\/span>,\n    command=select_file\n)\n\nopen_button.pack(expand=<span class=\"hljs-literal\">True<\/span>)\n\n\n<span class=\"hljs-comment\"># run the application<\/span>\nroot.mainloop()\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='selecting-multiple-files'>Selecting multiple files <a href=\"#selecting-multiple-files\" class=\"anchor\" id=\"selecting-multiple-files\" title=\"Anchor for Selecting multiple files\">#<\/a><\/h2>\n\n\n\n<p>The <code>askopenfilenames()<\/code> function displays a file dialog for multiple file selections. It returns the selected file names as a tuple. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> filedialog <span class=\"hljs-keyword\">as<\/span> fd\n<span class=\"hljs-keyword\">from<\/span> tkinter.messagebox <span class=\"hljs-keyword\">import<\/span> showinfo\n\n<span class=\"hljs-comment\"># create the root window<\/span>\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">'Tkinter File Dialog'<\/span>)\nroot.resizable(<span class=\"hljs-literal\">False<\/span>, <span class=\"hljs-literal\">False<\/span>)\nroot.geometry(<span class=\"hljs-string\">'300x150'<\/span>)\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">select_files<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    filetypes = (\n        (<span class=\"hljs-string\">'text files'<\/span>, <span class=\"hljs-string\">'*.txt'<\/span>),\n        (<span class=\"hljs-string\">'All files'<\/span>, <span class=\"hljs-string\">'*.*'<\/span>)\n    )\n\n    filenames = fd.askopenfilenames(\n        title=<span class=\"hljs-string\">'Open files'<\/span>,\n        initialdir=<span class=\"hljs-string\">'\/'<\/span>,\n        filetypes=filetypes)\n\n    showinfo(\n        title=<span class=\"hljs-string\">'Selected Files'<\/span>,\n        message=filenames\n    )\n\n\n<span class=\"hljs-comment\"># open button<\/span>\nopen_button = ttk.Button(\n    root,\n    text=<span class=\"hljs-string\">'Open Files'<\/span>,\n    command=select_files\n)\n\nopen_button.pack(expand=<span class=\"hljs-literal\">True<\/span>)\n\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='opening-files-directly'>Opening files directly <a href=\"#opening-files-directly\" class=\"anchor\" id=\"opening-files-directly\" title=\"Anchor for Opening files directly\">#<\/a><\/h2>\n\n\n\n<p>After getting the selected file names, you can open them using the <code>open()<\/code> method.<\/p>\n\n\n\n<p>To make it more convenient, the <code>tkinter.filedialog<\/code> module also provides some functions that allow you to select one or more files and return the file objects directly.<\/p>\n\n\n\n<p>The <code>askopenfile()<\/code> function displays a file dialog and returns a file object of the selected file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">f = fd.askopenfile()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And the <code>askopenfiles()<\/code> function shows a file dialog and returns file objects of the selected files:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">f = fd.askopenfiles()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following program illustrates how to use the <code>askopenfile()<\/code> function:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"552\" height=\"282\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Open-File-Dialog-Display-a-File.png\" alt=\"\" class=\"wp-image-1597\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Open-File-Dialog-Display-a-File.png 552w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Open-File-Dialog-Display-a-File-300x153.png 300w\" sizes=\"auto, (max-width: 552px) 100vw, 552px\" \/><\/figure><\/div>\n\n\n\n<p>It&#8217;ll allow you to open a text file and display the file content on a <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-text\/\">Text<\/a> widget:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> filedialog <span class=\"hljs-keyword\">as<\/span> fd\n\n<span class=\"hljs-comment\"># Root window<\/span>\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">'Display a Text File'<\/span>)\nroot.resizable(<span class=\"hljs-literal\">False<\/span>, <span class=\"hljs-literal\">False<\/span>)\nroot.geometry(<span class=\"hljs-string\">'550x250'<\/span>)\n\n<span class=\"hljs-comment\"># Text editor<\/span>\ntext = tk.Text(root, height=<span class=\"hljs-number\">12<\/span>)\ntext.grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">0<\/span>, sticky=<span class=\"hljs-string\">'nsew'<\/span>)\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">open_text_file<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-comment\"># file type<\/span>\n    filetypes = (\n        (<span class=\"hljs-string\">'text files'<\/span>, <span class=\"hljs-string\">'*.txt'<\/span>),\n        (<span class=\"hljs-string\">'All files'<\/span>, <span class=\"hljs-string\">'*.*'<\/span>)\n    )\n    <span class=\"hljs-comment\"># show the open file dialog<\/span>\n    f = fd.askopenfile(filetypes=filetypes)\n    <span class=\"hljs-comment\"># read the text file and show its content on the Text<\/span>\n    text.insert(<span class=\"hljs-string\">'1.0'<\/span>, f.readlines())\n\n\n<span class=\"hljs-comment\"># open file button<\/span>\nopen_button = ttk.Button(\n    root,\n    text=<span class=\"hljs-string\">'Open a File'<\/span>,\n    command=open_text_file\n)\n\nopen_button.grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">1<\/span>, sticky=<span class=\"hljs-string\">'w'<\/span>, padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>)\n\n\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the <code>askopenfilename()<\/code> function to display an open file dialog that allows users to select one file.<\/li><li>Use the <code>askopenfilenames()<\/code> function to display an open file dialog that allows users to select multiple files.<\/li><li>Use the <code>askopenfile()<\/code> or <code>askopenfiles()<\/code> function to display an open file dialog that allows users to select one or multiple files and receive a file or multiple file objects.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1591\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-open-file-dialog\/\"\n\t\t\t\tdata-post-title=\"Tkinter Open File Dialog\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1591\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-open-file-dialog\/\"\n\t\t\t\tdata-post-title=\"Tkinter Open File Dialog\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to show an open file dialog in Tkinter applications.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":35,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1591","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1591","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=1591"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1591\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1232"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=1591"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}