{"id":1673,"date":"2020-11-13T00:00:00","date_gmt":"2020-11-12T15:00:00","guid":{"rendered":"https:\/\/www.ishikawasekkei.com\/?p=1673"},"modified":"2020-11-12T20:11:30","modified_gmt":"2020-11-12T11:11:30","slug":"python-tkinter-gui-programing-canvas-box-connection-part2","status":"publish","type":"post","link":"https:\/\/www.ishikawasekkei.com\/index.php\/2020\/11\/13\/python-tkinter-gui-programing-canvas-box-connection-part2\/","title":{"rendered":"Python tkinter GUI\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 Canvas\u3067\u7bb1\u3092\u3064\u306a\u3050\u7dda\u3092\u63cf\u304f\u305d\u306e\uff12"},"content":{"rendered":"\n<p>\u3000\u4eca\u65e5\u3082\u898b\u306b\u6765\u3066\u304f\u3060\u3055\u3063\u3066\u3001\u3042\u308a\u304c\u3068\u3046\u3054\u3056\u3044\u307e\u3059\u3002\u77f3\u5ddd\u3055\u3093\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u3000\u524d\u56de\u306e\u5ba3\u8a00\u901a\u308a\u3001\u4eca\u56de\u306f\u9670\u7dda\u51e6\u7406\u3067\u3059\u3002\u7bb1\u306e\u4e2d\u5fc3\u304b\u3089\u4e2d\u5fc3\u3078\u7dda\u3092\u5f15\u304f\u3068\u3053\u308d\u307e\u3067\u306f\u540c\u3058\u306a\u306e\u3067\u3059\u304c\u3001\u7bb1\u306b\u304b\u3076\u308b\u90e8\u5206\u306f\u7dda\u3092\u5f15\u304b\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<a href=\"https:\/\/stackoverflow.com\/questions\/50252070\/svg-draw-connection-line-between-two-rectangles\" target=\"_blank\" rel=\"noreferrer noopener\">\u3053\u306e\u30da\u30fc\u30b8<\/a>\u3092\u53c2\u8003\u306b\u3055\u305b\u3066\u3044\u305f\u3060\u304d\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u3067\u304d\u3042\u304c\u308a\u30a4\u30e1\u30fc\u30b8<\/h2>\n\n\n\n<p>\u3000\u3067\u304d\u3042\u304c\u308a\u30a4\u30e1\u30fc\u30b8\u306f\u524d\u56de\u3068\u307b\u307c\u5909\u5316\u306a\u3057\u3067\u3059\u3002\u4e2d\u5fc3\u304b\u3089\u7bb1\u307e\u3067\u306e\u7dda\u304c\u63cf\u304b\u308c\u306a\u304f\u306a\u3063\u305f\u3060\u3051\u3067\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"642\" height=\"366\" src=\"https:\/\/www.ishikawasekkei.com\/wp-content\/uploads\/2020\/11\/image-9.png\" alt=\"\" class=\"wp-image-1674\" srcset=\"https:\/\/www.ishikawasekkei.com\/wp-content\/uploads\/2020\/11\/image-9.png 642w, https:\/\/www.ishikawasekkei.com\/wp-content\/uploads\/2020\/11\/image-9-300x171.png 300w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><figcaption>\u7bb1\u3068\u7bb1\u3092\u7dda\u3067\u7d50\u3076\u3002\u305f\u3060\u3057\u3001\u7bb1\u306e\u4e2d\u306e\u7dda\u3092\u53d6\u308a\u9664\u304f\u3002<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h2>\n\n\n\n<p>\u3000\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002\u5909\u66f4\u70b9\u306fConnection\u30af\u30e9\u30b9\u3060\u3051\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import tkinter as tk\n\n\nclass Entity():\n    ''' Entity class '''\n    def __init__(self, canvas, x, y, width=60, height=40):\n        self.canvas = canvas\n        self.x, self.y, self.width, self.height = x, y, width, height\n        self.start_x = self.start_y = None\n        self.connections = []\n\n        self.id = self.canvas.create_rectangle(x, y, x + width, y + height,\n                                               fill=\"lightblue\", width=3)\n        self.canvas.tag_bind(self.id, \"&lt;ButtonPress>\", self.button_press)\n        self.canvas.tag_bind(self.id, \"&lt;Motion>\", self.move)\n        self.canvas.tag_bind(self.id, \"&lt;ButtonRelease>\", self.button_release)\n\n    def button_press(self, event):\n        ''' \u30de\u30a6\u30b9\u306e\u30dc\u30bf\u30f3\u304c\u62bc\u3055\u308c\u305f\u3068\u304d\u306e\u51e6\u7406 '''\n        self.start_x = self.canvas.canvasx(event.x)\n        self.start_y = self.canvas.canvasy(event.y)\n\n    def move(self, event):\n        ''' \u30de\u30a6\u30b9\u304c\u79fb\u52d5\u3057\u305f\u3068\u304d\u306e\u51e6\u7406 '''\n        if event.state &amp; 256:  # \u30de\u30a6\u30b9\u30dc\u30bf\u30f31\u304c\u62bc\u3055\u308c\u3066\u3044\u308b\u3068\u304d\u3060\u3051\uff08\u30c9\u30e9\u30c3\u30b0\u4e2d\u306e\u307f\uff09\n            can_x = self.canvas.canvasx(event.x)\n            can_y = self.canvas.canvasy(event.y)\n            coords = self.canvas.coords(self.id)\n            coords[0] -= self.start_x - can_x\n            coords[1] -= self.start_y - can_y\n            coords[2] -= self.start_x - can_x\n            coords[3] -= self.start_y - can_y\n            self.canvas.coords(self.id, coords)\n            self.start_x = can_x\n            self.start_y = can_y\n            self.x, self.y = coords[0:2]\n            for connection in self.connections:\n                connection.move(self)\n\n    def button_release(self, event):  # pylint: disable=unused-argument\n        ''' \u30de\u30a6\u30b9\u306e\u30dc\u30bf\u30f3\u304c\u96e2\u3055\u308c\u305f\u3068\u304d '''\n        self.start_x = self.start_y = None\n\n    def get_center(self):\n        ''' \u4e2d\u5fc3\u5ea7\u6a19\u3092\u623b\u3057\u307e\u3059 '''\n        return self.x + self.width\/\/2, self.y + self.height\/\/2\n\n    def add_listener(self, connection):\n        ''' \u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u306e\u30ea\u30b9\u30ca\u30fc\u3092\u767b\u9332\u3057\u307e\u3059 '''\n        self.connections.append(connection)\n\n\nclass Connection():\n    ''' Connection class '''\n    def __init__(self, canvas, start_entity, end_entity):\n        self.canvas = canvas\n        self.start_e = start_entity\n        self.end_e = end_entity\n        start_entity.add_listener(self)\n        end_entity.add_listener(self)\n\n        self.id = self.canvas.create_line(self.get_intersection(start_entity),\n                                          self.get_intersection(end_entity))\n\n    def move(self, entity):\n        ''' \u30a8\u30f3\u30c6\u30a3\u30c6\u30a3\u304c\u79fb\u52d5\u3057\u305f\u3068\u304d\u306e\u51e6\u7406\uff08\u30a8\u30f3\u30c6\u30a3\u30c6\u30a3\u304b\u3089\u547c\u3073\u51fa\u3055\u308c\u308b\uff09'''\n        coords = self.canvas.coords(self.id)\n        if entity == self.start_e:\n            coords[0:2] = self.get_intersection(entity)\n            coords[2:4] = self.get_intersection(self.end_e)\n        elif entity == self.end_e:\n            coords[0:2] = self.get_intersection(self.start_e)\n            coords[2:4] = self.get_intersection(entity)\n        self.canvas.coords(self.id, coords)\n\n    def get_intersection(self, entity):\n        ''' \u77e9\u5f62\u3068\u306e\u63a5\u70b9\u3092\u6c42\u3081\u308b '''\n        x, y = entity.get_center()\n        height, width = entity.height \/\/ 2, entity.width \/\/ 2\n        dx, dy = self.end_e.x - self.start_e.x, self.end_e.y - self.start_e.y\n        if entity == self.end_e:\n            dx, dy = -dx, -dy\n        if abs(dy \/ dx) &lt; (height \/ width):  # \u5782\u76f4\u5074\n            x_pos = x + width if dx > 0 else x - width\n            y_pos = y + dy * width \/ abs(dx)\n        else:  # \u6c34\u5e73\u5074\n            x_pos = x + dx * height \/ abs(dy)\n            y_pos = y + height if dy > 0 else y - height\n        return x_pos, y_pos\n\n\nclass Application(tk.Tk):\n    ''' Application class '''\n    def __init__(self):\n        super().__init__()\n        self.title(\"Connecter test 2\")\n        self.geometry(\"640x320\")\n\n        self.canvas = tk.Canvas(self, background=\"white\")\n        self.canvas.pack(fill=tk.BOTH, expand=True)\n\n        entity1 = Entity(self.canvas, 40, 80)\n        entity2 = Entity(self.canvas, 240, 160)\n        Connection(self.canvas, entity1, entity2)\n        entity3 = Entity(self.canvas, 420, 60)\n        Connection(self.canvas, entity2, entity3)\n\n\ndef main():\n    ''' main function '''\n    application = Application()\n    application.mainloop()\n\n\nif __name__ == \"__main__\":\n    main()\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u8aac<\/h2>\n\n\n\n<p>\u3000\u4eca\u56de\u306f\u3001\u4e0a\u8ff0\u3057\u305f\u3088\u3046\u306b\u3001Connection\u30af\u30e9\u30b9\u3092\u5909\u66f4\u3057\u305f\u3060\u3051\u3067\u3059\u3002\u4e3b\u306a\u5909\u66f4\u70b9\u306f\u3001get_intersection()\u30e1\u30bd\u30c3\u30c9\u306e\u8ffd\u52a0\u3068\u3001\u3053\u308c\u307e\u3067\u7bb1\u5074\u306eget_center()\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066\u958b\u59cb\u70b9\u3001\u7d42\u70b9\u3092\u7b97\u51fa\u3057\u3066\u3044\u305f\u3068\u3053\u308d\u3001\u3053\u306e\u8ffd\u52a0\u3057\u305fget_intersection()\u30e1\u30bd\u30c3\u30c9\u306b\u5909\u66f4\u3057\u305f\u3068\u3053\u308d\u3067\u3059\u3002\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u3069\u3053\u306b\u6301\u3064\u306e\u304c\u3044\u3044\u306e\u304b\u306a\u3041\u3001\u3068\u3044\u3046\u306e\u306f\u3061\u3087\u3063\u3068\u60a9\u307f\u307e\u3057\u305f\u304c\u3001\u7bb1\u3068\u7bb1\u306e\u95a2\u4fc2\u306b\u3088\u3063\u3066\u7dda\u3068\u306e\u4ea4\u70b9\u304c\u5909\u308f\u3063\u3066\u304f\u308b\u306e\u3067\u3001Connection\u30af\u30e9\u30b9\u306e\u65b9\u3078\u5b9f\u88c5\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u3000get_intersection()\u30e1\u30bd\u30c3\u30c9\u3067\u306e\u8a08\u7b97\u306e\u30dd\u30a4\u30f3\u30c8\u306f\u3001\u7bb1\u3068\u7bb1\u306e\u4e2d\u5fc3\u9593\u3092\u7d50\u3076\u76f4\u7dda\u306e\u50be\u304d\uff08dx \/ dy\uff09\u3068\u3001\u7bb1\u305d\u306e\u3082\u306e\u306e\u7e26\u6a2a\u6bd4\uff08height \/ width\uff09\u3092\u6bd4\u8f03\u3059\u308b\u3053\u3068\u3067\u3001\u7e26\u306e\u7dda\u3068\u4ea4\u308f\u308b\u306e\u304b\u3001\u6a2a\u306e\u7dda\u3068\u4ea4\u308f\u308b\u306e\u304b\u3001\u3068\u3044\u3046\u306e\u3092\u6c7a\u3081\u3066\u3044\u308b\u3068\u3053\u308d\u3067\u3059\u306d\u3002\u7e26\u306e\u7dda\u3068\u4ea4\u308f\u308b\u3068\u304d\u306fx\u5ea7\u6a19\u306f\u305d\u306e\u307e\u307e\u623b\u3059\u3001\u6a2a\u306e\u7dda\u306e\u5834\u5408\u306fy\u5ea7\u6a19\u3092\u305d\u306e\u307e\u307e\u623b\u3059\u611f\u3058\u306b\u306a\u308a\u307e\u3059\u3002\u5b9f\u969b\u306b\u306f\u4e0a\u4e0b\u3001\u5de6\u53f3\u3068\u4e8c\u7a2e\u985e\u3042\u308b\u306e\u3067\u3001dx\u3001dy\u306e\u6b63\u8ca0\u3092\u6761\u4ef6\u306b\u3057\u3066\u5207\u308a\u66ff\u3048\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002\u5ea7\u6a19\u306e\u4f4d\u7f6e\u304c\u305d\u306e\u307e\u307e\u3058\u3083\u306a\u3044\u65b9\u306b\u3064\u3044\u3066\u306f\u3001\u7e26\u7dda\u307e\u305f\u306f\u6a2a\u7dda\u306e\u9577\u3055\u306b\u76f4\u7dda\u306e\u50be\u304d\u306e\u6bd4\u7387\u3092\u639b\u3051\u308b\u3053\u3068\u3067\u3001\u6c42\u3081\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3000Connection\u30af\u30e9\u30b9\u306emove()\u30e1\u30bd\u30c3\u30c9\u306e\u5909\u66f4\u70b9\u3068\u3057\u3066\u306f\u3001\u3053\u308c\u307e\u3067\u79fb\u52d5\u3057\u305f\u65b9\u306e\u7bb1\u306e\u4e2d\u5fc3\u70b9\u3060\u3051\u5909\u66f4\u3057\u3066\u3044\u308c\u3070\u3088\u304b\u3063\u305f\u3068\u3053\u308d\u3092\u3001\u958b\u59cb\u70b9\u3068\u7d42\u4e86\u70b9\u306e\u4e21\u65b9\u3092\u8a08\u7b97\u3059\u308b\u5fc5\u8981\u304c\u3067\u3066\u304d\u305f\u306e\u3067\u300168\uff5e73\u884c\u76ee\u3067\u5bfe\u5fdc\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u3000\u3042\u3068\u3001\u5b9f\u306f\u7bb1\u304c\u91cd\u306a\u3063\u305f\u6642\u306b\u5909\u306a\u3068\u3053\u308d\u306b\u7dda\u304c\u5f15\u304b\u308c\u3066\u3057\u307e\u3046\u306e\u3068\u3001dx\u304c0\u306b\u306a\u3063\u305f\u3068\u304d\u306b\u300cZeroDivisionError: float division by zero\u300d\u306e\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u307e\u3059\u306e\u3067\u3001\u4f59\u88d5\u306e\u3042\u308b\u65b9\u306f\u4fee\u6b63\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u307e\u3068\u3081<\/h2>\n\n\n\n<p>\u3000\u51fa\u6765\u4e0a\u304c\u3063\u3066\u307f\u308b\u3068\u306a\u3093\u3068\u8a00\u3046\u3053\u3068\u306f\u306a\u3044\u306e\u3067\u3059\u3051\u3069\u3001\u4f5c\u308b\u306e\u306f\u7d50\u69cb\u96e3\u3057\u3044\u3067\u3059\u306d\u3002\u53c2\u8003\u306b\u306a\u308c\u3070\u3046\u308c\u3057\u3044\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3000\u4eca\u65e5\u3082\u898b\u306b\u6765\u3066\u304f\u3060\u3055\u3063\u3066\u3001\u3042\u308a\u304c\u3068\u3046\u3054\u3056\u3044\u307e\u3059\u3002\u77f3\u5ddd\u3055\u3093\u3067\u3059\u3002 \u3000\u524d\u56de\u306e\u5ba3\u8a00\u901a\u308a\u3001\u4eca\u56de\u306f\u9670\u7dda\u51e6\u7406\u3067\u3059\u3002\u7bb1\u306e\u4e2d\u5fc3\u304b\u3089\u4e2d\u5fc3\u3078\u7dda\u3092\u5f15\u304f\u3068\u3053\u308d\u307e\u3067\u306f\u540c\u3058\u306a\u306e\u3067\u3059\u304c\u3001\u7bb1\u306b\u304b\u3076\u308b\u90e8\u5206\u306f\u7dda\u3092\u5f15\u304b\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002\u3053\u306e\u30da\u30fc\u30b8\u3092\u53c2\u8003 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.ishikawasekkei.com\/index.php\/2020\/11\/13\/python-tkinter-gui-programing-canvas-box-connection-part2\/\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;Python tkinter GUI\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 Canvas\u3067\u7bb1\u3092\u3064\u306a\u3050\u7dda\u3092\u63cf\u304f\u305d\u306e\uff12&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,13,2],"tags":[],"class_list":["post-1673","post","type-post","status-publish","format-standard","hentry","category-program","category-tkinter","category-blog"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/posts\/1673","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/comments?post=1673"}],"version-history":[{"count":4,"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/posts\/1673\/revisions"}],"predecessor-version":[{"id":1678,"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/posts\/1673\/revisions\/1678"}],"wp:attachment":[{"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/media?parent=1673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/categories?post=1673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/tags?post=1673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}