{"id":1760,"date":"2020-12-21T10:24:02","date_gmt":"2020-12-21T10:24:02","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1760"},"modified":"2021-10-08T06:56:23","modified_gmt":"2021-10-08T06:56:23","slug":"tkinter-thread-progressbar","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-thread-progressbar\/","title":{"rendered":"How to Display a Progress Bar while a Thread is Running in Tkinter"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn to display a progressbar while a thread is running in a Tkinter application.<\/p>\n\n\n\n<p>This tutorial assumes that you know how to use the <code><a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-after\/\">after()<\/a><\/code> method and understand how <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-thread\/\">threadings<\/a> work in Python. Also, you should know how to <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkraise\/\">switch between frames using the tkraise() method<\/a>.<\/p>\n\n\n\n<p>In this tutorial, you&#8217;ll build a picture viewer that shows a random picture from unsplash.com using its API.<\/p>\n\n\n\n<p>If you make an HTTP request to the following API endpoint:<\/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\">https:\/\/source.unsplash.com\/random\/<span class=\"hljs-number\">640<\/span>x480<\/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>&#8230;you&#8217;ll get a random picture with the size of 640&#215;480.<\/p>\n\n\n\n<p>The following picture shows the final Image Viewer application:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"646\" height=\"541\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Thread-Progressbar.png\" alt=\"\" class=\"wp-image-1762\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Thread-Progressbar.png 646w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Thread-Progressbar-300x251.png 300w\" sizes=\"auto, (max-width: 646px) 100vw, 646px\" \/><\/figure><\/div>\n\n\n\n<p>When you click the <strong>Next Picture <\/strong>button, the program calls the API from unsplash.com to download a random picture and displays it on the window.<\/p>\n\n\n\n<p>It&#8217;ll also show a <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-progressbar\/\">progress bar<\/a> while the picture is downloading, indicating that the download is in progress:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"646\" height=\"541\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Thread-Progressbar-Progressbar.png\" alt=\"\" class=\"wp-image-1763\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Thread-Progressbar-Progressbar.png 646w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Thread-Progressbar-Progressbar-300x251.png 300w\" sizes=\"auto, (max-width: 646px) 100vw, 646px\" \/><\/figure><\/div>\n\n\n\n<p>To call the API, you use the <a href=\"https:\/\/pypi.org\/project\/requests\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\"><code>requests<\/code> module<\/a>. <\/p>\n\n\n\n<p>First, install the <code>requests<\/code> module if it&#8217;s not available on your computer:<\/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\">pip install requests<\/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>Second, <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class\/\">define a new class<\/a> that inherits from the <code><a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-threading\/\">Thread<\/a><\/code> class:<\/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-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">PictureDownload<\/span><span class=\"hljs-params\">(Thread)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, url)<\/span>:<\/span>\n        super().__init__()\n\n        self.picture_file = <span class=\"hljs-literal\">None<\/span>\n        self.url = url\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">run<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-string\">\"\"\" download a picture and save it to a file \"\"\"<\/span>\n        <span class=\"hljs-comment\"># download the picture<\/span>\n        response = requests.get(self.url, proxies=proxyDict)\n        picture_name = self.url.split(<span class=\"hljs-string\">'\/'<\/span>)&#91;<span class=\"hljs-number\">-1<\/span>]\n        picture_file = <span class=\"hljs-string\">f'.\/assets\/<span class=\"hljs-subst\">{picture_name}<\/span>.jpg'<\/span>\n\n        <span class=\"hljs-comment\"># save the picture to a file<\/span>\n        <span class=\"hljs-keyword\">with<\/span> open(picture_file, <span class=\"hljs-string\">'wb'<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n            f.write(response.content)\n\n        self.picture_file = picture_file\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<p>In this <code>PictureDownload<\/code> class, the <code>run()<\/code> method calls the API using the <code>requests<\/code> module. <\/p>\n\n\n\n<p>The <code>run()<\/code> method downloads a picture and saves it to the <code>\/assets\/<\/code> folder. Also, it assigns the path of the downloaded picture to the <code>picture_file<\/code> instance attribute.<\/p>\n\n\n\n<p>Third, define an <code>App<\/code> class that inherits the <code>Tk<\/code> class. The <code>App<\/code> class represents the root window.<\/p>\n\n\n\n<p>The <code>root<\/code> window has two frames, one for displaying the <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-progressbar\/\">Progressbar<\/a> and the other for showing the Canvas which holds the downloaded picture:<\/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-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, canvas_width, canvas_height)<\/span>:<\/span>\n    super().__init__()\n    self.resizable(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>)\n    self.title(<span class=\"hljs-string\">'Image Viewer'<\/span>)\n\n    <span class=\"hljs-comment\"># Progress frame<\/span>\n    self.progress_frame = ttk.Frame(self)\n\n    <span class=\"hljs-comment\"># configrue the grid to place the progress bar is at the center<\/span>\n    self.progress_frame.columnconfigure(<span class=\"hljs-number\">0<\/span>, weight=<span class=\"hljs-number\">1<\/span>)\n    self.progress_frame.rowconfigure(<span class=\"hljs-number\">0<\/span>, weight=<span class=\"hljs-number\">1<\/span>)\n\n    <span class=\"hljs-comment\"># progressbar<\/span>\n    self.pb = ttk.Progressbar(\n        self.progress_frame, orient=tk.HORIZONTAL, mode=<span class=\"hljs-string\">'indeterminate'<\/span>)\n    self.pb.grid(row=<span class=\"hljs-number\">0<\/span>, column=<span class=\"hljs-number\">0<\/span>, sticky=tk.EW, padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>)\n\n    <span class=\"hljs-comment\"># place the progress frame<\/span>\n    self.progress_frame.grid(row=<span class=\"hljs-number\">0<\/span>, column=<span class=\"hljs-number\">0<\/span>, sticky=tk.NSEW)\n\n    <span class=\"hljs-comment\"># Picture frame<\/span>\n    self.picture_frame = ttk.Frame(self)\n\n    <span class=\"hljs-comment\"># canvas width &amp;amp; height<\/span>\n    self.canvas_width = canvas_width\n    self.canvas_height = canvas_height\n\n    <span class=\"hljs-comment\"># canvas<\/span>\n    self.canvas = tk.Canvas(\n        self.picture_frame,\n        width=self.canvas_width,\n        height=self.canvas_height)\n    self.canvas.grid(row=<span class=\"hljs-number\">0<\/span>, column=<span class=\"hljs-number\">0<\/span>)\n\n    self.picture_frame.grid(row=<span class=\"hljs-number\">0<\/span>, column=<span class=\"hljs-number\">0<\/span>)\n<\/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<p>When you click the <code>Next Picture<\/code> button, the <code>handle_download()<\/code> method executes:<\/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\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">handle_download<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n    <span class=\"hljs-string\">\"\"\" Download a random photo from unsplash \"\"\"<\/span>\n    self.start_downloading()\n\n    url = <span class=\"hljs-string\">'https:\/\/source.unsplash.com\/random\/640x480'<\/span>\n    download_thread = PictureDownload(url)\n    download_thread.start()\n\n    self.monitor(download_thread)\n<\/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>The <code>handle_download()<\/code> method shows the progress frame by calling the <code>start_downloading()<\/code> method and starts the progress bar:<\/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\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">start_downloading<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n    self.progress_frame.tkraise()\n    self.pb.start(<span class=\"hljs-number\">20<\/span>)<\/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>It also creates a new thread that downloads the random picture and calls the <code>monitor()<\/code> method to monitor the status of the thread.<\/p>\n\n\n\n<p>The following shows the <code>monitor()<\/code> method:<\/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-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">monitor<\/span><span class=\"hljs-params\">(self, download_thread)<\/span>:<\/span>\n    <span class=\"hljs-string\">\"\"\" Monitor the download thread \"\"\"<\/span>\n    <span class=\"hljs-keyword\">if<\/span> download_thread.is_alive():\n        self.after(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-keyword\">lambda<\/span>: self.monitor(download_thread))\n    <span class=\"hljs-keyword\">else<\/span>:\n        self.stop_downloading()\n        self.set_picture(download_thread.picture_file)<\/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<p>The <code>monitor()<\/code> method checks the status of the thread. If the thread is running, it schedules another check after 100ms. <\/p>\n\n\n\n<p>Otherwise, the <code>monitor()<\/code> method calls the <code>stop_downloading()<\/code> method to stop the progressbar, display the picture frame, and show the image.<\/p>\n\n\n\n<p>The following shows the <code>stop_downloading()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">stop_downloading<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n    self.picture_frame.tkraise()\n    self.pb.stop()   <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><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 shows the complete Image Viewer program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> requests\n<span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n<span class=\"hljs-keyword\">from<\/span> threading <span class=\"hljs-keyword\">import<\/span> Thread\n<span class=\"hljs-keyword\">from<\/span> PIL <span class=\"hljs-keyword\">import<\/span> Image, ImageTk\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\n<span class=\"hljs-keyword\">from<\/span> proxies <span class=\"hljs-keyword\">import<\/span> proxyDict\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">PictureDownload<\/span><span class=\"hljs-params\">(Thread)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, url)<\/span>:<\/span>\n        super().__init__()\n\n        self.picture_file = <span class=\"hljs-literal\">None<\/span>\n        self.url = url\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">run<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-string\">\"\"\" download a picture and save it to a file \"\"\"<\/span>\n        <span class=\"hljs-comment\"># download the picture<\/span>\n        response = requests.get(self.url, proxies=proxyDict)\n        picture_name = self.url.split(<span class=\"hljs-string\">'\/'<\/span>)&#91;<span class=\"hljs-number\">-1<\/span>]\n        picture_file = <span class=\"hljs-string\">f'.\/assets\/<span class=\"hljs-subst\">{picture_name}<\/span>.jpg'<\/span>\n\n        <span class=\"hljs-comment\"># save the picture to a file<\/span>\n        <span class=\"hljs-keyword\">with<\/span> open(picture_file, <span class=\"hljs-string\">'wb'<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n            f.write(response.content)\n\n        self.picture_file = picture_file\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">App<\/span><span class=\"hljs-params\">(tk.Tk)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, canvas_width, canvas_height)<\/span>:<\/span>\n        super().__init__()\n        self.resizable(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>)\n        self.title(<span class=\"hljs-string\">'Image Viewer'<\/span>)\n\n        <span class=\"hljs-comment\"># Progress frame<\/span>\n        self.progress_frame = ttk.Frame(self)\n\n        <span class=\"hljs-comment\"># configrue the grid to place the progress bar is at the center<\/span>\n        self.progress_frame.columnconfigure(<span class=\"hljs-number\">0<\/span>, weight=<span class=\"hljs-number\">1<\/span>)\n        self.progress_frame.rowconfigure(<span class=\"hljs-number\">0<\/span>, weight=<span class=\"hljs-number\">1<\/span>)\n\n        <span class=\"hljs-comment\"># progressbar<\/span>\n        self.pb = ttk.Progressbar(\n            self.progress_frame, orient=tk.HORIZONTAL, mode=<span class=\"hljs-string\">'indeterminate'<\/span>)\n        self.pb.grid(row=<span class=\"hljs-number\">0<\/span>, column=<span class=\"hljs-number\">0<\/span>, sticky=tk.EW, padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>)\n\n        <span class=\"hljs-comment\"># place the progress frame<\/span>\n        self.progress_frame.grid(row=<span class=\"hljs-number\">0<\/span>, column=<span class=\"hljs-number\">0<\/span>, sticky=tk.NSEW)\n\n        <span class=\"hljs-comment\"># Picture frame<\/span>\n        self.picture_frame = ttk.Frame(self)\n\n        <span class=\"hljs-comment\"># canvas width &amp;amp; height<\/span>\n        self.canvas_width = canvas_width\n        self.canvas_height = canvas_height\n\n        <span class=\"hljs-comment\"># canvas<\/span>\n        self.canvas = tk.Canvas(\n            self.picture_frame,\n            width=self.canvas_width,\n            height=self.canvas_height)\n        self.canvas.grid(row=<span class=\"hljs-number\">0<\/span>, column=<span class=\"hljs-number\">0<\/span>)\n\n        self.picture_frame.grid(row=<span class=\"hljs-number\">0<\/span>, column=<span class=\"hljs-number\">0<\/span>)\n\n        <span class=\"hljs-comment\"># Button<\/span>\n        btn = ttk.Button(self, text=<span class=\"hljs-string\">'Next Picture'<\/span>)\n        btn&#91;<span class=\"hljs-string\">'command'<\/span>] = self.handle_download\n        btn.grid(row=<span class=\"hljs-number\">1<\/span>, column=<span class=\"hljs-number\">0<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">start_downloading<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.progress_frame.tkraise()\n        self.pb.start(<span class=\"hljs-number\">20<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">stop_downloading<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.picture_frame.tkraise()\n        self.pb.stop()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">set_picture<\/span><span class=\"hljs-params\">(self, file_path)<\/span>:<\/span>\n        <span class=\"hljs-string\">\"\"\" Set the picture to the canvas \"\"\"<\/span>\n        pil_img = Image.open(file_path)\n\n        <span class=\"hljs-comment\"># resize the picture<\/span>\n        resized_img = pil_img.resize(\n            (self.canvas_width, self.canvas_height),\n            Image.ANTIALIAS)\n\n        self.img = ImageTk.PhotoImage(resized_img)\n\n        <span class=\"hljs-comment\"># set background image<\/span>\n        self.bg = self.canvas.create_image(\n            <span class=\"hljs-number\">0<\/span>,\n            <span class=\"hljs-number\">0<\/span>,\n            anchor=tk.NW,\n            image=self.img)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">handle_download<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-string\">\"\"\" Download a random photo from unsplash \"\"\"<\/span>\n        self.start_downloading()\n\n        url = <span class=\"hljs-string\">'https:\/\/source.unsplash.com\/random\/640x480'<\/span>\n        download_thread = PictureDownload(url)\n        download_thread.start()\n\n        self.monitor(download_thread)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">monitor<\/span><span class=\"hljs-params\">(self, download_thread)<\/span>:<\/span>\n        <span class=\"hljs-string\">\"\"\" Monitor the download thread \"\"\"<\/span>\n        <span class=\"hljs-keyword\">if<\/span> download_thread.is_alive():\n            self.after(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-keyword\">lambda<\/span>: self.monitor(download_thread))\n        <span class=\"hljs-keyword\">else<\/span>:\n            self.stop_downloading()\n            self.set_picture(download_thread.picture_file)\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    app = App(<span class=\"hljs-number\">640<\/span>, <span class=\"hljs-number\">480<\/span>)\n    app.mainloop()\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><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>In this tutorial, you&#8217;ve learned how to display a progressbar that connects to a running thread to indicate that an operation is still in progress.<\/p>\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=\"1760\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-thread-progressbar\/\"\n\t\t\t\tdata-post-title=\"How to Display a Progress Bar while a Thread is Running in Tkinter\"\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=\"1760\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-thread-progressbar\/\"\n\t\t\t\tdata-post-title=\"How to Display a Progress Bar while a Thread is Running in Tkinter\"\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 display a progressbar while a thread is running in a Tkinter application.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":55,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1760","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1760","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=1760"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1760\/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=1760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}