{"id":1385,"date":"2020-12-01T01:50:57","date_gmt":"2020-12-01T01:50:57","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1385"},"modified":"2021-01-05T06:45:19","modified_gmt":"2021-01-05T06:45:19","slug":"tkinter-slider","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-slider\/","title":{"rendered":"Tkinter Slider"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to create a slider using the Tkinter Scale widget.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-tkinter-slider-widget'>Introduction to Tkinter slider widget <a href=\"#introduction-to-tkinter-slider-widget\" class=\"anchor\" id=\"introduction-to-tkinter-slider-widget\" title=\"Anchor for Introduction to Tkinter slider widget\">#<\/a><\/h2>\n\n\n\n<p>A slider allows you to enter a value by moving an indicator. A slider can be vertical or horizontal:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"302\" height=\"232\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Scale.png\" alt=\"\" class=\"wp-image-1391\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Scale.png 302w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Scale-300x230.png 300w\" sizes=\"auto, (max-width: 302px) 100vw, 302px\" \/><\/figure><\/div>\n\n\n\n<p>To create a slider, you&#8217;ll use the <code>ttk.Scale()<\/code> constructor as follows:<\/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\">ttk.Scale(container,from_,to)<\/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>In this syntax, the <code>container<\/code> specifies the parent component of the slider.<\/p>\n\n\n\n<p>The <code>from_<\/code> and <code>to<\/code> options specify the minimum and maximum values of the slider. Since <code>from<\/code> is a keyword in Python, Tkinter uses <code>from_<\/code> instead.<\/p>\n\n\n\n<p>By default, a slider is horizontal. To specify how the slider is arranged, you use the <code>orient<\/code> option which can be horizontal or vertical. For example:<\/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\">slider = ttk.Scale(\n    root,\n    from_=<span class=\"hljs-number\">0<\/span>,\n    to=<span class=\"hljs-number\">100<\/span>,\n    orient=<span class=\"hljs-string\">'vertical'<\/span>,  <span class=\"hljs-comment\"># horizontal<\/span>\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<h3 class=\"wp-block-heading\" id='getting-current-value'>Getting current value <a href=\"#getting-current-value\" class=\"anchor\" id=\"getting-current-value\" title=\"Anchor for Getting current value\">#<\/a><\/h3>\n\n\n\n<p>To get the current value of the slider, you can assign a <code>DoubleVar<\/code> to the <code>variable<\/code> of the slider like this:<\/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\">current_value = tk.DoubleVar()\nslider = ttk.Scale(\n    root,\n    from_=<span class=\"hljs-number\">0<\/span>,\n    to=<span class=\"hljs-number\">100<\/span>,\n    orient=<span class=\"hljs-string\">'horizontal'<\/span>,\n    variable=current_value\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>Another way to get the current value of slider is to call the <code>get()<\/code> method of the slider object:<\/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\">slider.get()<\/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<h3 class=\"wp-block-heading\" id='executing-a-callback'>Executing a callback <a href=\"#executing-a-callback\" class=\"anchor\" id=\"executing-a-callback\" title=\"Anchor for Executing a callback\">#<\/a><\/h3>\n\n\n\n<p>To run a function whenever the value of the slider changes, you can assign it to the <code>command<\/code> option as follows:<\/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\">slider_changed<\/span><span class=\"hljs-params\">(event)<\/span>:<\/span>  \n    print(slider.get())\n\nslider = ttk.Scale(\n    root,\n    from_=<span class=\"hljs-number\">0<\/span>,\n    to=<span class=\"hljs-number\">100<\/span>,\n    orient=<span class=\"hljs-string\">'horizontal'<\/span>,\n    variable=current_value\n    command=slider_changed\n)\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>Notice that calling a function when the value of the slider changes can cause performance problems.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='disabling-the-slider'>Disabling the slider <a href=\"#disabling-the-slider\" class=\"anchor\" id=\"disabling-the-slider\" title=\"Anchor for Disabling the slider\">#<\/a><\/h3>\n\n\n\n<p>To disable the slider, you set its state to <code>'disabled'<\/code>. To re-enable it, you set its state to <code>'normal'<\/code>.<\/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\">slider&#91;<span class=\"hljs-string\">'state'<\/span>] = <span class=\"hljs-string\">'disabled'<\/span>\nslider&#91;<span class=\"hljs-string\">'state'<\/span>] = <span class=\"hljs-string\">'normal'<\/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>By default, the slider&#8217;s state is <code>'normal'<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='tkinter-slider-example'>Tkinter slider example <a href=\"#tkinter-slider-example\" class=\"anchor\" id=\"tkinter-slider-example\" title=\"Anchor for Tkinter slider example\">#<\/a><\/h2>\n\n\n\n<p>The following program illustrates how to use a Tkinter slider widget. The label will update the current value of the slider when you change the slider&#8217;s value.<\/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\n\n<span class=\"hljs-comment\"># root window<\/span>\nroot = tk.Tk()\nroot.geometry(<span class=\"hljs-string\">'300x200'<\/span>)\nroot.resizable(<span class=\"hljs-literal\">False<\/span>, <span class=\"hljs-literal\">False<\/span>)\nroot.title(<span class=\"hljs-string\">'Slider Demo'<\/span>)\n\n\nroot.columnconfigure(<span class=\"hljs-number\">0<\/span>, weight=<span class=\"hljs-number\">1<\/span>)\nroot.columnconfigure(<span class=\"hljs-number\">1<\/span>, weight=<span class=\"hljs-number\">3<\/span>)\n\n\n<span class=\"hljs-comment\"># slider current value<\/span>\ncurrent_value = tk.DoubleVar()\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">get_current_value<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">'{: .2f}'<\/span>.format(current_value.get())\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">slider_changed<\/span><span class=\"hljs-params\">(event)<\/span>:<\/span>\n    value_label.configure(text=get_current_value())\n\n\n<span class=\"hljs-comment\"># label for the slider<\/span>\nslider_label = ttk.Label(\n    root,\n    text=<span class=\"hljs-string\">'Slider:'<\/span>\n)\n\nslider_label.grid(\n    column=<span class=\"hljs-number\">0<\/span>,\n    row=<span class=\"hljs-number\">0<\/span>,\n    sticky=<span class=\"hljs-string\">'w'<\/span>\n)\n\n<span class=\"hljs-comment\">#  slider<\/span>\nslider = ttk.Scale(\n    root,\n    from_=<span class=\"hljs-number\">0<\/span>,\n    to=<span class=\"hljs-number\">100<\/span>,\n    orient=<span class=\"hljs-string\">'horizontal'<\/span>,  <span class=\"hljs-comment\"># vertical<\/span>\n    command=slider_changed,\n    variable=current_value\n)\n\nslider.grid(\n    column=<span class=\"hljs-number\">1<\/span>,\n    row=<span class=\"hljs-number\">0<\/span>,\n    sticky=<span class=\"hljs-string\">'we'<\/span>\n)\n\n<span class=\"hljs-comment\"># current value label<\/span>\ncurrent_value_label = ttk.Label(\n    root,\n    text=<span class=\"hljs-string\">'Current Value:'<\/span>\n)\n\ncurrent_value_label.grid(\n    row=<span class=\"hljs-number\">1<\/span>,\n    columnspan=<span class=\"hljs-number\">2<\/span>,\n    sticky=<span class=\"hljs-string\">'n'<\/span>,\n    ipadx=<span class=\"hljs-number\">10<\/span>,\n    ipady=<span class=\"hljs-number\">10<\/span>\n)\n\n<span class=\"hljs-comment\"># value label<\/span>\nvalue_label = ttk.Label(\n    root,\n    text=get_current_value()\n)\nvalue_label.grid(\n    row=<span class=\"hljs-number\">2<\/span>,\n    columnspan=<span class=\"hljs-number\">2<\/span>,\n    sticky=<span class=\"hljs-string\">'n'<\/span>\n)\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<p>Output:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"302\" height=\"232\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Slider.png\" alt=\"\" class=\"wp-image-1388\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Slider.png 302w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Slider-300x230.png 300w\" sizes=\"auto, (max-width: 302px) 100vw, 302px\" \/><\/figure><\/div>\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>Use the <code>ttk.Scale()<\/code> to create a slider widget.<\/li><li>Use the <code>scale.get()<\/code> or the <code>variable<\/code> option to get the current value of the slider.<\/li><li>Use the <code>command<\/code> option to assign a function that will execute when the slider&#8217;s value changes.<\/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=\"1385\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-slider\/\"\n\t\t\t\tdata-post-title=\"Tkinter Slider\"\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=\"1385\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-slider\/\"\n\t\t\t\tdata-post-title=\"Tkinter Slider\"\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 Tkinter Scal widget to create a slider that allows entering a value by moving an indicator.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":22,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1385","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1385","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=1385"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1385\/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=1385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}