{"id":1527,"date":"2020-12-08T10:01:08","date_gmt":"2020-12-08T10:01:08","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1527"},"modified":"2020-12-22T01:29:15","modified_gmt":"2020-12-22T01:29:15","slug":"tkraise","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkraise\/","title":{"rendered":"Switching between Frames Using the Frame tkraise() Method"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Frame <code>tkraise()<\/code> method to switch between frames in a Tkinter application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-frame-tkraise-method'>Introduction to the Frame tkraise() method <a href=\"#introduction-to-the-frame-tkraise-method\" class=\"anchor\" id=\"introduction-to-the-frame-tkraise-method\" title=\"Anchor for Introduction to the Frame tkraise() method\">#<\/a><\/h2>\n\n\n\n<p>Typically, a Tkinter application consists of multiple <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-frame\/\">frames<\/a>. And you often need to switch between frames to display the one that is relevant to the user&#8217;s choice.<\/p>\n\n\n\n<p>Tkinter allows you to stack frames on top of each other. To show a particular frame, you can simply raise one above the other in the stacking order. The top frame will be visible.<\/p>\n\n\n\n<p>To bring  a frame to the top, you use the <code>tkraise()<\/code> method of the Frame widget like this:<\/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\">frame.tkraise()<\/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<h2 class=\"wp-block-heading\" id='the-frame-tkraise-method-example'>The Frame tkraise() method example <a href=\"#the-frame-tkraise-method-example\" class=\"anchor\" id=\"the-frame-tkraise-method-example\" title=\"Anchor for The Frame tkraise() method example\">#<\/a><\/h2>\n\n\n\n<p>In this example, you&#8217;ll extend the <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-object-oriented-application\/\">temperature converter application<\/a> by adding the conversion of a temperature from Celsius to Fahrenheit:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"302\" height=\"102\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Example.png\" alt=\"\" class=\"wp-image-1500\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Example.png 302w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Example-300x101.png 300w\" sizes=\"auto, (max-width: 302px) 100vw, 302px\" \/><\/figure><\/div>\n\n\n\n<p>By default, the application converts a temperature from Fahrenheit to Celsius. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"302\" height=\"152\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-tkraise-f-to-c.png\" alt=\"\" class=\"wp-image-1528\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-tkraise-f-to-c.png 302w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-tkraise-f-to-c-300x151.png 300w\" sizes=\"auto, (max-width: 302px) 100vw, 302px\" \/><\/figure><\/div>\n\n\n\n<p>If you select the C to F <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-radio-button\/\">radio button<\/a>, the application shows a new frame that allows you to convert a temperature from Celsius to Fahrenheit:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"302\" height=\"152\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-tkraise-c-to-f.png\" alt=\"\" class=\"wp-image-1529\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-tkraise-c-to-f.png 302w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-tkraise-c-to-f-300x151.png 300w\" sizes=\"auto, (max-width: 302px) 100vw, 302px\" \/><\/figure><\/div>\n\n\n\n<p>To build this application, you&#8217;ll need to have three main widgets:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A root window.<\/li><li>The <code>ConverterFrame<\/code> that shows the form fields.<\/li><li>And the <code>ControlFrame<\/code> that shows the radio buttons.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"548\" height=\"281\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-tkraise-frames-and-windows.png\" alt=\"\" class=\"wp-image-1530\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-tkraise-frames-and-windows.png 548w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-tkraise-frames-and-windows-300x154.png 300w\" sizes=\"auto, (max-width: 548px) 100vw, 548px\" \/><\/figure>\n\n\n\n<p>The <code>ConverterFrame<\/code> will have two instances, one that converts a temperature from Fahrenheit to Celsius and the other that converts a temperature from Celsius to Fahrenheit:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"355\" height=\"222\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-tkraise-Frames-1.png\" alt=\"\" class=\"wp-image-1532\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-tkraise-Frames-1.png 355w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-tkraise-Frames-1-300x188.png 300w\" sizes=\"auto, (max-width: 355px) 100vw, 355px\" \/><\/figure><\/div>\n\n\n\n<p>First, define a <code>TemperatureConverter<\/code> class that has two static methods: <code>fahrenheit_to_celsius<\/code> and <code>celsius_to_fahrenheit<\/code>.<\/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\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TemperatureConverter<\/span>:<\/span>\n<span class=\"hljs-meta\">    @staticmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">fahrenheit_to_celsius<\/span><span class=\"hljs-params\">(f, format=True)<\/span>:<\/span>\n        result = (f - <span class=\"hljs-number\">32<\/span>) * <span class=\"hljs-number\">5<\/span>\/<span class=\"hljs-number\">9<\/span>\n        <span class=\"hljs-keyword\">if<\/span> format:\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f'<span class=\"hljs-subst\">{f}<\/span> Fahrenheit = <span class=\"hljs-subst\">{result:<span class=\"hljs-number\">.2<\/span>f}<\/span> Celsius'<\/span>\n        <span class=\"hljs-keyword\">return<\/span> result\n\n<span class=\"hljs-meta\">    @staticmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">celsius_to_fahrenheit<\/span><span class=\"hljs-params\">(c, format=True)<\/span>:<\/span>\n        result = c * <span class=\"hljs-number\">9<\/span>\/<span class=\"hljs-number\">5<\/span> + <span class=\"hljs-number\">32<\/span>\n        <span class=\"hljs-keyword\">if<\/span> format:\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f'<span class=\"hljs-subst\">{c}<\/span> Celsius = <span class=\"hljs-subst\">{result:<span class=\"hljs-number\">.2<\/span>f}<\/span> Fahrenheit'<\/span>\n        <span class=\"hljs-keyword\">return<\/span> result\n<\/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>fahrenheit_to_celsius<\/code> and  <code>celsius_to_fahrenheit<\/code> methods return a formatted string if you ignore the second argument or pass <code>True<\/code> to them. Otherwise, they&#8217;ll return the result as a number.<\/p>\n\n\n\n<p>Second, define the <code>ConverterFrame<\/code> that will show the UI for converting temperature from Fahrenheit to Celsius and vice versa.<\/p>\n\n\n\n<p>To do it, you&#8217;ll need to make the <code>ConverterFrame<\/code> more flexible by adding the following parameters to the <code>__init__()<\/code> method:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A string that will be displayed as Fahrenheit and Celsius<\/li><li>A callback function for converting the temperature.<\/li><\/ul>\n\n\n\n<p>The following shows a complete <code>ConverterFrame<\/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\">ConverterFrame<\/span><span class=\"hljs-params\">(ttk.Frame)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, container, unit_from, converter)<\/span>:<\/span>\n        super().__init__(container)\n\n        self.unit_from = unit_from\n        self.converter = converter\n\n        <span class=\"hljs-comment\"># field options<\/span>\n        options = {<span class=\"hljs-string\">'padx'<\/span>: <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-string\">'pady'<\/span>: <span class=\"hljs-number\">0<\/span>}\n\n        <span class=\"hljs-comment\"># temperature label<\/span>\n        self.temperature_label = ttk.Label(self, text=self.unit_from)\n        self.temperature_label.grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">0<\/span>, sticky=<span class=\"hljs-string\">'w'<\/span>,  **options)\n\n        <span class=\"hljs-comment\"># temperature entry<\/span>\n        self.temperature = tk.StringVar()\n        self.temperature_entry = ttk.Entry(self, textvariable=self.temperature)\n        self.temperature_entry.grid(column=<span class=\"hljs-number\">1<\/span>, row=<span class=\"hljs-number\">0<\/span>, sticky=<span class=\"hljs-string\">'w'<\/span>, **options)\n        self.temperature_entry.focus()\n\n        <span class=\"hljs-comment\"># button<\/span>\n        self.convert_button = ttk.Button(self, text=<span class=\"hljs-string\">'Convert'<\/span>)\n        self.convert_button.grid(column=<span class=\"hljs-number\">2<\/span>, row=<span class=\"hljs-number\">0<\/span>, sticky=<span class=\"hljs-string\">'w'<\/span>, **options)\n        self.convert_button.configure(command=self.convert)\n\n        <span class=\"hljs-comment\"># result label<\/span>\n        self.result_label = ttk.Label(self)\n        self.result_label.grid(row=<span class=\"hljs-number\">1<\/span>, columnspan=<span class=\"hljs-number\">3<\/span>, **options)\n\n        <span class=\"hljs-comment\"># add padding to the frame and show it<\/span>\n        self.grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">0<\/span>, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>, sticky=<span class=\"hljs-string\">\"nsew\"<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">convert<\/span><span class=\"hljs-params\">(self, event=None)<\/span>:<\/span>\n        <span class=\"hljs-string\">\"\"\"  Handle button click event\n        \"\"\"<\/span>\n        <span class=\"hljs-keyword\">try<\/span>:\n            input_value = float(self.temperature.get())\n            result = self.converter(input_value)\n            self.result_label.config(text=result)\n        <span class=\"hljs-keyword\">except<\/span> ValueError <span class=\"hljs-keyword\">as<\/span> error:\n            showerror(title=<span class=\"hljs-string\">'Error'<\/span>, message=error)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">reset<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.temperature_entry.delete(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-string\">\"end\"<\/span>)\n        self.result_label.text = <span class=\"hljs-string\">''<\/span><\/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<ul class=\"wp-block-list\"><li>Use the <code>unit_from<\/code> argument to show the label for the temperature.<\/li><li>Call the <code>self.convert<\/code> callback in the <code>convert()<\/code> method to convert a temperature from one unit to another.<\/li><li>Define the <code>reset()<\/code> method to clear the entry widget and the result label when the frame is switched from one to another.<\/li><\/ul>\n\n\n\n<p>Third, define a <code>ControlFrame<\/code> class that shows the radio buttons for selecting a frame to show. The <code>ControFrame<\/code> class inherits from the <code>ttk.LabelFrame<\/code>.<\/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-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ControlFrame<\/span><span class=\"hljs-params\">(ttk.LabelFrame)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, container)<\/span>:<\/span>\n\n        super().__init__(container)\n        self&#91;<span class=\"hljs-string\">'text'<\/span>] = <span class=\"hljs-string\">'Options'<\/span>\n\n        <span class=\"hljs-comment\"># radio buttons<\/span>\n        self.selected_value = tk.IntVar()\n\n        ttk.Radiobutton(\n            self,\n            text=<span class=\"hljs-string\">'F to C'<\/span>,\n            value=<span class=\"hljs-number\">0<\/span>,\n            variable=self.selected_value,\n            command=self.change_frame).grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">0<\/span>, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>)\n\n        ttk.Radiobutton(\n            self,\n            text=<span class=\"hljs-string\">'C to F'<\/span>,\n            value=<span class=\"hljs-number\">1<\/span>,\n            variable=self.selected_value,\n            command=self.change_frame).grid(column=<span class=\"hljs-number\">1<\/span>, row=<span class=\"hljs-number\">0<\/span>, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>)\n\n        self.grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">1<\/span>, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>, sticky=<span class=\"hljs-string\">'ew'<\/span>)\n\n        <span class=\"hljs-comment\"># initialize frames<\/span>\n        self.frames = {}\n        self.frames&#91;<span class=\"hljs-number\">0<\/span>] = ConverterFrame(\n            container,\n            <span class=\"hljs-string\">'Fahrenheit'<\/span>,\n            TemperatureConverter.fahrenheit_to_celsius)\n        self.frames&#91;<span class=\"hljs-number\">1<\/span>] = ConverterFrame(\n            container,\n            <span class=\"hljs-string\">'Celsius'<\/span>,\n            TemperatureConverter.celsius_to_fahrenheit)\n\n        self.change_frame()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">change_frame<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        frame = self.frames&#91;self.selected_value.get()]\n        frame.reset()\n        frame.tkraise()<\/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>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Each radio button holds a value 0 or 1.<\/li><li>Create two instances of the <code>ConverterFrame<\/code> class, one is in charge of converting a temperature from Fahrenheit to Celsius, and the other converts a temperature from Celsius to Fahrenheit. Also, define a dictionary to store these frames. The keys of frames are the same as the values of the radio buttons.<\/li><li>When a radio button is clicked, the <code>change_frame()<\/code> method is called to select the corresponding frame from the dictionary based on the value of the selected button.<\/li><li>Call the <code>reset()<\/code> method of the frame to reset the entry field and result label.  And also invoke the <code>tkraise()<\/code> method to display the frame.<\/li><\/ul>\n\n\n\n<p>Fourth, define the <code>App<\/code> class that subclasses from the <code>tk.Tk<\/code> class:<\/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-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.title(<span class=\"hljs-string\">'Temperature Converter'<\/span>)\n        self.geometry(<span class=\"hljs-string\">'300x120'<\/span>)\n        self.resizable(<span class=\"hljs-literal\">False<\/span>, <span class=\"hljs-literal\">False<\/span>)\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>Finally, bootstrap the application from the <code>if&nbsp;__name__&nbsp;==&nbsp;\"__main__\"<\/code> block:<\/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-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">\"__main__\"<\/span>:\n    app = App()\n    ControlFrame(app)\n    app.mainloop()<\/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>Put it all together.<\/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.messagebox <span class=\"hljs-keyword\">import<\/span> showerror\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TemperatureConverter<\/span>:<\/span>\n<span class=\"hljs-meta\">    @staticmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">fahrenheit_to_celsius<\/span><span class=\"hljs-params\">(f, format=True)<\/span>:<\/span>\n        result = (f - <span class=\"hljs-number\">32<\/span>) * <span class=\"hljs-number\">5<\/span>\/<span class=\"hljs-number\">9<\/span>\n        <span class=\"hljs-keyword\">if<\/span> format:\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f'<span class=\"hljs-subst\">{f}<\/span> Fahrenheit = <span class=\"hljs-subst\">{result:<span class=\"hljs-number\">.2<\/span>f}<\/span> Celsius'<\/span>\n        <span class=\"hljs-keyword\">return<\/span> result\n\n<span class=\"hljs-meta\">    @staticmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">celsius_to_fahrenheit<\/span><span class=\"hljs-params\">(c, format=True)<\/span>:<\/span>\n        result = c * <span class=\"hljs-number\">9<\/span>\/<span class=\"hljs-number\">5<\/span> + <span class=\"hljs-number\">32<\/span>\n        <span class=\"hljs-keyword\">if<\/span> format:\n            <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f'<span class=\"hljs-subst\">{c}<\/span> Celsius = <span class=\"hljs-subst\">{result:<span class=\"hljs-number\">.2<\/span>f}<\/span> Fahrenheit'<\/span>\n        <span class=\"hljs-keyword\">return<\/span> result\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ConverterFrame<\/span><span class=\"hljs-params\">(ttk.Frame)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, container, unit_from, converter)<\/span>:<\/span>\n        super().__init__(container)\n\n        self.unit_from = unit_from\n        self.converter = converter\n\n        <span class=\"hljs-comment\"># field options<\/span>\n        options = {<span class=\"hljs-string\">'padx'<\/span>: <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-string\">'pady'<\/span>: <span class=\"hljs-number\">0<\/span>}\n\n        <span class=\"hljs-comment\"># temperature label<\/span>\n        self.temperature_label = ttk.Label(self, text=self.unit_from)\n        self.temperature_label.grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">0<\/span>, sticky=<span class=\"hljs-string\">'w'<\/span>,  **options)\n\n        <span class=\"hljs-comment\"># temperature entry<\/span>\n        self.temperature = tk.StringVar()\n        self.temperature_entry = ttk.Entry(self, textvariable=self.temperature)\n        self.temperature_entry.grid(column=<span class=\"hljs-number\">1<\/span>, row=<span class=\"hljs-number\">0<\/span>, sticky=<span class=\"hljs-string\">'w'<\/span>, **options)\n        self.temperature_entry.focus()\n\n        <span class=\"hljs-comment\"># button<\/span>\n        self.convert_button = ttk.Button(self, text=<span class=\"hljs-string\">'Convert'<\/span>)\n        self.convert_button.grid(column=<span class=\"hljs-number\">2<\/span>, row=<span class=\"hljs-number\">0<\/span>, sticky=<span class=\"hljs-string\">'w'<\/span>, **options)\n        self.convert_button.configure(command=self.convert)\n\n        <span class=\"hljs-comment\"># result label<\/span>\n        self.result_label = ttk.Label(self)\n        self.result_label.grid(row=<span class=\"hljs-number\">1<\/span>, columnspan=<span class=\"hljs-number\">3<\/span>, **options)\n\n        <span class=\"hljs-comment\"># add padding to the frame and show it<\/span>\n        self.grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">0<\/span>, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>, sticky=<span class=\"hljs-string\">\"nsew\"<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">convert<\/span><span class=\"hljs-params\">(self, event=None)<\/span>:<\/span>\n        <span class=\"hljs-string\">\"\"\"  Handle button click event\n        \"\"\"<\/span>\n        <span class=\"hljs-keyword\">try<\/span>:\n            input_value = float(self.temperature.get())\n            result = self.converter(input_value)\n            self.result_label.config(text=result)\n        <span class=\"hljs-keyword\">except<\/span> ValueError <span class=\"hljs-keyword\">as<\/span> error:\n            showerror(title=<span class=\"hljs-string\">'Error'<\/span>, message=error)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">reset<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.temperature_entry.delete(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-string\">\"end\"<\/span>)\n        self.result_label.text = <span class=\"hljs-string\">''<\/span>\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">ControlFrame<\/span><span class=\"hljs-params\">(ttk.LabelFrame)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, container)<\/span>:<\/span>\n\n        super().__init__(container)\n        self&#91;<span class=\"hljs-string\">'text'<\/span>] = <span class=\"hljs-string\">'Options'<\/span>\n\n        <span class=\"hljs-comment\"># radio buttons<\/span>\n        self.selected_value = tk.IntVar()\n\n        ttk.Radiobutton(\n            self,\n            text=<span class=\"hljs-string\">'F to C'<\/span>,\n            value=<span class=\"hljs-number\">0<\/span>,\n            variable=self.selected_value,\n            command=self.change_frame).grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">0<\/span>, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>)\n\n        ttk.Radiobutton(\n            self,\n            text=<span class=\"hljs-string\">'C to F'<\/span>,\n            value=<span class=\"hljs-number\">1<\/span>,\n            variable=self.selected_value,\n            command=self.change_frame).grid(column=<span class=\"hljs-number\">1<\/span>, row=<span class=\"hljs-number\">0<\/span>, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>)\n\n        self.grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">1<\/span>, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>, sticky=<span class=\"hljs-string\">'ew'<\/span>)\n\n        <span class=\"hljs-comment\"># initialize frames<\/span>\n        self.frames = {}\n        self.frames&#91;<span class=\"hljs-number\">0<\/span>] = ConverterFrame(\n            container,\n            <span class=\"hljs-string\">'Fahrenheit'<\/span>,\n            TemperatureConverter.fahrenheit_to_celsius)\n        self.frames&#91;<span class=\"hljs-number\">1<\/span>] = ConverterFrame(\n            container,\n            <span class=\"hljs-string\">'Celsius'<\/span>,\n            TemperatureConverter.celsius_to_fahrenheit)\n\n        self.change_frame()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">change_frame<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        frame = self.frames&#91;self.selected_value.get()]\n        frame.reset()\n        frame.tkraise()\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.title(<span class=\"hljs-string\">'Temperature Converter'<\/span>)\n        self.geometry(<span class=\"hljs-string\">'300x120'<\/span>)\n        self.resizable(<span class=\"hljs-literal\">False<\/span>, <span class=\"hljs-literal\">False<\/span>)\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">\"__main__\"<\/span>:\n    app = App()\n    ControlFrame(app)\n    app.mainloop()\n<\/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 <code>tkraise()<\/code> method to bring a frame on top a list of frames.<\/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=\"1527\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkraise\/\"\n\t\t\t\tdata-post-title=\"Switching between Frames Using the Frame tkraise() Method\"\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=\"1527\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkraise\/\"\n\t\t\t\tdata-post-title=\"Switching between Frames Using the Frame tkraise() Method\"\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 use the Frame tkraise() method to switch between frames in a Tkinter application.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":44,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1527","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1527","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=1527"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1527\/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=1527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}