{"id":1644,"date":"2021-03-19T17:48:33","date_gmt":"2021-03-19T09:48:33","guid":{"rendered":"http:\/\/cnliutz.uicp.io\/?p=1644"},"modified":"2021-03-19T17:48:33","modified_gmt":"2021-03-19T09:48:33","slug":"tkinter-canvas","status":"publish","type":"post","link":"http:\/\/strawberry.ipyingshe.net:5347\/?p=1644","title":{"rendered":"tkinter  canvas"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>from tkinter import *\r\nimport random\r\nimport tkinter.colorchooser as cc\r\nc = cc.askcolor()\r\n\r\ntk = Tk()\r\ncanvas = Canvas(tk, width=400, height=400,bg=c&#91;1])\r\ncanvas.pack()\r\n\r\ndef random_rectangle(width, height,fill_color):\r\n    x1 = random.randrange(width)\r\n    y1 = random.randrange(height)\r\n    x2 = x1 + random.randrange(width)\r\n    y2 = y1 + random.randrange(height)\r\n    canvas.create_rectangle(x1, y1, x2, y2,fill=fill_color)\r\n\r\n\r\n_color = &#91; 'green', 'red', 'blue', 'orange', 'yellow', 'pink', 'purple', 'violet','magenta','cyan']\r\n\r\n#Draw rectangle with filled color\r\n\r\nfor x in range(0, 500):    \r\n    random_rectangle(400, 400,_color&#91;random.randrange(len(_color))])\r\n\r\n#write text to canvas\r\ncanvas.create_text(130, 120, text='Who rode around on a moose.', fill='red')\r\ncanvas.create_text(150, 100, text='There once was a man from Toulouse',fill=c&#91;1])\r\ncanvas.create_text(150, 150, text='He said, \"It\\'s my curse,', font=('Times', 15))\r\ncanvas.create_text(200, 200, text='But it could be worse,', font=('Helvetica', 20))\r\ncanvas.create_text(220, 250, text='My cousin rides round', font=('Courier', 22))\r\ncanvas.create_text(220, 300, text='on a goose.\"', font=('Courier', 30),fill=\"blue\")\r\n\r\n#Canvas background picture\r\nmy_image = PhotoImage(file='c:\\\\test.gif')\r\ncanvas.create_image(0, 0, anchor=NW, image=myimage)\r\n\r\n##random_rectangle(400, 400, 'green')\r\n##random_rectangle(400, 400, 'red')\r\n##random_rectangle(400, 400, 'blue')\r\n##random_rectangle(400, 400, 'orange')\r\n##random_rectangle(400, 400, 'yellow')\r\n##random_rectangle(400, 400, 'pink')\r\n##random_rectangle(400, 400, 'purple')\r\n##random_rectangle(400, 400, 'violet')\r\n##random_rectangle(400, 400, 'magenta')\r\n##random_rectangle(400, 400, 'cyan')<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-1644","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/strawberry.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/strawberry.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/strawberry.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/strawberry.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/strawberry.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1644"}],"version-history":[{"count":1,"href":"http:\/\/strawberry.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1644\/revisions"}],"predecessor-version":[{"id":1645,"href":"http:\/\/strawberry.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1644\/revisions\/1645"}],"wp:attachment":[{"href":"http:\/\/strawberry.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/strawberry.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1644"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/strawberry.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}