{"id":2145,"date":"2021-01-07T06:39:19","date_gmt":"2021-01-07T06:39:19","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=2145"},"modified":"2022-06-04T07:46:20","modified_gmt":"2022-06-04T07:46:20","slug":"tkinter-optionmenu","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-optionmenu\/","title":{"rendered":"Tkinter OptionMenu"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Tkinter <code>OptionMenu<\/code> widget to display a set of options in a drop-down menu.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-tkinter-optionmenu-widget'>Introduction to the Tkinter OptionMenu widget <a href=\"#introduction-to-the-tkinter-optionmenu-widget\" class=\"anchor\" id=\"introduction-to-the-tkinter-optionmenu-widget\" title=\"Anchor for Introduction to the Tkinter &lt;code&gt;OptionMenu&lt;\/code&gt; widget\">#<\/a><\/h2>\n\n\n\n<p>The <code>OptionMenu<\/code> widget provides you with a predefined set of options in a drop-down menu.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"369\" height=\"237\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-OptionMenu.png\" alt=\"Tkinter OptionMenu\" class=\"wp-image-2150\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-OptionMenu.png 369w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-OptionMenu-300x193.png 300w\" sizes=\"auto, (max-width: 369px) 100vw, 369px\" \/><\/figure>\n\n\n\n<p>To create a new <code>OptionMenu<\/code> widget, you use the <code>OptionMenu<\/code> constructor:<\/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\">OptionMenu(container, variable, default=<span class=\"hljs-literal\">None<\/span>, *values, **kwargs)<\/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 <code>OptionMenu<\/code> constructor accepts a number of parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>container<\/code> is the parent widget of the <code>OptionMenu<\/code> widget.<\/li><li><code>variable<\/code> is a <code>tk.StringVar<\/code> object that holds the currently selected option of the <code>OptionMenu<\/code>.<\/li><li><code>default<\/code> is the default option that the widget displays initially.<\/li><li><code>values<\/code> is a list of values that appear on the drop-down menu.<\/li><li><code>kwargs<\/code> is the widget-specific configuration.<\/li><\/ul>\n\n\n\n<p>The <code>OptionMenu<\/code> allows you to change the direction of the drop-down menu via the <code>direction<\/code> option. The valid directions are <code>'above'<\/code>, <code>'below'<\/code>, <code>'left'<\/code>, <code>'right'<\/code>, or <code>'flush'<\/code>.<\/p>\n\n\n\n<p>The <code>OptionMenu<\/code> widget also supports the <code><a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-command\/\">command<\/a><\/code> option. This allows you to assign a callback that will be called after an item is selected.<\/p>\n\n\n\n<p>Like other <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-ttk\/\">ttk widgets<\/a>, you can specify the style name for the <code>OptionMenu<\/code> using the <code>style<\/code> option.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='tkinter-optionmenu-widget-example'>Tkinter OptionMenu widget example <a href=\"#tkinter-optionmenu-widget-example\" class=\"anchor\" id=\"tkinter-optionmenu-widget-example\" title=\"Anchor for Tkinter &lt;code&gt;OptionMenu&lt;\/code&gt; widget example\">#<\/a><\/h2>\n\n\n\n<p>The following example illustrates how to use an <code>OptionMenu<\/code> widget. When you select an item, it&#8217;ll show your selection in a label:<\/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-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\">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        self.geometry(<span class=\"hljs-string\">\"320x80\"<\/span>)\n        self.title(<span class=\"hljs-string\">'Tkinter OptionMenu Widget'<\/span>)\n\n        <span class=\"hljs-comment\"># initialize data<\/span>\n        self.languages = (<span class=\"hljs-string\">'Python'<\/span>, <span class=\"hljs-string\">'JavaScript'<\/span>, <span class=\"hljs-string\">'Java'<\/span>,\n                        <span class=\"hljs-string\">'Swift'<\/span>, <span class=\"hljs-string\">'GoLang'<\/span>, <span class=\"hljs-string\">'C#'<\/span>, <span class=\"hljs-string\">'C++'<\/span>, <span class=\"hljs-string\">'Scala'<\/span>)\n\n        <span class=\"hljs-comment\"># set up variable<\/span>\n        self.option_var = tk.StringVar(self)\n\n        <span class=\"hljs-comment\"># create widget<\/span>\n        self.create_wigets()\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">create_wigets<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-comment\"># padding for widgets using the grid layout<\/span>\n        paddings = {<span class=\"hljs-string\">'padx'<\/span>: <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-string\">'pady'<\/span>: <span class=\"hljs-number\">5<\/span>}\n\n        <span class=\"hljs-comment\"># label<\/span>\n        label = ttk.Label(self,  text=<span class=\"hljs-string\">'Select your most favorite language:'<\/span>)\n        label.grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">0<\/span>, sticky=tk.W, **paddings)\n\n        <span class=\"hljs-comment\"># option menu<\/span>\n        option_menu = ttk.OptionMenu(\n            self,\n            self.option_var,\n            self.languages&#91;<span class=\"hljs-number\">0<\/span>],\n            *self.languages,\n            command=self.option_changed)\n\n        option_menu.grid(column=<span class=\"hljs-number\">1<\/span>, row=<span class=\"hljs-number\">0<\/span>, sticky=tk.W, **paddings)\n\n        <span class=\"hljs-comment\"># output label<\/span>\n        self.output_label = ttk.Label(self, foreground=<span class=\"hljs-string\">'red'<\/span>)\n        self.output_label.grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">1<\/span>, sticky=tk.W, **paddings)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">option_changed<\/span><span class=\"hljs-params\">(self, *args)<\/span>:<\/span>\n        self.output_label&#91;<span class=\"hljs-string\">'text'<\/span>] = <span class=\"hljs-string\">f'You selected: <span class=\"hljs-subst\">{self.option_var.get()}<\/span>'<\/span>\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-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>Output:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"350\" height=\"231\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-OptionMenu-Demo.gif\" alt=\"Tkinter OptionMenu Demo\" class=\"wp-image-2152\"\/><\/figure>\n<\/div>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, define a list of strings used for displaying on the <code>OptionMenu<\/code> widget: <\/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\">self.languages = (<span class=\"hljs-string\">'Python'<\/span>, <span class=\"hljs-string\">'JavaScript'<\/span>, <span class=\"hljs-string\">'Java'<\/span>, <span class=\"hljs-string\">'Swift'<\/span>, <span class=\"hljs-string\">'GoLang'<\/span>, <span class=\"hljs-string\">'C#'<\/span>, <span class=\"hljs-string\">'C++'<\/span>, <span class=\"hljs-string\">'Scala'<\/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>Second, define a <code>ttk.StringVar()<\/code> object that holds the currently selected item of the <code>OptionMenu<\/code> in the <code>__init__()<\/code> method:<\/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\">self.option_var = tk.StringVar(self)<\/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>Third, create a new instance of the <code>OptionMenu<\/code> widget: <\/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\">option_menu = ttk.OptionMenu(\n    self,\n    self.option_var,\n    self.languages&#91;<span class=\"hljs-number\">0<\/span>],\n    *self.languages,\n    command=self.option_changed)<\/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 class=\"error\">Note that if you skip the default value <code>self.languages[0]<\/code>, the first item of the <code>OptionMenu<\/code> will vanish.<\/p>\n\n\n\n<p>The <code>option_changed()<\/code> method will be executed after an item is selected. The method sets the text for the <code>output_label<\/code> to the selected item:<\/p>\n\n\n<pre class=\"wp-block-code error\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">self<\/span>.output_label&#91;<span class=\"hljs-string\">'text'<\/span>] = f<span class=\"hljs-string\">'You selected: {self.option_var.get()}'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/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 Tkinter <code>OptionMenu<\/code> widget to provide users with a fixed set of choices in a drop-down menu.<\/li><li>Always specify the default value for the <code>OptionMenu<\/code> constructor.<\/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=\"2145\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-optionmenu\/\"\n\t\t\t\tdata-post-title=\"Tkinter OptionMenu\"\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=\"2145\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-optionmenu\/\"\n\t\t\t\tdata-post-title=\"Tkinter OptionMenu\"\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 about the Tkinter OptionMenu widget to display a set of options in a drop-down menu.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":39,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2145","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2145","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=2145"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2145\/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=2145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}