{"id":4575,"date":"2022-09-05T05:09:37","date_gmt":"2022-09-05T05:09:37","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=4575"},"modified":"2022-09-05T06:48:45","modified_gmt":"2022-09-05T06:48:45","slug":"tkinter-cursors","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-cursors\/","title":{"rendered":"Tkinter Cursors"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to set the cursors for widgets in the Tkinter application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='changing-the-cursor-for-the-root-window'>Changing the cursor for the root window <a href=\"#changing-the-cursor-for-the-root-window\" class=\"anchor\" id=\"changing-the-cursor-for-the-root-window\" title=\"Anchor for Changing the cursor for the root window\">#<\/a><\/h2>\n\n\n\n<p>The root window has only two cursors:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Normal cursor<\/li><li>Busy cursor<\/li><\/ul>\n\n\n\n<p>The Normal cursor has the value of <code>\"\"<\/code> while the busy cursor has the value of <code>\"watch\"<\/code>.<\/p>\n\n\n\n<p>The following program shows how to change the cursor of the root window from normal to busy:<\/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\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\n\r\nroot = tk.Tk()\r\n\r\nroot.geometry(<span class=\"hljs-string\">\"300x300\"<\/span>)\r\nroot.config(cursor=<span class=\"hljs-string\">\"watch\"<\/span>)\r\n\r\nroot.mainloop()<\/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>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, set the width and height of the root windows to 300&#215;300.<\/li><li>Second, change the cursor to busy using the cursor parameter.<\/li><\/ul>\n\n\n\n<p>If you want to change the cursor for an area of the root window, you can use the &lt;Motion> event and track the x (and\/or y) coordinate of the cursor. 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\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\n\r\nroot = tk.Tk()\r\nroot.geometry(<span class=\"hljs-string\">\"300x300\"<\/span>)\r\n\r\n\r\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">change_cursor<\/span><span class=\"hljs-params\">(event)<\/span>:<\/span>\r\n    <span class=\"hljs-keyword\">if<\/span> event.x <span class=\"hljs-keyword\">in<\/span> range(<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">300<\/span>):\r\n        root.config(cursor=<span class=\"hljs-string\">\"watch\"<\/span>)\r\n    <span class=\"hljs-keyword\">else<\/span>:\r\n        root.config(cursor=<span class=\"hljs-string\">\"\"<\/span>)\r\n\r\n\r\nroot.bind(<span class=\"hljs-string\">\"&lt;Motion&gt;\"<\/span>, change_cursor)\r\nroot.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>In this example, the cursor changes to busy when the mouse is starting on the x-coordinates from 100 to 300.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='changing-cursor-for-widgets'>Changing cursor for widgets <a href=\"#changing-cursor-for-widgets\" class=\"anchor\" id=\"changing-cursor-for-widgets\" title=\"Anchor for Changing cursor for widgets\">#<\/a><\/h2>\n\n\n\n<p>All <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-ttk\/\">ttk widgets<\/a> have the cursor parameter that allows you to change the cursor when the mouse hovers them.<\/p>\n\n\n\n<p>For example, if you want to change the cursor of a <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-button\/\">button<\/a>, you can set the cursor name using the cursor parameter as follows:<\/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\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\n\n<span class=\"hljs-comment\">#...<\/span>\nbutton = ttk.Button(cursor=cursor_name)<\/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>The cursor name can be one of the following values:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>arrow<\/li><li>based_arrow_down<\/li><li>based_arrow_up<\/li><li>boat<\/li><li>bogosity<\/li><li>bottom_left_corner<\/li><li>bottom_right_corner<\/li><li>bottom_side<\/li><li>bottom_tee<\/li><li>box_spiral<\/li><li>center_ptr<\/li><li>circle<\/li><li>clock<\/li><li>coffee_mug<\/li><li>cross<\/li><li>cross_reverse<\/li><li>crosshair<\/li><li>diamond_cross<\/li><li>dot<\/li><li>dotbox<\/li><li>double_arrow<\/li><li>draft_large<\/li><li>draft_small<\/li><li>draped_box<\/li><li>exchange<\/li><li>fleur<\/li><li>gobbler<\/li><li>gumby<\/li><li>hand1<\/li><li>hand2<\/li><li>heart<\/li><li>icon<\/li><li>iron_cross<\/li><li>left_ptr<\/li><li>left_side<\/li><li>left_tee<\/li><li>leftbutton<\/li><li>ll_angle<\/li><li>lr_angle<\/li><li>man<\/li><li>middlebutton<\/li><li>mouse<\/li><li>pencil<\/li><li>pirate<\/li><li>plus<\/li><li>question_arrow<\/li><li>right_ptr<\/li><li>right_side<\/li><li>right_tee<\/li><li>rightbutton<\/li><li>rtl_logo<\/li><li>sailboat<\/li><li>sb_down_arrow<\/li><li>sb_h_double_arrow<\/li><li>sb_left_arrow<\/li><li>sb_right_arrow<\/li><li>sb_up_arrow<\/li><li>sb_v_double_arrow<\/li><li>shuttle<\/li><li>sizing<\/li><li>spider<\/li><li>spraycan<\/li><li>star<\/li><li>target<\/li><li>tcross<\/li><li>top_left_arrow<\/li><li>top_left_corner<\/li><li>top_right_corner<\/li><li>top_side<\/li><li>top_tee<\/li><li>trek<\/li><li>ul_angle<\/li><li>umbrella<\/li><li>ur_angle<\/li><li>watch<\/li><li>xterm<\/li><li>X_cursor<\/li><\/ul>\n\n\n\n<p>To change the cursor of the root window, you can: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>First, create a new <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-frame\/\">Frame<\/a>. <\/li><li>Second, place it on the root window and expand it 100%.<\/li><li>Third, set the cursor on the frame.<\/li><\/ul>\n\n\n\n<p>The following program allows you to change the cursor by selecting it in a <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-combobox\/\">combobox<\/a>:<\/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-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\n<span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n\nroot = tk.Tk()\n\n<span class=\"hljs-comment\"># config the root window<\/span>\nroot.geometry(<span class=\"hljs-string\">'600x400'<\/span>)\nroot.resizable(<span class=\"hljs-literal\">False<\/span>, <span class=\"hljs-literal\">False<\/span>)\nroot.title(<span class=\"hljs-string\">'Tkinter Cursors'<\/span>)\n\nframe = ttk.Frame(root)\n\n\n<span class=\"hljs-comment\"># label<\/span>\nlabel = ttk.Label(frame, text=<span class=\"hljs-string\">\"Cursor:\"<\/span>)\nlabel.pack(fill=tk.X, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>)\n\n<span class=\"hljs-comment\"># cursor list<\/span>\nselected_cursor = tk.StringVar()\ncursor_list = ttk.Combobox(frame, textvariable=selected_cursor, cursor=<span class=\"hljs-string\">'arrow'<\/span>)\ncursors = &#91;<span class=\"hljs-string\">'arrow'<\/span>, <span class=\"hljs-string\">'man'<\/span>, <span class=\"hljs-string\">'based_arrow_down'<\/span>, <span class=\"hljs-string\">'middlebutton'<\/span>, <span class=\"hljs-string\">'based_arrow_up'<\/span>, <span class=\"hljs-string\">'mouse'<\/span>, <span class=\"hljs-string\">'boat'<\/span>, <span class=\"hljs-string\">'pencil'<\/span>, <span class=\"hljs-string\">'bogosity'<\/span>, <span class=\"hljs-string\">'pirate'<\/span>, <span class=\"hljs-string\">'bottom_left_corner'<\/span>, <span class=\"hljs-string\">'plus'<\/span>, <span class=\"hljs-string\">'bottom_right_corner'<\/span>, <span class=\"hljs-string\">'question_arrow'<\/span>, <span class=\"hljs-string\">'bottom_side'<\/span>, <span class=\"hljs-string\">'right_ptr'<\/span>, <span class=\"hljs-string\">'bottom_tee'<\/span>, <span class=\"hljs-string\">'right_side'<\/span>, <span class=\"hljs-string\">'box_spiral'<\/span>, <span class=\"hljs-string\">'right_tee'<\/span>, <span class=\"hljs-string\">'center_ptr'<\/span>, <span class=\"hljs-string\">'rightbutton'<\/span>, <span class=\"hljs-string\">'circle'<\/span>, <span class=\"hljs-string\">'rtl_logo'<\/span>, <span class=\"hljs-string\">'clock'<\/span>, <span class=\"hljs-string\">'sailboat'<\/span>, <span class=\"hljs-string\">'coffee_mug'<\/span>, <span class=\"hljs-string\">'sb_down_arrow'<\/span>, <span class=\"hljs-string\">'cross'<\/span>, <span class=\"hljs-string\">'sb_h_double_arrow'<\/span>, <span class=\"hljs-string\">'cross_reverse'<\/span>, <span class=\"hljs-string\">'sb_left_arrow'<\/span>, <span class=\"hljs-string\">'crosshair'<\/span>, <span class=\"hljs-string\">'sb_right_arrow'<\/span>, <span class=\"hljs-string\">'diamond_cross'<\/span>,\n           <span class=\"hljs-string\">'sb_up_arrow'<\/span>, <span class=\"hljs-string\">'dot'<\/span>, <span class=\"hljs-string\">'sb_v_double_arrow'<\/span>, <span class=\"hljs-string\">'dotbox'<\/span>, <span class=\"hljs-string\">'shuttle'<\/span>, <span class=\"hljs-string\">'double_arrow'<\/span>, <span class=\"hljs-string\">'sizing'<\/span>, <span class=\"hljs-string\">'draft_large'<\/span>, <span class=\"hljs-string\">'spider'<\/span>, <span class=\"hljs-string\">'draft_small'<\/span>, <span class=\"hljs-string\">'spraycan'<\/span>, <span class=\"hljs-string\">'draped_box'<\/span>, <span class=\"hljs-string\">'star'<\/span>, <span class=\"hljs-string\">'exchange'<\/span>, <span class=\"hljs-string\">'target'<\/span>, <span class=\"hljs-string\">'fleur'<\/span>, <span class=\"hljs-string\">'tcross'<\/span>, <span class=\"hljs-string\">'gobbler'<\/span>, <span class=\"hljs-string\">'top_left_arrow'<\/span>, <span class=\"hljs-string\">'gumby'<\/span>, <span class=\"hljs-string\">'top_left_corner'<\/span>, <span class=\"hljs-string\">'hand1'<\/span>, <span class=\"hljs-string\">'top_right_corner'<\/span>, <span class=\"hljs-string\">'hand2'<\/span>, <span class=\"hljs-string\">'top_side'<\/span>, <span class=\"hljs-string\">'heart'<\/span>, <span class=\"hljs-string\">'top_tee'<\/span>, <span class=\"hljs-string\">'icon'<\/span>, <span class=\"hljs-string\">'trek'<\/span>, <span class=\"hljs-string\">'iron_cross'<\/span>, <span class=\"hljs-string\">'ul_angle'<\/span>, <span class=\"hljs-string\">'left_ptr'<\/span>, <span class=\"hljs-string\">'umbrella'<\/span>, <span class=\"hljs-string\">'left_side'<\/span>, <span class=\"hljs-string\">'ur_angle'<\/span>, <span class=\"hljs-string\">'left_tee'<\/span>, <span class=\"hljs-string\">'watch'<\/span>, <span class=\"hljs-string\">'leftbutton'<\/span>, <span class=\"hljs-string\">'xterm'<\/span>, <span class=\"hljs-string\">'ll_angle'<\/span>, <span class=\"hljs-string\">'X_cursor'<\/span>, <span class=\"hljs-string\">'lr_angle'<\/span>]\ncursor_list&#91;<span class=\"hljs-string\">'values'<\/span>] = cursors\ncursor_list&#91;<span class=\"hljs-string\">'state'<\/span>] = <span class=\"hljs-string\">'readonly'<\/span>\n\n\ncursor_list.pack(fill=tk.X, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>)\n\nframe.pack(expand=<span class=\"hljs-literal\">True<\/span>, fill=tk.BOTH)\n\n\n<span class=\"hljs-comment\"># bind the selected value changes<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">cursor_changed<\/span><span class=\"hljs-params\">(event)<\/span>:<\/span>\n    frame.config(cursor=selected_cursor.get())\n\n\ncursor_list.bind(<span class=\"hljs-string\">'&lt;&lt;ComboboxSelected&gt;&gt;'<\/span>, cursor_changed)\n\nroot.mainloop()<\/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<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>The root window has only two cursors: normal (<code>\"\"<\/code>) and busy (<code>\"watch\"<\/code>).<\/li><li>The widget has many cursors with fixed names.<\/li><li>Use the <code>cursor<\/code> parameter to change the cursor for the root window or a widget.<\/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=\"4575\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-cursors\/\"\n\t\t\t\tdata-post-title=\"Tkinter Cursors\"\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=\"4575\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-cursors\/\"\n\t\t\t\tdata-post-title=\"Tkinter Cursors\"\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 set the cursor for widgets in the Tkinter applications.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":30,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4575","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4575","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=4575"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4575\/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=4575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}