{"id":1159753,"date":"2025-01-13T18:56:34","date_gmt":"2025-01-13T10:56:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1159753.html"},"modified":"2025-01-13T18:56:37","modified_gmt":"2025-01-13T10:56:37","slug":"python3-6%e5%a6%82%e4%bd%95%e8%b0%83%e8%83%8c%e6%99%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1159753.html","title":{"rendered":"python3.6\u5982\u4f55\u8c03\u80cc\u666f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25201409\/f8ab1c3e-c009-4124-b9d1-675358f9d2d9.webp\" alt=\"python3.6\u5982\u4f55\u8c03\u80cc\u666f\" \/><\/p>\n<p><p> <strong>\u5728 Python 3.6 \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u5e93\u6765\u8c03\u6574\u80cc\u666f\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ec tkinter\u3001Pygame \u548c PIL\uff08Pillow\uff09\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>tkinter<\/strong> \u662f\u4e00\u4e2a\u6807\u51c6\u7684 GUI\uff08\u56fe\u5f62\u7528\u6237\u754c\u9762\uff09\u5de5\u5177\u5305\uff0c\u9002\u5408\u521d\u5b66\u8005\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63a7\u4ef6\u548c\u7b80\u5355\u7684\u64cd\u4f5c\uff0c\u9002\u5408\u7528\u6765\u521b\u5efa\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 tkinter \u6765\u8c03\u80cc\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165tkinter<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528 tkinter \u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u786e\u4fdd\u5b83\u5df2\u7ecf\u5b89\u88c5\u3002\u5e78\u8fd0\u7684\u662f\uff0ctkinter \u662f Python \u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u901a\u5e38\u9ed8\u8ba4\u5b89\u88c5\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo apt-get install python3-tk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5bfc\u5165 tkinter\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u4e3b\u7a97\u53e3\u548c\u8bbe\u7f6e\u80cc\u666f\u989c\u8272<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e3b\u7a97\u53e3\u662f\u4f7f\u7528 tkinter \u7684\u7b2c\u4e00\u6b65\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>tk.Tk()<\/code> \u65b9\u6cd5\u6765\u521b\u5efa\u4e00\u4e2a\u7a97\u53e3\u5bf9\u8c61\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 <code>configure<\/code> \u65b9\u6cd5\u6765\u8bbe\u7f6e\u7a97\u53e3\u7684\u80cc\u666f\u989c\u8272\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u6807\u9898<\/strong><\/h2>\n<p>root.title(&quot;\u8c03\u80cc\u666f\u793a\u4f8b&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/strong><\/h2>\n<p>root.geometry(&quot;400x300&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u80cc\u666f\u989c\u8272<\/strong><\/h2>\n<p>root.configure(bg=&#39;lightblue&#39;)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a 400&#215;300 \u7684\u4e3b\u7a97\u53e3\uff0c\u5e76\u5c06\u80cc\u666f\u989c\u8272\u8bbe\u7f6e\u4e3a\u6d45\u84dd\u8272\uff08lightblue\uff09\u3002\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u7a97\u53e3\u7684\u5927\u5c0f\u548c\u80cc\u666f\u989c\u8272\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u56fe\u7247\u4f5c\u4e3a\u80cc\u666f<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u8bbe\u7f6e\u7eaf\u8272\u80cc\u666f\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u56fe\u7247\u4f5c\u4e3a\u80cc\u666f\u3002\u53ef\u4ee5\u4f7f\u7528 <code>PhotoImage<\/code> \u6216\u8005 <code>PIL<\/code> \u5e93\u6765\u52a0\u8f7d\u56fe\u7247\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u80cc\u666f\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528 PhotoImage<\/h4>\n<\/p>\n<p><p><code>PhotoImage<\/code> \u662f tkinter \u7684\u5185\u7f6e\u7c7b\uff0c\u7528\u4e8e\u5904\u7406 GIF \u548c PGM\/PPM \u683c\u5f0f\u7684\u56fe\u7247\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/strong><\/h2>\n<p>root.geometry(&quot;400x300&quot;)<\/p>\n<h2><strong>\u52a0\u8f7d\u56fe\u7247<\/strong><\/h2>\n<p>background_image = tk.PhotoImage(file=&quot;background.gif&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6807\u7b7e\u5e76\u8bbe\u7f6e\u80cc\u666f\u56fe\u7247<\/strong><\/h2>\n<p>background_label = tk.Label(root, image=background_image)<\/p>\n<p>background_label.place(relwidth=1, relheight=1)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u52a0\u8f7d\u4e86\u4e00\u5f20\u540d\u4e3a <code>background.gif<\/code> \u7684\u56fe\u7247\uff0c\u5e76\u5c06\u5176\u8bbe\u7f6e\u4e3a\u4e3b\u7a97\u53e3\u7684\u80cc\u666f\u3002\u8bf7\u6ce8\u610f\uff0cPhotoImage \u4ec5\u652f\u6301 GIF \u548c PGM\/PPM \u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528 PIL\uff08Pillow\uff09<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u652f\u6301\u66f4\u591a\u7684\u56fe\u7247\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528 PIL\uff08Pillow\uff09\u5e93\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5 Pillow\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pillow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u52a0\u8f7d\u548c\u8bbe\u7f6e\u80cc\u666f\u56fe\u7247\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from PIL import Image, ImageTk<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/strong><\/h2>\n<p>root.geometry(&quot;400x300&quot;)<\/p>\n<h2><strong>\u52a0\u8f7d\u56fe\u7247<\/strong><\/h2>\n<p>image = Image.open(&quot;background.jpg&quot;)<\/p>\n<p>background_image = ImageTk.PhotoImage(image)<\/p>\n<h2><strong>\u521b\u5efa\u6807\u7b7e\u5e76\u8bbe\u7f6e\u80cc\u666f\u56fe\u7247<\/strong><\/h2>\n<p>background_label = tk.Label(root, image=background_image)<\/p>\n<p>background_label.place(relwidth=1, relheight=1)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 Pillow \u52a0\u8f7d\u4e86\u4e00\u5f20 JPEG \u683c\u5f0f\u7684\u56fe\u7247\uff0c\u5e76\u5c06\u5176\u8bbe\u7f6e\u4e3a\u4e3b\u7a97\u53e3\u7684\u80cc\u666f\u3002Pillow \u652f\u6301\u66f4\u591a\u7684\u56fe\u7247\u683c\u5f0f\uff0c\u5982 JPEG\u3001PNG\u3001BMP \u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Canvas\u7ec4\u4ef6\u8c03\u6574\u80cc\u666f<\/h3>\n<\/p>\n<p><p>tkinter \u7684 <code>Canvas<\/code> \u7ec4\u4ef6\u53ef\u4ee5\u7ed8\u5236\u56fe\u5f62\u548c\u56fe\u50cf\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u80cc\u666f\u8c03\u6574\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 <code>Canvas<\/code> \u7ec4\u4ef6\u7ed8\u5236\u80cc\u666f\u56fe\u7247\u548c\u56fe\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>from PIL import Image, ImageTk<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/strong><\/h2>\n<p>root.geometry(&quot;400x300&quot;)<\/p>\n<h2><strong>\u521b\u5efaCanvas\u7ec4\u4ef6<\/strong><\/h2>\n<p>canvas = tk.Canvas(root, width=400, height=300)<\/p>\n<p>canvas.pack(fill=&quot;both&quot;, expand=True)<\/p>\n<h2><strong>\u52a0\u8f7d\u56fe\u7247<\/strong><\/h2>\n<p>image = Image.open(&quot;background.jpg&quot;)<\/p>\n<p>background_image = ImageTk.PhotoImage(image)<\/p>\n<h2><strong>\u5728Canvas\u4e0a\u7ed8\u5236\u80cc\u666f\u56fe\u7247<\/strong><\/h2>\n<p>canvas.create_image(0, 0, anchor=&quot;nw&quot;, image=background_image)<\/p>\n<h2><strong>\u5728Canvas\u4e0a\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>canvas.create_rectangle(50, 50, 150, 100, fill=&quot;blue&quot;)<\/p>\n<p>canvas.create_oval(200, 50, 300, 100, fill=&quot;red&quot;)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a <code>Canvas<\/code> \u7ec4\u4ef6\uff0c\u5e76\u5728\u5176\u4e0a\u7ed8\u5236\u4e86\u80cc\u666f\u56fe\u7247\u548c\u4e24\u4e2a\u56fe\u5f62\uff08\u77e9\u5f62\u548c\u692d\u5706\uff09\u3002\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5728 <code>Canvas<\/code> \u4e0a\u7ed8\u5236\u66f4\u591a\u7684\u56fe\u5f62\u548c\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u8c03\u6574\u80cc\u666f\u900f\u660e\u5ea6<\/h3>\n<\/p>\n<p><p>tkinter \u4e0d\u652f\u6301\u76f4\u63a5\u8bbe\u7f6e\u7a97\u53e3\u7684\u900f\u660e\u5ea6\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528 <code>wm_attributes<\/code> \u65b9\u6cd5\u8c03\u6574\u6574\u4e2a\u7a97\u53e3\u7684\u900f\u660e\u5ea6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/strong><\/h2>\n<p>root.geometry(&quot;400x300&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u900f\u660e\u5ea6\uff080.0\u52301.0\uff0c0.0\u4e3a\u5b8c\u5168\u900f\u660e\uff09<\/strong><\/h2>\n<p>root.wm_attributes(&#39;-alpha&#39;, 0.8)<\/p>\n<h2><strong>\u8bbe\u7f6e\u80cc\u666f\u989c\u8272<\/strong><\/h2>\n<p>root.configure(bg=&#39;lightblue&#39;)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u7a97\u53e3\u7684\u900f\u660e\u5ea6\u8bbe\u7f6e\u4e3a 0.8\uff0880% \u4e0d\u900f\u660e\uff09\u3002\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u900f\u660e\u5ea6\u7684\u503c\uff08\u8303\u56f4\u4ece 0.0 \u5230 1.0\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u54cd\u5e94\u4e8b\u4ef6\u8c03\u6574\u80cc\u666f<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u7ed1\u5b9a\u4e8b\u4ef6\u6765\u52a8\u6001\u8c03\u6574\u80cc\u666f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u9f20\u6807\u70b9\u51fb\u65f6\u66f4\u6539\u80cc\u666f\u989c\u8272\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def change_background(event):<\/p>\n<p>    # \u83b7\u53d6\u968f\u673a\u989c\u8272<\/p>\n<p>    colors = [&#39;lightblue&#39;, &#39;lightgreen&#39;, &#39;lightyellow&#39;, &#39;lightpink&#39;]<\/p>\n<p>    new_color = colors[event.x % len(colors)]<\/p>\n<p>    root.configure(bg=new_color)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<h2><strong>\u8bbe\u7f6e\u7a97\u53e3\u5927\u5c0f<\/strong><\/h2>\n<p>root.geometry(&quot;400x300&quot;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u521d\u59cb\u80cc\u666f\u989c\u8272<\/strong><\/h2>\n<p>root.configure(bg=&#39;lightblue&#39;)<\/p>\n<h2><strong>\u7ed1\u5b9a\u9f20\u6807\u70b9\u51fb\u4e8b\u4ef6<\/strong><\/h2>\n<p>root.bind(&#39;&lt;Button-1&gt;&#39;, change_background)<\/p>\n<h2><strong>\u8fd0\u884c\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u7ed1\u5b9a\u4e86\u9f20\u6807\u5de6\u952e\u70b9\u51fb\u4e8b\u4ef6\uff08<code>&lt;Button-1&gt;<\/code>\uff09\uff0c\u5e76\u5728\u4e8b\u4ef6\u89e6\u53d1\u65f6\u66f4\u6539\u80cc\u666f\u989c\u8272\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728 Python 3.6 \u4e2d\u4f7f\u7528 tkinter \u8c03\u6574\u80cc\u666f\u3002\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\u8bbe\u7f6e\u7eaf\u8272\u80cc\u666f\u3001\u4f7f\u7528\u56fe\u7247\u4f5c\u4e3a\u80cc\u666f\u3001\u4f7f\u7528 <code>Canvas<\/code> \u7ec4\u4ef6\u7ed8\u5236\u80cc\u666f\u3001\u8c03\u6574\u7a97\u53e3\u900f\u660e\u5ea6\u4ee5\u53ca\u54cd\u5e94\u4e8b\u4ef6\u52a8\u6001\u8c03\u6574\u80cc\u666f\u3002tkinter \u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684 GUI \u5de5\u5177\u5305\uff0c\u9002\u5408\u7528\u4e8e\u521b\u5efa\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u57fa\u672c\u64cd\u4f5c\uff0c\u53ef\u4ee5\u7075\u6d3b\u8c03\u6574\u5e94\u7528\u7a0b\u5e8f\u7684\u80cc\u666f\uff0c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python 3.6\u4e2d\u8bbe\u7f6e\u7a97\u53e3\u80cc\u666f\u989c\u8272\uff1f<\/strong><br \/>\u5728Python 3.6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\u6765\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0c\u5e76\u8bbe\u7f6e\u7a97\u53e3\u7684\u80cc\u666f\u989c\u8272\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Tkinter\u3002\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u53ef\u4ee5\u8f7b\u677e\u66f4\u6539\u7a97\u53e3\u7684\u80cc\u666f\u989c\u8272\uff1a  <\/p>\n<pre><code class=\"language-python\">import tkinter as tk\n\nroot = tk.Tk()\nroot.configure(bg=&#39;blue&#39;)  # \u5c06\u80cc\u666f\u989c\u8272\u8bbe\u7f6e\u4e3a\u84dd\u8272\nroot.mainloop()\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u5c06\u7a97\u53e3\u7684\u80cc\u666f\u989c\u8272\u8bbe\u7f6e\u4e3a\u84dd\u8272\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5c06\u989c\u8272\u66ff\u6362\u4e3a\u5176\u4ed6\u989c\u8272\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728Python 3.6\u4e2d\u4f7f\u7528\u56fe\u50cf\u4f5c\u4e3a\u80cc\u666f\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u5728Python 3.6\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528PIL\u5e93\uff08Pillow\uff09\u6765\u52a0\u8f7d\u56fe\u50cf\uff0c\u5e76\u5c06\u5176\u8bbe\u7f6e\u4e3aTkinter\u7a97\u53e3\u7684\u80cc\u666f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">from tkinter import *\nfrom PIL import Image, ImageTk\n\nroot = Tk()\nimage = Image.open(&#39;background.jpg&#39;)  # \u66ff\u6362\u4e3a\u4f60\u7684\u56fe\u7247\u8def\u5f84\nbg_image = ImageTk.PhotoImage(image)\nlabel = Label(root, image=bg_image)\nlabel.place(x=0, y=0, relwidth=1, relheight=1)\nroot.mainloop()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u52a0\u8f7d\u540d\u4e3a<code>background.jpg<\/code>\u7684\u56fe\u50cf\u5e76\u5c06\u5176\u8bbe\u7f6e\u4e3a\u7a97\u53e3\u7684\u80cc\u666f\u3002<\/p>\n<p><strong>\u5728Python 3.6\u4e2d\u5982\u4f55\u52a8\u6001\u6539\u53d8\u80cc\u666f\uff1f<\/strong><br \/>\u52a8\u6001\u6539\u53d8\u80cc\u666f\u989c\u8272\u6216\u56fe\u50cf\u4e5f\u5f88\u7b80\u5355\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u6309\u94ae\u6216\u5176\u4ed6\u4e8b\u4ef6\u6765\u89e6\u53d1\u80cc\u666f\u7684\u53d8\u5316\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u901a\u8fc7\u6309\u94ae\u6539\u53d8\u80cc\u666f\u989c\u8272\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import tkinter as tk\n\ndef change_background():\n    root.configure(bg=&#39;green&#39;)  # \u70b9\u51fb\u6309\u94ae\u540e\u5c06\u80cc\u666f\u989c\u8272\u66f4\u6539\u4e3a\u7eff\u8272\n\nroot = tk.Tk()\nbutton = tk.Button(root, text=&#39;\u66f4\u6539\u80cc\u666f&#39;, command=change_background)\nbutton.pack()\nroot.mainloop()\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u70b9\u51fb\u6309\u94ae\u65f6\uff0c\u7a97\u53e3\u7684\u80cc\u666f\u989c\u8272\u5c06\u53d8\u4e3a\u7eff\u8272\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u66f4\u6539\u989c\u8272\u6216\u6dfb\u52a0\u66f4\u591a\u7684\u80cc\u666f\u9009\u9879\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728 Python 3.6 \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u5e93\u6765\u8c03\u6574\u80cc\u666f\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ec tkinter\u3001Pygame \u548c PI [&hellip;]","protected":false},"author":3,"featured_media":1159758,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159753"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1159753"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159753\/revisions"}],"predecessor-version":[{"id":1159760,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159753\/revisions\/1159760"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1159758"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1159753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1159753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1159753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}