{"id":3093,"date":"2019-08-21T19:42:12","date_gmt":"2019-08-21T17:42:12","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=3093"},"modified":"2019-08-22T13:38:12","modified_gmt":"2019-08-22T11:38:12","slug":"tkinter-and-listbox-2-sync","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/tkinter-and-listbox-2-sync\/","title":{"rendered":"Tkinter and Listbox &#8211; 2 (sync)"},"content":{"rendered":"<h2>Some listboxes in a tkinter GUI<\/h2>\n<p>We were at this point in the <a href=\"https:\/\/pythonprogramming.altervista.org\/tkinter-and-listbox-1\/\">last post about tkinter and listbox<\/a>.<\/p>\n<p>We had two listbox that could scroll using the mousewheel, the arrow keys or dragging the mouse over the listboxes.<\/p>\n<pre class=\"lang:default decode:true \"># lb8\r\nimport tkinter as tk\r\n\r\nroot = tk.Tk()\r\nroot.title(\"Listboxes in Sync\")\r\nroot.geometry(\"400x300+400+200\")\r\n\r\ndef def_listbox(): # =&gt; Listbox\r\n\tlb = tk.Listbox(frame1)\r\n\tlb.pack(side=\"left\", expand=1, fill=tk.BOTH)\r\n\r\n\tfor n in range(100):\r\n\t\tlb.insert(tk.END, n)\r\n\treturn lb\r\n\r\nframe1 = tk.Frame(root)\r\nframe1.pack(expand=1, fill=tk.BOTH)\r\nlb1 = def_listbox()\r\nlb2 = def_listbox()<\/pre>\n<h2>What if we wanted to scroll the two listboxes in sync?<\/h2>\n<h3>Sync with the mousewheel<\/h3>\n<p>Now we sync the listbox scrolling with the mousewheel action.<\/p>\n<p>This function makes a listbox (passed as argument to be moved of a step of four. But, what is that 120 thing? When the mousewheel moves its event.delta returns 120 or -120. If we divide this event.delta by 120 we will get 1 or -1, that multiplicated by 4 will give 4 or -4, so that the listbox will scroll up or down of 4.<\/p>\n<pre class=\"lang:default decode:true \">def scroll(event, lb):\r\n\tlb.yview_scroll(int(-4*(event.delta\/120)), \"units\")<\/pre>\n<p>This function must be binded to the listbox1 (lb1) and 2 (lb2). This is how:<\/p>\n<pre class=\"lang:default decode:true \">lb1.bind(\"&lt;MouseWheel&gt;\", lambda e: scroll(e, lb2))\r\nlb2.bind(\"&lt;MouseWheel&gt;\", lambda e: scroll(e, lb1))<\/pre>\n<h3>Sync with arrowkeys<\/h3>\n<p>Now we want to sync also with the arrow key Up and Down.<\/p>\n<p>We create this function<\/p>\n<pre class=\"lang:default decode:true \">def scroll2(event, num):\r\n\tlb1.yview(\"scroll\", int(num), \"units\")\r\n\tlb2.yview(\"scroll\", int(num), \"units\")<\/pre>\n<p>and we bind the listboxes to the arrow Up and Down like this<\/p>\n<pre class=\"lang:default decode:true \">lb2.bind(\"&lt;Up&gt;\", lambda e: scroll2(e, -1))\r\nlb1.bind(\"&lt;Up&gt;\", lambda e: scroll2(e, -1))\r\nlb2.bind(\"&lt;Down&gt;\", lambda e: scroll2(e, 1))\r\nlb1.bind(\"&lt;Down&gt;\", lambda e: scroll2(e, 1))<\/pre>\n<p>The whole code<\/p>\n<pre class=\"lang:default decode:true \"># lb8\r\nimport tkinter as tk\r\n\r\nroot = tk.Tk()\r\nroot.title(\"Listboxes in Sync\")\r\nroot.geometry(\"400x300+400+200\")\r\n\r\ndef def_listbox(): # =&gt; Listbox\r\n\tlb = tk.Listbox(frame1)\r\n\tlb.pack(side=\"left\", expand=1, fill=tk.BOTH)\r\n\r\n\tfor n in range(100):\r\n\t\tlb.insert(tk.END, n)\r\n\treturn lb\r\n\r\nframe1 = tk.Frame(root)\r\nframe1.pack(expand=1, fill=tk.BOTH)\r\nlb1 = def_listbox()\r\nlb2 = def_listbox()\r\n\r\n\r\n###################################### 2\r\ndef scroll(event, lb):\r\n\tlb.yview_scroll(int(-4*(event.delta\/120)), \"units\")\r\ndef scroll2(event, num):\r\n\tlb1.yview(\"scroll\", int(num), \"units\")\r\n\tlb2.yview(\"scroll\", int(num), \"units\")\r\nlb1.bind(\"&lt;MouseWheel&gt;\", lambda e: scroll(e, lb2))\r\nlb2.bind(\"&lt;MouseWheel&gt;\", lambda e: scroll(e, lb1))\r\n###################################### \/2\r\nlb2.bind(\"&lt;Up&gt;\", lambda e: scroll2(e, -1))\r\nlb1.bind(\"&lt;Up&gt;\", lambda e: scroll2(e, -1))\r\nlb2.bind(\"&lt;Down&gt;\", lambda e: scroll2(e, 1))\r\nlb1.bind(\"&lt;Down&gt;\", lambda e: scroll2(e, 1))\r\nroot.mainloop()\r\n<\/pre>\n<p>To be continued&#8230; ?<\/p>\n","protected":false},"excerpt":{"rendered":"Second part of this topic in which we will see how we can syncronize 2 different listboxes using the mousewheel or the up and down arrow keys.\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/tkinter-and-listbox-2-sync\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":3102,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1,49],"tags":[96,118,518,51],"class_list":["post-3093","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples","category-tkinter","tag-96","tag-listbox","tag-synched","tag-tkinter"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":false,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3093","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=3093"}],"version-history":[{"count":4,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3093\/revisions"}],"predecessor-version":[{"id":3106,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/3093\/revisions\/3106"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/3102"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=3093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=3093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=3093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}