{"id":1665,"date":"2020-11-12T00:00:00","date_gmt":"2020-11-11T15:00:00","guid":{"rendered":"https:\/\/www.ishikawasekkei.com\/?p=1665"},"modified":"2020-11-11T09:03:51","modified_gmt":"2020-11-11T00:03:51","slug":"python-tkinter-gui-programing-canvas-box-connection","status":"publish","type":"post","link":"https:\/\/www.ishikawasekkei.com\/index.php\/2020\/11\/12\/python-tkinter-gui-programing-canvas-box-connection\/","title":{"rendered":"Python tkinter GUI\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0 Canvas\u3067\u7bb1\u3092\u3064\u306a\u3050\u7dda\u3092\u63cf\u304f"},"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>\u3000Canvas\u3067\u7bb1\u3092\u63cf\u3044\u3066\u3001\u52d5\u304b\u3059\u3053\u3068\u3092\u3084\u3063\u3066\u304d\u307e\u3057\u305f\u3051\u3069\u3001\u7bb1\u3068\u7bb1\u3092\u3064\u306a\u3050\u305f\u3081\u306e\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u306e\u3088\u3046\u306a\u7dda\u3092\u63cf\u304f\u3053\u3068\u3092\u3084\u3063\u3066\u307f\u307e\u3057\u305f\u3002\u307e\u3042\u307e\u3042\u3046\u307e\u3044\u3053\u3068\u3067\u304d\u305f\u3088\u3046\u306b\u601d\u3044\u307e\u3059\u306e\u3067\u3001\u7d39\u4ecb\u3055\u305b\u3066\u3044\u305f\u3060\u304d\u307e\u3059\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\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\u3002\u7bb1\u3092\u63cf\u3044\u3066\u3001\u307e\u305a\u306f\u4e2d\u5fc3\u540c\u58eb\u3067\u76f4\u7dda\u3092\u7d50\u3076\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002\u7bb1\u3092\u52d5\u304b\u3059\u3068\u7dda\u304c\u3064\u3044\u3066\u304f\u308b\u3001\u3068\u3044\u3046\u306e\u304c\u76ee\u6a19\u3067\u3059\u3002\u96a0\u7dda\u51e6\u7406\u306f\u3001\u6b21\u56de\u306e\u30c6\u30fc\u30de\u3068\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"642\" height=\"366\" src=\"https:\/\/www.ishikawasekkei.com\/wp-content\/uploads\/2020\/11\/image-8.png\" alt=\"\" class=\"wp-image-1667\" srcset=\"https:\/\/www.ishikawasekkei.com\/wp-content\/uploads\/2020\/11\/image-8.png 642w, https:\/\/www.ishikawasekkei.com\/wp-content\/uploads\/2020\/11\/image-8-300x171.png 300w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><\/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\u3068\u304a\u308a\u3067\u3059\u3002\u5148\u65e5\u304a\u8a71\u3057\u305f\u3068\u304a\u308a\u3001pylint\u3067\u30c1\u30a7\u30c3\u30af\u3057\u3066\u3044\u308b\u306e\u3067\u3059\u3051\u3069\u3001\u4e00\u90e8\u3001\u30ea\u30d5\u30a1\u30af\u30bf\u30ea\u30f3\u30b0\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\u304c\u6b8b\u3063\u3066\u3044\u307e\u3059\u3002\u305d\u308c\u306f\u6b21\u3005\u56de\u4ee5\u964d\u306e\u30c6\u30fc\u30de\u3067\u304a\u8a71\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\uff08R0902: too-many-instance-attributes\u3068R0913: too-many-arguments\uff09<\/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 self.start_x is None:\n            return\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.make_figure()\n\n    def make_figure(self):\n        ''' \u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u3092\u63cf\u304d\u307e\u3059 '''\n        start_point = self.start_e.get_center()\n        end_point = self.end_e.get_center()\n        self.id = self.canvas.create_line(start_point, end_point)\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] = entity.get_center()\n        elif entity == self.end_e:\n            coords[2:4] = entity.get_center()\n        self.canvas.coords(self.id, coords)\n\n\nclass Application(tk.Tk):\n    ''' Application class '''\n    def __init__(self):\n        super().__init__()\n        self.title(\"Connecter test\")\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\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u8aac\u660e<\/h2>\n\n\n\n<p>\u3000\u7bb1\u3092\u52d5\u304b\u3059\u90e8\u5206\u306b\u3064\u3044\u3066\u306f\u3001\u3053\u308c\u307e\u3067\u306b\u66f8\u3044\u3066\u304d\u3066\u3044\u308b\u306e\u3067\u7701\u7565\u3057\u307e\u3059\u3002\u4eca\u56de\u306e\u4e00\u3064\u76ee\u306e\u30dd\u30a4\u30f3\u30c8\u306f\u3001Entity\u30af\u30e9\u30b9\u3067\u3059\u300210\u884c\u76ee\u3067\u63a5\u7d9a\u3055\u308c\u305f\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u3092\u53d7\u3051\u53d6\u308b\u305f\u3081\u306e\u30ea\u30b9\u30c8\u3092\u4f5c\u6210\u3057\u3066\u3044\u307e\u3059\u300250\uff5e52\u884c\u3067\u5b9a\u7fa9\u3055\u308c\u305fadd_listener()\u30e1\u30bd\u30c3\u30c9\u3067\u3001\u30ea\u30b9\u30ca\u30fc\u3068\u3057\u3066\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u3092\u767b\u9332\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002Entity\u30af\u30e9\u30b9\u306emove()\u30e1\u30bd\u30c3\u30c9\u306e39\uff5e40\u884c\u3067\u3001\u767b\u9332\u3055\u308c\u305f\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u306emove()\u30e1\u30bd\u30c3\u30c9\u304c\u547c\u3073\u51fa\u3055\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u30002\u3064\u76ee\u306e\u30dd\u30a4\u30f3\u30c8\u306f\u3001Connection\u30af\u30e9\u30b9\u3067\u3059\u300255\uff5e79\u884c\u76ee\u3067\u3059\u3002\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u304c\u767b\u9332\u3055\u308c\u305f\u3068\u304d\u306b\u3001\u7bb1\u306e\u60c5\u5831\u3092\u53d6\u5f97\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059\u3002\u6307\u5b9a\u3055\u308c\u305f\u7bb1\u306eadd_listener()\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3063\u3066\u81ea\u5206\u81ea\u8eab\u3092\u767b\u9332\u3057\u307e\u3059\u3002\u767b\u9332\u3059\u308b\u3053\u3068\u3067\u3001\u7bb1\u304c\u79fb\u52d5\u3057\u305f\u3068\u304d\u306b\u3001\u3053\u3061\u3089\u306emove()\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u3066\u3082\u3089\u3063\u3066\u3001\u7bb1\u306e\u52d5\u4f5c\u306b\u3042\u308f\u305b\u3066\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u304c\u3064\u3044\u3066\u3044\u304f\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u307e\u3068\u3081<\/h2>\n\n\n\n<p>\u3000\u7bb1\u306b\u9023\u52d5\u3059\u308b\u30b3\u30cd\u30af\u30bf\u30fc\u304c\u4f5c\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002\u6b21\u56de\u306f\u63a5\u7d9a\u90e8\u5206\u306e\u96a0\u7dda\u51e6\u7406\u306b\u3064\u3044\u3066\u66f8\u304d\u305f\u3044\u3068\u601d\u3044\u307e\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 \u3000Canvas\u3067\u7bb1\u3092\u63cf\u3044\u3066\u3001\u52d5\u304b\u3059\u3053\u3068\u3092\u3084\u3063\u3066\u304d\u307e\u3057\u305f\u3051\u3069\u3001\u7bb1\u3068\u7bb1\u3092\u3064\u306a\u3050\u305f\u3081\u306e\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u306e\u3088\u3046\u306a\u7dda\u3092\u63cf\u304f\u3053\u3068\u3092\u3084\u3063\u3066\u307f\u307e\u3057\u305f\u3002\u307e\u3042\u307e\u3042\u3046\u307e\u3044\u3053\u3068\u3067\u304d\u305f &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.ishikawasekkei.com\/index.php\/2020\/11\/12\/python-tkinter-gui-programing-canvas-box-connection\/\" 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&#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-1665","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\/1665","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=1665"}],"version-history":[{"count":6,"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/posts\/1665\/revisions"}],"predecessor-version":[{"id":1672,"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/posts\/1665\/revisions\/1672"}],"wp:attachment":[{"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/media?parent=1665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/categories?post=1665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ishikawasekkei.com\/index.php\/wp-json\/wp\/v2\/tags?post=1665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}