{"id":1117697,"date":"2025-01-08T18:28:25","date_gmt":"2025-01-08T10:28:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1117697.html"},"modified":"2025-01-08T18:28:28","modified_gmt":"2025-01-08T10:28:28","slug":"%e5%a6%82%e4%bd%95%e5%b0%86python%e8%ae%be%e7%bd%ae%e4%b8%89%e4%b8%aa%e7%aa%97%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1117697.html","title":{"rendered":"\u5982\u4f55\u5c06Python\u8bbe\u7f6e\u4e09\u4e2a\u7a97\u53e3"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081514\/adc79563-b6e1-4be9-8581-eca3f6838adb.webp\" alt=\"\u5982\u4f55\u5c06Python\u8bbe\u7f6e\u4e09\u4e2a\u7a97\u53e3\" \/><\/p>\n<p><p> <strong>\u8981\u5c06Python\u8bbe\u7f6e\u4e3a\u4e09\u4e2a\u7a97\u53e3\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u5e93\u548c\u5de5\u5177\uff0c\u6700\u5e38\u89c1\u7684\u6709Tkinter\u3001PyQt\u548cPygame\u3002<\/strong> \u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e09\u79cd\u65b9\u6cd5\u5206\u522b\u521b\u5efa\u5e76\u8bbe\u7f6e\u4e09\u4e2a\u7a97\u53e3\u3002\u6211\u4eec\u4f1a\u4e3a\u6bcf\u4e2a\u65b9\u6cd5\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u548c\u8be6\u7ec6\u7684\u89e3\u91ca\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u4f7f\u7528Tkinter<\/h2>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\uff08\u56fe\u5f62\u7528\u6237\u754c\u9762\uff09\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u521b\u5efa\u684c\u9762\u5e94\u7528\u7a0b\u5e8f\u3002\u5b83\u975e\u5e38\u7b80\u5355\u6613\u7528\uff0c\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>1. \u521b\u5efa\u4e09\u4e2a\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165Tkinter\u6a21\u5757\uff0c\u7136\u540e\u521b\u5efa\u4e09\u4e2a\u72ec\u7acb\u7684\u7a97\u53e3\u3002\u4ee5\u4e0b\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>root1 = tk.Tk()<\/p>\n<p>root1.title(&quot;Window 1&quot;)<\/p>\n<p>root1.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2a\u7a97\u53e3<\/strong><\/h2>\n<p>root2 = tk.Toplevel(root1)<\/p>\n<p>root2.title(&quot;Window 2&quot;)<\/p>\n<p>root2.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e09\u4e2a\u7a97\u53e3<\/strong><\/h2>\n<p>root3 = tk.Toplevel(root1)<\/p>\n<p>root3.title(&quot;Window 3&quot;)<\/p>\n<p>root3.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u542f\u52a8Tkinter\u4e8b\u4ef6\u5faa\u73af<\/strong><\/h2>\n<p>root1.m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u8be6\u7ec6\u8bf4\u660e<\/h3>\n<\/p>\n<ul>\n<li><strong>tk.Tk()<\/strong>: \u521b\u5efa\u4e3b\u7a97\u53e3\u3002<\/li>\n<li><strong>tk.Toplevel()<\/strong>: \u521b\u5efa\u5b50\u7a97\u53e3\u3002\u8fd9\u4e9b\u7a97\u53e3\u662f\u72ec\u7acb\u7684\uff0c\u4f46\u4f1a\u4e0e\u4e3b\u7a97\u53e3\u4e00\u8d77\u8fd0\u884c\u3002<\/li>\n<li><strong>title()<\/strong>: \u8bbe\u7f6e\u7a97\u53e3\u7684\u6807\u9898\u3002<\/li>\n<li><strong>geometry()<\/strong>: \u8bbe\u7f6e\u7a97\u53e3\u7684\u5927\u5c0f\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u548c\u7ba1\u7406\u591a\u4e2a\u7a97\u53e3\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e8c\u3001\u4f7f\u7528PyQt<\/h2>\n<\/p>\n<p><p>PyQt\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684Python GUI\u5e93\uff0c\u5b83\u57fa\u4e8eQt\u5e93\uff0c\u529f\u80fd\u5f3a\u5927\u4e14\u7075\u6d3b\uff0c\u9002\u7528\u4e8e\u9700\u8981\u590d\u6742\u7528\u6237\u754c\u9762\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5PyQt<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86PyQt\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyQt5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u521b\u5efa\u4e09\u4e2a\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528PyQt\u521b\u5efa\u4e09\u4e2a\u7a97\u53e3\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>from PyQt5.QtWidgets import QApplication, QWidget<\/p>\n<h2><strong>\u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61<\/strong><\/h2>\n<p>app = QApplication(sys.argv)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e00\u4e2a\u7a97\u53e3<\/strong><\/h2>\n<p>window1 = QWidget()<\/p>\n<p>window1.setWindowTitle(&quot;Window 1&quot;)<\/p>\n<p>window1.setGeometry(100, 100, 300, 200)<\/p>\n<p>window1.show()<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2a\u7a97\u53e3<\/strong><\/h2>\n<p>window2 = QWidget()<\/p>\n<p>window2.setWindowTitle(&quot;Window 2&quot;)<\/p>\n<p>window2.setGeometry(450, 100, 300, 200)<\/p>\n<p>window2.show()<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e09\u4e2a\u7a97\u53e3<\/strong><\/h2>\n<p>window3 = QWidget()<\/p>\n<p>window3.setWindowTitle(&quot;Window 3&quot;)<\/p>\n<p>window3.setGeometry(800, 100, 300, 200)<\/p>\n<p>window3.show()<\/p>\n<h2><strong>\u8fdb\u5165\u5e94\u7528\u7a0b\u5e8f\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>sys.exit(app.exec_())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u8be6\u7ec6\u8bf4\u660e<\/h3>\n<\/p>\n<ul>\n<li><strong>QApplication<\/strong>: \u521b\u5efa\u5e94\u7528\u7a0b\u5e8f\u5bf9\u8c61\uff0c\u8d1f\u8d23\u7ba1\u7406\u5e94\u7528\u7a0b\u5e8f\u7684\u63a7\u5236\u6d41\u548c\u4e3b\u8981\u8bbe\u7f6e\u3002<\/li>\n<li><strong>QWidget<\/strong>: \u521b\u5efa\u7a97\u53e3\u5bf9\u8c61\u3002<\/li>\n<li><strong>setWindowTitle()<\/strong>: \u8bbe\u7f6e\u7a97\u53e3\u7684\u6807\u9898\u3002<\/li>\n<li><strong>setGeometry()<\/strong>: \u8bbe\u7f6e\u7a97\u53e3\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\u3002<\/li>\n<li><strong>show()<\/strong>: \u663e\u793a\u7a97\u53e3\u3002<\/li>\n<\/ul>\n<p><p>PyQt\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u590d\u6742\u7684\u7528\u6237\u754c\u9762\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e09\u3001\u4f7f\u7528Pygame<\/h2>\n<\/p>\n<p><p>Pygame\u662f\u4e00\u4e2a\u8de8\u5e73\u53f0\u7684Python\u6a21\u5757\uff0c\u7528\u4e8e\u5f00\u53d1\u89c6\u9891\u6e38\u620f\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u7b80\u5355\u7684\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5Pygame<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Pygame\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pygame<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u521b\u5efa\u4e09\u4e2a\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528Pygame\u521b\u5efa\u4e09\u4e2a\u7a97\u53e3\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pygame<\/p>\n<p>import os<\/p>\n<h2><strong>\u521d\u59cb\u5316Pygame<\/strong><\/h2>\n<p>pygame.init()<\/p>\n<h2><strong>\u521b\u5efa\u7a97\u53e3<\/strong><\/h2>\n<p>def create_window(title, position):<\/p>\n<p>    os.environ[&#39;SDL_VIDEO_WINDOW_POS&#39;] = position<\/p>\n<p>    window = pygame.display.set_mode((300, 200))<\/p>\n<p>    pygame.display.set_caption(title)<\/p>\n<p>    return window<\/p>\n<h2><strong>\u521b\u5efa\u4e09\u4e2a\u7a97\u53e3<\/strong><\/h2>\n<p>window1 = create_window(&quot;Window 1&quot;, &quot;100,100&quot;)<\/p>\n<p>window2 = create_window(&quot;Window 2&quot;, &quot;450,100&quot;)<\/p>\n<p>window3 = create_window(&quot;Window 3&quot;, &quot;800,100&quot;)<\/p>\n<h2><strong>\u4e3b\u5faa\u73af<\/strong><\/h2>\n<p>running = True<\/p>\n<p>while running:<\/p>\n<p>    for event in pygame.event.get():<\/p>\n<p>        if event.type == pygame.QUIT:<\/p>\n<p>            running = False<\/p>\n<h2><strong>\u9000\u51faPygame<\/strong><\/h2>\n<p>pygame.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u8be6\u7ec6\u8bf4\u660e<\/h3>\n<\/p>\n<ul>\n<li><strong>pygame.init()<\/strong>: \u521d\u59cb\u5316Pygame\u6a21\u5757\u3002<\/li>\n<li><strong>pygame.display.set_mode()<\/strong>: \u8bbe\u7f6e\u7a97\u53e3\u7684\u5927\u5c0f\u3002<\/li>\n<li><strong>pygame.display.set_caption()<\/strong>: \u8bbe\u7f6e\u7a97\u53e3\u7684\u6807\u9898\u3002<\/li>\n<li><strong>os.environ[&#39;SDL_VIDEO_WINDOW_POS&#39;]<\/strong>: \u8bbe\u7f6e\u7a97\u53e3\u7684\u4f4d\u7f6e\u3002<\/li>\n<li><strong>pygame.event.get()<\/strong>: \u83b7\u53d6\u4e8b\u4ef6\u961f\u5217\u4e2d\u7684\u6240\u6709\u4e8b\u4ef6\u3002<\/li>\n<li><strong>pygame.quit()<\/strong>: \u9000\u51faPygame\u3002<\/li>\n<\/ul>\n<p><p>Pygame\u901a\u5e38\u7528\u4e8e\u6e38\u620f\u5f00\u53d1\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u7b80\u5355\u7684GUI\u7a97\u53e3\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4e09\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u8bbe\u7f6e\u548c\u7ba1\u7406\u591a\u4e2a\u7a97\u53e3\u3002\u6839\u636e\u4f60\u7684\u9700\u6c42\u548c\u719f\u6089\u7a0b\u5ea6\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u8fdb\u884c\u5f00\u53d1\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684Tkinter\uff0c\u529f\u80fd\u5f3a\u5927\u7684PyQt\uff0c\u8fd8\u662f\u8de8\u5e73\u53f0\u7684Pygame\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u591a\u4e2a\u7a97\u53e3\u7684\u7a0b\u5e8f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\u6765\u521b\u5efa\u591a\u4e2a\u7a97\u53e3\u3002\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u591a\u4e2aToplevel\u7a97\u53e3\u6765\u5b9e\u73b0\u3002\u6bcf\u4e2aToplevel\u7a97\u53e3\u90fd\u662f\u72ec\u7acb\u7684\uff0c\u53ef\u4ee5\u5728\u5176\u4e2d\u6dfb\u52a0\u4e0d\u540c\u7684\u63a7\u4ef6\u548c\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import tkinter as tk\n\ndef create_new_window():\n    new_window = tk.Toplevel(root)\n    new_window.title(&quot;\u65b0\u7a97\u53e3&quot;)\n    new_label = tk.Label(new_window, text=&quot;\u8fd9\u662f\u4e00\u4e2a\u65b0\u7a97\u53e3&quot;)\n    new_label.pack()\n\nroot = tk.Tk()\nroot.title(&quot;\u4e3b\u7a97\u53e3&quot;)\nopen_window_button = tk.Button(root, text=&quot;\u6253\u5f00\u65b0\u7a97\u53e3&quot;, command=create_new_window)\nopen_window_button.pack()\nroot.mainloop()\n<\/code><\/pre>\n<p>\u8fd0\u884c\u6b64\u4ee3\u7801\u540e\uff0c\u53ef\u4ee5\u70b9\u51fb\u6309\u94ae\u521b\u5efa\u65b0\u7684\u7a97\u53e3\u3002<\/p>\n<p><strong>\u5728\u591a\u4e2a\u7a97\u53e3\u4e2d\u5982\u4f55\u5171\u4eab\u6570\u636e\uff1f<\/strong><br \/>\u8981\u5728\u591a\u4e2a\u7a97\u53e3\u4e4b\u95f4\u5171\u4eab\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\u6216\u521b\u5efa\u4e00\u4e2a\u7c7b\u6765\u7ba1\u7406\u6570\u636e\u3002\u901a\u8fc7\u51fd\u6570\u8c03\u7528\u6216\u76f4\u63a5\u8bbf\u95ee\u5171\u4eab\u53d8\u91cf\uff0c\u60a8\u53ef\u4ee5\u5728\u4e0d\u540c\u7a97\u53e3\u4e2d\u4f20\u9012\u548c\u66f4\u65b0\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u6709\u6548\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u63a7\u5236\u7a97\u53e3\u7684\u5e03\u5c40\u4e0e\u6837\u5f0f\uff1f<\/strong><br \/>\u5728Tkinter\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u51e0\u4f55\u7ba1\u7406\u5668\uff08\u5982pack\u3001grid\u548cplace\uff09\u6765\u63a7\u5236\u7a97\u53e3\u4e2d\u7684\u63a7\u4ef6\u5e03\u5c40\u3002\u901a\u8fc7\u8bbe\u7f6e\u63a7\u4ef6\u7684\u5c5e\u6027\uff0c\u4f8b\u5982\u5b57\u4f53\u3001\u989c\u8272\u548c\u5927\u5c0f\uff0c\u60a8\u53ef\u4ee5\u81ea\u5b9a\u4e49\u7a97\u53e3\u7684\u6837\u5f0f\u3002\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u4f7f\u6bcf\u4e2a\u7a97\u53e3\u7684\u754c\u9762\u66f4\u7b26\u5408\u7528\u6237\u9700\u6c42\u548c\u7f8e\u89c2\u6807\u51c6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06Python\u8bbe\u7f6e\u4e3a\u4e09\u4e2a\u7a97\u53e3\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u5e93\u548c\u5de5\u5177\uff0c\u6700\u5e38\u89c1\u7684\u6709Tkinter\u3001PyQt\u548cPygame\u3002 \u5728 [&hellip;]","protected":false},"author":3,"featured_media":1117709,"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\/1117697"}],"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=1117697"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1117697\/revisions"}],"predecessor-version":[{"id":1117712,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1117697\/revisions\/1117712"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1117709"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1117697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1117697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1117697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}