{"id":1785,"date":"2020-12-23T03:42:52","date_gmt":"2020-12-23T03:42:52","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1785"},"modified":"2020-12-24T03:42:42","modified_gmt":"2020-12-24T03:42:42","slug":"tkinter-toplevel","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-toplevel\/","title":{"rendered":"Tkinter Toplevel"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to create multiple windows in a Tkinter application using the Tkinter Toplevel class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-tkinter-toplevel-window'>Introduction to Tkinter Toplevel window <a href=\"#introduction-to-tkinter-toplevel-window\" class=\"anchor\" id=\"introduction-to-tkinter-toplevel-window\" title=\"Anchor for Introduction to Tkinter Toplevel window\">#<\/a><\/h2>\n\n\n\n<p>In a Tkinter application, the instance of the <code>Tk<\/code> class represents the <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-window\/\">main window<\/a>.<\/p>\n\n\n\n<p>When you destroy the main window, the application exits, and the event loop finishes.<\/p>\n\n\n\n<p>Sometimes, you need to create additional windows. For example, you may want to create a custom dialog or a wizard form.<\/p>\n\n\n\n<p>To do that, you can use top-level windows which are instances of the <code>Toplevel<\/code> class.<\/p>\n\n\n\n<p>Unlike the main window, you can create as many top-level windows as you want.<\/p>\n\n\n\n<p>To show a <code>Toplevel<\/code> window from the main window, you follow these steps:<\/p>\n\n\n\n<p>First, create an instance of the <code>Toplevel<\/code> class and set its parent to the root window:<\/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\">window = tk.Toplevel(root)<\/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>The moment you create the <code>Toplevel<\/code> window, it&#8217;ll display on the screen.<\/p>\n\n\n\n<p>Second, add widgets to the <code>Toplevel<\/code> window like you do with the <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-frame\/\">frames<\/a> and <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-window\/\">main window<\/a>.<\/p>\n\n\n\n<p>Third, call the <code>grab_set()<\/code> method of the <code>Toplevel<\/code> window instance to allow it to receive events and prevent users from interacting with the main window:<\/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\">window.grab_set()<\/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<h2 class=\"wp-block-heading\" id='a-simple-tkinter-toplevel-window-example'>A simple Tkinter Toplevel Window example <a href=\"#a-simple-tkinter-toplevel-window-example\" class=\"anchor\" id=\"a-simple-tkinter-toplevel-window-example\" title=\"Anchor for A simple Tkinter Toplevel Window example\">#<\/a><\/h2>\n\n\n\n<p>The following program displays a window that has one <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-button\/\">button<\/a>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"587\" height=\"280\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Toplevel-Window.png\" alt=\"\" class=\"wp-image-1787\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Toplevel-Window.png 587w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Toplevel-Window-300x143.png 300w\" sizes=\"auto, (max-width: 587px) 100vw, 587px\" \/><\/figure>\n\n\n\n<p>When you click the button, it&#8217;ll open a <code>Toplevel<\/code> window. The <code>Toplevel<\/code> window also consists of a button.<\/p>\n\n\n\n<p>If you click or press the <code>Close<\/code> button, the <code>Toplevel<\/code> window closes.<\/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\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Window<\/span><span class=\"hljs-params\">(tk.Toplevel)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, parent)<\/span>:<\/span>\n        super().__init__(parent)\n\n        self.geometry(<span class=\"hljs-string\">'300x100'<\/span>)\n        self.title(<span class=\"hljs-string\">'Toplevel Window'<\/span>)\n\n        ttk.Button(self,\n                text=<span class=\"hljs-string\">'Close'<\/span>,\n                command=self.destroy).pack(expand=<span class=\"hljs-literal\">True<\/span>)\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)<\/span>:<\/span>\n        super().__init__()\n\n        self.geometry(<span class=\"hljs-string\">'300x200'<\/span>)\n        self.title(<span class=\"hljs-string\">'Main Window'<\/span>)\n\n        <span class=\"hljs-comment\"># place a button on the root window<\/span>\n        ttk.Button(self,\n                text=<span class=\"hljs-string\">'Open a window'<\/span>,\n                command=self.open_window).pack(expand=<span class=\"hljs-literal\">True<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">open_window<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        window = Window(self)\n        window.grab_set()\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">\"__main__\"<\/span>:\n    app = App()\n    app.mainloop()<\/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>How it works.<\/p>\n\n\n\n<p>First, <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-class\/\">define a class<\/a> <code>Window<\/code> that <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-inheritance\/\">inherits<\/a> from the <code>Toplevel<\/code> window. The <code>Window<\/code> will be closed once the <code>Close<\/code> button is clicked.<\/p>\n\n\n\n<p>Second, assign the <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-command\/\">command<\/a> of the <strong>Open a window<\/strong> button to the <code>open_window()<\/code> method in the <code>App<\/code> class<\/p>\n\n\n\n<p>Third, in the <code>open_window()<\/code> method, create a new instance of the <code>Window<\/code> and call the <code>grab_set()<\/code> method so that it can receive events. The <code>grab_set()<\/code> method also prevents users from interacting with the main window.<\/p>\n\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>Show additional windows by creating instances of the <code>Toplevel<\/code> class.<\/li><li>Use <code>grab_set()<\/code> method so that the <code>Toplevel<\/code> window can receive events and prevents users from interacting with the main window.<\/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=\"1785\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-toplevel\/\"\n\t\t\t\tdata-post-title=\"Tkinter Toplevel\"\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=\"1785\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-toplevel\/\"\n\t\t\t\tdata-post-title=\"Tkinter Toplevel\"\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 create multiple windows in a Tkinter application using the Tkinter Toplevel class.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":56,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1785","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1785","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=1785"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1785\/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=1785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}