{"id":1679,"date":"2020-11-14T00:00:00","date_gmt":"2020-11-13T15:00:00","guid":{"rendered":"https:\/\/www.ishikawasekkei.com\/?p=1679"},"modified":"2020-11-13T22:14:30","modified_gmt":"2020-11-13T13:14:30","slug":"python-tkinter-gui-programing-canvas-box-connection-part3","status":"publish","type":"post","link":"https:\/\/www.ishikawasekkei.com\/index.php\/2020\/11\/14\/python-tkinter-gui-programing-canvas-box-connection-part3\/","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\uff13"},"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\u3055\u3066\u3001\u305d\u306e\uff13\u3067\u3059\u304c\u3001\u305d\u306e\uff11\u3092\u66f8\u3044\u305f\u3068\u304d\u306b\u5ba3\u8a00\u3057\u305f\u901a\u308a\u3001\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u3092\u3084\u308a\u307e\u3059\u3002pylint\u3055\u3093\u304b\u3089\u3001\u3053\u3093\u306a\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u3092\u3057\u305f\u65b9\u304c\u3044\u3044\u3093\u3058\u3083\u306a\u3044\uff1f\u3001\u3068\u3001\u63d0\u6848\u304c\u3042\u308a\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">************* Module connecter_test2\nR0902: 11,0: Entity: Too many instance attributes (9\/7)\nR0913: 13,4: Entity.__init__: Too many arguments (6\/5)\n\n------------------------------------------------------------------\n\nYour code has been rated at 9.75\/10 (previous run: 9.75\/10, +0.00)<\/pre>\n\n\n\n<p>\u3000\u306f\u3044\u3001Entity\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u30a2\u30c8\u30ea\u30d3\u30e5\u30fc\u30c8\u304c\u591a\u3059\u304e\u307e\u3059\u3088\u3001\u521d\u671f\u5316\u306e\u30e1\u30bd\u30c3\u30c9\u306e\u5f15\u6570\u304c\u591a\u3059\u304e\u307e\u3059\u3088\u3001\u3068\u3001\u7533\u3057\u3066\u304a\u308a\u307e\u3059\u3002\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u306e\u30a2\u30c8\u30ea\u30d3\u30e5\u30fc\u30c8\u306f\u4e0a\u96507\u500b\u304c\u9069\u5f53\u3060\u3068pylint\u8a2d\u8a08\u8005\u306f\u8003\u3048\u3066\u3044\u308b\u3088\u3046\u3067\u3059\u306d\u3002\u78ba\u304b\u306b\u3001TM\u3067\u5206\u6790\u3057\u3066\u30a2\u30c8\u30ea\u30d3\u30e5\u30fc\u30c8\u306f\u3060\u3044\u305f\u30445\u500b\u672a\u6e80\u306b\u306a\u3089\u306a\u3044\u3068\u304a\u304b\u3057\u3044\u3082\u3093\u306d\u3002\u305f\u307e\u306b\u30a2\u30c8\u30ea\u30d3\u30e5\u30fc\u30c8\u304c\u591a\u3044\u30e4\u30c4\u3082\u3042\u308a\u307e\u3059\u3051\u3069\u306d\u3047\u30027\u500b\u307e\u3067\u3068\u3044\u3046\u306e\u306f\u3001\u76f4\u89b3\u7684\u306b\u3082\u3044\u3044\u6570\u5b57\u3067\u3059\u306d\u3002\u305d\u3057\u3066\u3001\u5f15\u6570\u306e\u4e0a\u9650\u306f5\u500b\u3001\u307e\u3042\u3001\u59a5\u5f53\u3067\u3057\u3087\u3046\u306d\u3002\u3042\u3093\u307e\u308a\u5f15\u6570\u304c\u591a\u3059\u304e\u308b\u3068\u3001\u9593\u9055\u3044\u306e\u3082\u3068\u3067\u3059\u304b\u3089\u306d\u3002<\/p>\n\n\n\n<p>\u3000\u30a2\u30c8\u30ea\u30d3\u30e5\u30fc\u30c8\u3092\u898b\u3066\u307f\u307e\u3059\u3068\u3001canvas\u3001x\u3001y\u3001width\u3001height\u3001id\u3001start_x\u3001start_y\u3001connections\u3068\u3001\u3001\u3001\u307e\u3042\u307e\u3042\u591a\u3044\u3067\u3059\u306d\u3002Point\u3092\u3064\u304f\u308b\u3068x\u3001y\u306f\u4e00\u3064\u306b\u306a\u308a\u307e\u3059\u306d\u3002\u3069\u3046\u3057\u307e\u3057\u3087\u3046\u304b\u3002\u4eca\u56de\u306f\u7bb1\u3092\u3064\u304f\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3001Rectangle\u3092\u7528\u610f\u3057\u3066\u307f\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002\u305d\u3057\u3066\u3001\u524d\u304b\u3089\u305a\u3063\u3068\u4f7f\u3044\u305f\u304b\u3063\u305f\u3001namedtuple\u3092\u4f7f\u3063\u3066\u307f\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\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<\/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\nfrom collections import namedtuple\n\nRectangle = namedtuple('Rectangle', ['x', 'y', 'width', 'height'],\n                       defaults=(60, 40))\n\n\nclass Entity():\n    ''' Entity class '''\n    def __init__(self, canvas, rectangle):\n        self.canvas = canvas\n        self.rectangle = rectangle\n        self.start_x = self.start_y = None\n        self.connections = []\n\n        self.id = self.canvas.create_rectangle(rectangle.x, rectangle.y,\n                                               rectangle.x + rectangle.width,\n                                               rectangle.y + rectangle.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.rectangle = Rectangle(*coords[0:2], *self.rectangle[2:4])\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        center_x = self.rectangle.x + self.rectangle.width\/\/2\n        center_y = self.rectangle.y + self.rectangle.height\/\/2\n        return center_x, center_y\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\u306e\u63a5\u70b9\u3092\u6c42\u3081\u308b '''\n        x, y = entity.get_center()\n        height, width = entity.rectangle.height\/\/2, entity.rectangle.width\/\/2\n        dx = self.end_e.rectangle.x - self.start_e.rectangle.x\n        dy = self.end_e.rectangle.y - self.start_e.rectangle.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 3\")\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, Rectangle(40, 80))\n        entity2 = Entity(self.canvas, Rectangle(240, 160))\n        Connection(self.canvas, entity1, entity2)\n        entity3 = Entity(self.canvas, Rectangle(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\">\u8aac\u660e<\/h2>\n\n\n\n<p>\u3000\u307e\u305a\u306f\u30012\u884c\u76ee\u3067\u3059\u3002namedtuple\u306fcollections\u30e2\u30b8\u30e5\u30fc\u30eb\u306b\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u306e\u3067\u3001\u307e\u305a\u306fimport\u3057\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002\u3053\u308c\u3067namedtuple\u3092\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u30024\u884c\u76ee\u3067\u3055\u3063\u305d\u304f\u4f7f\u3063\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u3060\u3051\u3067\u3001x\u3001y\u3001width\u3001height\u306e4\u3064\u306e\u30a2\u30c8\u30ea\u30d3\u30e5\u30fc\u30c8\u3092\u3082\u3064Rectangle\u30af\u30e9\u30b9\u3068\u540c\u7b49\u306e\u3082\u306e\u304c\u3067\u304d\u3042\u304c\u308a\u307e\u3057\u305f\u3002defaults\u30aa\u30d7\u30b7\u30e7\u30f3\u306f\u5f8c\u308d\u306e\u65b9\u306e\u521d\u671f\u5024\u306b\u306a\u308a\u307e\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=\"\">Rectangle = namedtuple('Rectangle', ['x', 'y', 'width', 'height'],\n                       defaults=(60, 40))<\/pre>\n\n\n\n<p>\u3000\u4eca\u56de\u306e\u5834\u5408\u306f\u3001width\u3068height\u304c\u7701\u7565\u3055\u308c\u305f\u5834\u5408\u3001\u3053\u308c\u3089\u306b\u5bfe\u3057\u300160\u306840\u304c\u30bb\u30c3\u30c8\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3000\u3053\u308c\u3067\u3001Entity\u30af\u30e9\u30b9\u306e__init__()\u30e1\u30bd\u30c3\u30c9\u306e4\u3064\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u4e00\u3064\u306b\u307e\u3068\u307e\u308a\u307e\u3059\u306e\u3067\u3001\u3053\u308c\u3060\u3051\u3067\u300cToo many arguments\u300d\u306e\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u306f\u5b8c\u4e86\u3067\u3059\u3002\u3042\u3068\u306f\u3001\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u30a2\u30c8\u30ea\u30d3\u30e5\u30fc\u30c8\u3082\u5730\u9053\u306b\u5909\u66f4\u3059\u308c\u3070\u5b8c\u4e86\u3067\u3059\u3002self.x\u3092self.retangle.x\u3001self.y\u3092self.rectangle.y\u3001self.width\u3092self.rectangle.widht\u3001self.height\u3092self.rectangle.height\u3068\u3044\u3063\u305f\u5177\u5408\u306b\u3059\u3079\u3066\u5909\u66f4\u3057\u307e\u3057\u305f\u3002\u3042\u3068\u3001111\u884c\u76ee\u3042\u305f\u308a\u306eEntity\u4f5c\u6210\u6642\u306e\u521d\u671f\u5024\u90e8\u5206\u3092Rectangle(40, 80)\u3068\u3044\u3046\u3075\u3046\u306b\u5909\u66f4\u3057\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<p>\u3000\u5b9f\u884c\u3057\u3066\u3044\u305f\u308f\u304b\u3063\u305f\u3053\u3068\u3067\u3059\u304c\u3001namedtuple\u306f\u540d\u524d\u304c\u30bf\u30d7\u30eb\u3068\u3044\u3046\u3053\u3068\u3060\u3051\u3042\u3063\u3066\u3001\u4e0d\u5909\u306a\u306e\u3067\u3059\u306d\u3002\u7bb1\u3092\u52d5\u304b\u3057\u305f\u3068\u304d\u306b\u3001\u5ea7\u6a19\u3092\u30bb\u30c3\u30c8\u3057\u3066\u3044\u305f\u306e\u3067\u3059\u3051\u3069\u3001self.rectangle.x\u306f\u5024\u3092\u5909\u3048\u3089\u308c\u307e\u305b\u3093\u3067\u3057\u305f\u3002\u307e\u3001\u540d\u524d\u304b\u3089\u60f3\u50cf\u3059\u308c\u3070\u3001\u5f53\u305f\u308a\u524d\u306e\u3053\u3068\u3060\u3063\u305f\u306e\u3067\u3059\u3051\u3069\u306d\u3002\u3068\u3001\u3044\u3046\u3053\u3068\u3067\u3001\u552f\u4e00\u306e\u5ea7\u6a19\u3092\u4fdd\u6301\u3059\u308b\u305f\u3081\u306e\u5834\u6240\u306f\u3001Rectangle\u30af\u30e9\u30b9\u3092\u518d\u4f5c\u6210\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u300242\u884c\u76ee\u3067\u3059\u3002\u300cRectangle(*coords[0:2], *self.rectangle[2:4])\u300d\u3068\u3044\u3046\u3075\u3046\u306b\u521d\u671f\u5316\u3057\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001namedtuple\u3067\u751f\u6210\u3057\u305f\u30bf\u30d7\u30eb\u306f\u3001\u5c5e\u6027\u304c\u540d\u79f0\u3067\u53c2\u7167\u3067\u304d\u308b\u3060\u3051\u3067\u306a\u304f\u3001\u30bf\u30d7\u30eb\u3068\u540c\u3058\u3088\u3046\u306b\u4f4d\u7f6e\u3067\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u308b\u306e\u3067\u3059\u306d\u3002\u3059\u3070\u3089\u3057\u3044\u3067\u3059\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u307e\u3068\u3081<\/h2>\n\n\n\n<p>\u3000\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u30a2\u30c8\u30ea\u30d3\u30e5\u30fc\u30c8\u306e\u6570\u306f7\u500b\u307e\u3067\u306b\u304a\u3055\u3048\u307e\u3057\u3087\u3046\u3002\u5c5e\u6027\u3092\u307e\u3068\u3081\u308b\u306e\u306b\u306f\u3001namedtuple\u304c\u4fbf\u5229\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\u3055\u3066\u3001\u305d\u306e\uff13\u3067\u3059\u304c\u3001\u305d\u306e\uff11\u3092\u66f8\u3044\u305f\u3068\u304d\u306b\u5ba3\u8a00\u3057\u305f\u901a\u308a\u3001\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u3092\u3084\u308a\u307e\u3059\u3002pylint\u3055\u3093\u304b\u3089\u3001\u3053\u3093\u306a\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u3092\u3057\u305f\u65b9\u304c\u3044\u3044\u3093\u3058\u3083\u306a\u3044\uff1f &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.ishikawasekkei.com\/index.php\/2020\/11\/14\/python-tkinter-gui-programing-canvas-box-connection-part3\/\" 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\uff13&#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-1679","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\/1679","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=1679"}],"version-history":[{"count":3,"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/posts\/1679\/revisions"}],"predecessor-version":[{"id":1682,"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/posts\/1679\/revisions\/1682"}],"wp:attachment":[{"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/media?parent=1679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/categories?post=1679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/tags?post=1679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}