{"id":1071408,"date":"2025-01-08T11:07:04","date_gmt":"2025-01-08T03:07:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1071408.html"},"modified":"2025-01-08T11:07:06","modified_gmt":"2025-01-08T03:07:06","slug":"python%e5%a6%82%e4%bd%95%e5%81%9a%e9%80%89%e6%8b%a9%e6%a1%86%e5%8b%be%e9%80%89-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1071408.html","title":{"rendered":"python\u5982\u4f55\u505a\u9009\u62e9\u6846\u52fe\u9009"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101916\/e27032e1-291c-4838-921e-39ce52238303.webp\" alt=\"python\u5982\u4f55\u505a\u9009\u62e9\u6846\u52fe\u9009\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u9009\u62e9\u6846\u52fe\u9009\u529f\u80fd\uff0c\u5982\u4f7f\u7528Tkinter\u3001PyQt\u3001wxPython\u7b49\u5e93\u5b9e\u73b0\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Tkinter\u5e93\u5b9e\u73b0\u9009\u62e9\u6846\u52fe\u9009\u529f\u80fd\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<strong>\u4f7f\u7528Tkinter\u5e93\u3001\u521b\u5efaCheckbutton\u7ec4\u4ef6\u3001\u8bbe\u7f6e\u53d8\u91cf\u7ed1\u5b9a<\/strong>\u7b49\u90fd\u662f\u5b9e\u73b0\u9009\u62e9\u6846\u52fe\u9009\u529f\u80fd\u7684\u5173\u952e\u6b65\u9aa4\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u6b65\u9aa4\u6765\u521b\u5efa\u4e00\u4e2a\u9009\u62e9\u6846\uff0c\u5e76\u5bf9\u5176\u4e2d\u7684\u53d8\u91cf\u7ed1\u5b9a\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Tkinter\u5e93\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>Tkinter\u662fPython\u7684\u6807\u51c6GUI\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\u7684\u4e00\u7ec4\u4e30\u5bcc\u7684\u5de5\u5177\u3002Tkinter\u662fPython\u5185\u7f6e\u7684\u6a21\u5757\uff0c\u5b89\u88c5Python\u65f6\u4f1a\u81ea\u52a8\u5b89\u88c5\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684Tkinter\u7a97\u53e3<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u9009\u62e9\u6846\u4e4b\u524d\uff0c\u9996\u5148\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Tkinter\u7a97\u53e3\u3002\u5728\u6b64\u7a97\u53e3\u4e2d\uff0c\u6211\u4eec\u5c06\u6dfb\u52a0\u9009\u62e9\u6846\u7ec4\u4ef6\u3002<\/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<p>root.title(&quot;\u9009\u62e9\u6846\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u542f\u52a8\u4e3b\u4e8b\u4ef6\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><h3>\u4e09\u3001\u521b\u5efaCheckbutton\u7ec4\u4ef6<\/h3>\n<\/p>\n<p><p>Checkbutton\u7ec4\u4ef6\u662fTkinter\u4e2d\u7684\u4e00\u79cd\u9009\u62e9\u6846\u7ec4\u4ef6\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u9009\u62e9\u6216\u53d6\u6d88\u9009\u62e9\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u4e3b\u7a97\u53e3\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u6216\u591a\u4e2aCheckbutton\u7ec4\u4ef6\uff0c\u5e76\u7ed1\u5b9a\u53d8\u91cf\u6765\u4fdd\u5b58\u9009\u62e9\u72b6\u6001\u3002<\/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<p>root.title(&quot;\u9009\u62e9\u6846\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\u6765\u4fdd\u5b58\u9009\u62e9\u72b6\u6001<\/strong><\/h2>\n<p>var1 = tk.IntVar()<\/p>\n<p>var2 = tk.IntVar()<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2aCheckbutton\u7ec4\u4ef6<\/strong><\/h2>\n<p>cb1 = tk.Checkbutton(root, text=&quot;\u9009\u98791&quot;, variable=var1)<\/p>\n<p>cb2 = tk.Checkbutton(root, text=&quot;\u9009\u98792&quot;, variable=var2)<\/p>\n<h2><strong>\u4f7f\u7528pack\u5e03\u5c40\u7ba1\u7406\u5668\u5c06\u9009\u62e9\u6846\u6dfb\u52a0\u5230\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>cb1.pack()<\/p>\n<p>cb2.pack()<\/p>\n<h2><strong>\u542f\u52a8\u4e3b\u4e8b\u4ef6\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>tk.IntVar()<\/code>\u521b\u5efa\u4e86\u4e24\u4e2a\u53d8\u91cf<code>var1<\/code>\u548c<code>var2<\/code>\u6765\u4fdd\u5b58\u9009\u62e9\u72b6\u6001\u3002\u901a\u8fc7\u5c06<code>variable<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a\u8fd9\u4e9b\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u8ddf\u8e2a\u9009\u62e9\u6846\u7684\u9009\u62e9\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u9009\u62e9\u72b6\u6001<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5904\u7406\u9009\u62e9\u72b6\u6001\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5e76\u5728\u9009\u62e9\u6846\u72b6\u6001\u6539\u53d8\u65f6\u8c03\u7528\u5b83\u3002\u5728\u8fd9\u4e2a\u56de\u8c03\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9009\u62e9\u72b6\u6001\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def on_check():<\/p>\n<p>    selected_options = []<\/p>\n<p>    if var1.get() == 1:<\/p>\n<p>        selected_options.append(&quot;\u9009\u98791&quot;)<\/p>\n<p>    if var2.get() == 1:<\/p>\n<p>        selected_options.append(&quot;\u9009\u98792&quot;)<\/p>\n<p>    print(&quot;\u5df2\u9009\u62e9\u7684\u9009\u9879:&quot;, selected_options)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;\u9009\u62e9\u6846\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\u6765\u4fdd\u5b58\u9009\u62e9\u72b6\u6001<\/strong><\/h2>\n<p>var1 = tk.IntVar()<\/p>\n<p>var2 = tk.IntVar()<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2aCheckbutton\u7ec4\u4ef6\uff0c\u5e76\u5c06command\u53c2\u6570\u8bbe\u7f6e\u4e3a\u56de\u8c03\u51fd\u6570<\/strong><\/h2>\n<p>cb1 = tk.Checkbutton(root, text=&quot;\u9009\u98791&quot;, variable=var1, command=on_check)<\/p>\n<p>cb2 = tk.Checkbutton(root, text=&quot;\u9009\u98792&quot;, variable=var2, command=on_check)<\/p>\n<h2><strong>\u4f7f\u7528pack\u5e03\u5c40\u7ba1\u7406\u5668\u5c06\u9009\u62e9\u6846\u6dfb\u52a0\u5230\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>cb1.pack()<\/p>\n<p>cb2.pack()<\/p>\n<h2><strong>\u542f\u52a8\u4e3b\u4e8b\u4ef6\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>on_check<\/code>\u7684\u56de\u8c03\u51fd\u6570\uff0c\u5e76\u5728\u521b\u5efaCheckbutton\u7ec4\u4ef6\u65f6\u5c06<code>command<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a\u6b64\u56de\u8c03\u51fd\u6570\u3002\u6bcf\u5f53\u9009\u62e9\u6846\u7684\u72b6\u6001\u6539\u53d8\u65f6\uff0c<code>on_check<\/code>\u51fd\u6570\u5c06\u88ab\u8c03\u7528\uff0c\u5e76\u8f93\u51fa\u5f53\u524d\u5df2\u9009\u62e9\u7684\u9009\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u4e0d\u540c\u7c7b\u578b\u7684\u53d8\u91cf<\/h3>\n<\/p>\n<p><p>\u9664\u4e86<code>tk.IntVar()<\/code>\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>tk.StringVar()<\/code>\u548c<code>tk.BooleanVar()<\/code>\u6765\u4fdd\u5b58\u9009\u62e9\u72b6\u6001\u3002<code>tk.StringVar()<\/code>\u901a\u5e38\u7528\u4e8e\u4fdd\u5b58\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u72b6\u6001\uff0c\u800c<code>tk.BooleanVar()<\/code>\u7528\u4e8e\u4fdd\u5b58\u5e03\u5c14\u7c7b\u578b\u7684\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def on_check():<\/p>\n<p>    selected_options = []<\/p>\n<p>    if var1.get():<\/p>\n<p>        selected_options.append(&quot;\u9009\u98791&quot;)<\/p>\n<p>    if var2.get():<\/p>\n<p>        selected_options.append(&quot;\u9009\u98792&quot;)<\/p>\n<p>    print(&quot;\u5df2\u9009\u62e9\u7684\u9009\u9879:&quot;, selected_options)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;\u9009\u62e9\u6846\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\u6765\u4fdd\u5b58\u9009\u62e9\u72b6\u6001<\/strong><\/h2>\n<p>var1 = tk.BooleanVar()<\/p>\n<p>var2 = tk.BooleanVar()<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2aCheckbutton\u7ec4\u4ef6\uff0c\u5e76\u5c06command\u53c2\u6570\u8bbe\u7f6e\u4e3a\u56de\u8c03\u51fd\u6570<\/strong><\/h2>\n<p>cb1 = tk.Checkbutton(root, text=&quot;\u9009\u98791&quot;, variable=var1, command=on_check)<\/p>\n<p>cb2 = tk.Checkbutton(root, text=&quot;\u9009\u98792&quot;, variable=var2, command=on_check)<\/p>\n<h2><strong>\u4f7f\u7528pack\u5e03\u5c40\u7ba1\u7406\u5668\u5c06\u9009\u62e9\u6846\u6dfb\u52a0\u5230\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>cb1.pack()<\/p>\n<p>cb2.pack()<\/p>\n<h2><strong>\u542f\u52a8\u4e3b\u4e8b\u4ef6\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>tk.BooleanVar()<\/code>\u6765\u521b\u5efa\u53d8\u91cf<code>var1<\/code>\u548c<code>var2<\/code>\u3002\u8fd9\u4e9b\u53d8\u91cf\u4fdd\u5b58\u5e03\u5c14\u503c\uff08True\u6216False\uff09\uff0c\u8868\u793a\u9009\u62e9\u6846\u662f\u5426\u88ab\u9009\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u8bbe\u7f6e\u9ed8\u8ba4\u9009\u62e9\u72b6\u6001<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u521b\u5efa\u9009\u62e9\u6846\u65f6\u8bbe\u7f6e\u9ed8\u8ba4\u9009\u62e9\u72b6\u6001\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u9ed8\u8ba4\u9009\u4e2d\u201c\u9009\u98791\u201d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def on_check():<\/p>\n<p>    selected_options = []<\/p>\n<p>    if var1.get():<\/p>\n<p>        selected_options.append(&quot;\u9009\u98791&quot;)<\/p>\n<p>    if var2.get():<\/p>\n<p>        selected_options.append(&quot;\u9009\u98792&quot;)<\/p>\n<p>    print(&quot;\u5df2\u9009\u62e9\u7684\u9009\u9879:&quot;, selected_options)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;\u9009\u62e9\u6846\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\u6765\u4fdd\u5b58\u9009\u62e9\u72b6\u6001<\/strong><\/h2>\n<p>var1 = tk.BooleanVar(value=True)  # \u9ed8\u8ba4\u9009\u4e2d<\/p>\n<p>var2 = tk.BooleanVar(value=False) # \u9ed8\u8ba4\u672a\u9009\u4e2d<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2aCheckbutton\u7ec4\u4ef6\uff0c\u5e76\u5c06command\u53c2\u6570\u8bbe\u7f6e\u4e3a\u56de\u8c03\u51fd\u6570<\/strong><\/h2>\n<p>cb1 = tk.Checkbutton(root, text=&quot;\u9009\u98791&quot;, variable=var1, command=on_check)<\/p>\n<p>cb2 = tk.Checkbutton(root, text=&quot;\u9009\u98792&quot;, variable=var2, command=on_check)<\/p>\n<h2><strong>\u4f7f\u7528pack\u5e03\u5c40\u7ba1\u7406\u5668\u5c06\u9009\u62e9\u6846\u6dfb\u52a0\u5230\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>cb1.pack()<\/p>\n<p>cb2.pack()<\/p>\n<h2><strong>\u542f\u52a8\u4e3b\u4e8b\u4ef6\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5728\u521b\u5efa<code>tk.BooleanVar()<\/code>\u53d8\u91cf\u65f6\uff0c\u901a\u8fc7<code>value<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e86\u9ed8\u8ba4\u503c\u3002<code>var1<\/code>\u7684\u9ed8\u8ba4\u503c\u4e3a<code>True<\/code>\uff08\u9009\u4e2d\uff09\uff0c\u800c<code>var2<\/code>\u7684\u9ed8\u8ba4\u503c\u4e3a<code>False<\/code>\uff08\u672a\u9009\u4e2d\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528\u4e0d\u540c\u7684\u5e03\u5c40\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u9664\u4e86<code>pack<\/code>\u5e03\u5c40\u7ba1\u7406\u5668\uff0cTkinter\u8fd8\u63d0\u4f9b\u4e86<code>grid<\/code>\u548c<code>place<\/code>\u5e03\u5c40\u7ba1\u7406\u5668\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u5e03\u5c40\u7ba1\u7406\u5668\u6765\u63a7\u5236\u9009\u62e9\u6846\u5728\u7a97\u53e3\u4e2d\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528grid\u5e03\u5c40\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def on_check():<\/p>\n<p>    selected_options = []<\/p>\n<p>    if var1.get():<\/p>\n<p>        selected_options.append(&quot;\u9009\u98791&quot;)<\/p>\n<p>    if var2.get():<\/p>\n<p>        selected_options.append(&quot;\u9009\u98792&quot;)<\/p>\n<p>    print(&quot;\u5df2\u9009\u62e9\u7684\u9009\u9879:&quot;, selected_options)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;\u9009\u62e9\u6846\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\u6765\u4fdd\u5b58\u9009\u62e9\u72b6\u6001<\/strong><\/h2>\n<p>var1 = tk.BooleanVar()<\/p>\n<p>var2 = tk.BooleanVar()<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2aCheckbutton\u7ec4\u4ef6\uff0c\u5e76\u5c06command\u53c2\u6570\u8bbe\u7f6e\u4e3a\u56de\u8c03\u51fd\u6570<\/strong><\/h2>\n<p>cb1 = tk.Checkbutton(root, text=&quot;\u9009\u98791&quot;, variable=var1, command=on_check)<\/p>\n<p>cb2 = tk.Checkbutton(root, text=&quot;\u9009\u98792&quot;, variable=var2, command=on_check)<\/p>\n<h2><strong>\u4f7f\u7528grid\u5e03\u5c40\u7ba1\u7406\u5668\u5c06\u9009\u62e9\u6846\u6dfb\u52a0\u5230\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>cb1.grid(row=0, column=0, padx=10, pady=10)<\/p>\n<p>cb2.grid(row=1, column=0, padx=10, pady=10)<\/p>\n<h2><strong>\u542f\u52a8\u4e3b\u4e8b\u4ef6\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>grid<\/code>\u5e03\u5c40\u7ba1\u7406\u5668\u901a\u8fc7<code>row<\/code>\u548c<code>column<\/code>\u53c2\u6570\u6307\u5b9a\u9009\u62e9\u6846\u5728\u7f51\u683c\u4e2d\u7684\u4f4d\u7f6e\uff0c\u5e76\u901a\u8fc7<code>padx<\/code>\u548c<code>pady<\/code>\u53c2\u6570\u8bbe\u7f6e\u9009\u62e9\u6846\u4e0e\u5176\u4ed6\u7ec4\u4ef6\u4e4b\u95f4\u7684\u95f4\u8ddd\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528place\u5e03\u5c40\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>def on_check():<\/p>\n<p>    selected_options = []<\/p>\n<p>    if var1.get():<\/p>\n<p>        selected_options.append(&quot;\u9009\u98791&quot;)<\/p>\n<p>    if var2.get():<\/p>\n<p>        selected_options.append(&quot;\u9009\u98792&quot;)<\/p>\n<p>    print(&quot;\u5df2\u9009\u62e9\u7684\u9009\u9879:&quot;, selected_options)<\/p>\n<h2><strong>\u521b\u5efa\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;\u9009\u62e9\u6846\u793a\u4f8b&quot;)<\/p>\n<p>root.geometry(&quot;300x200&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\u6765\u4fdd\u5b58\u9009\u62e9\u72b6\u6001<\/strong><\/h2>\n<p>var1 = tk.BooleanVar()<\/p>\n<p>var2 = tk.BooleanVar()<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2aCheckbutton\u7ec4\u4ef6\uff0c\u5e76\u5c06command\u53c2\u6570\u8bbe\u7f6e\u4e3a\u56de\u8c03\u51fd\u6570<\/strong><\/h2>\n<p>cb1 = tk.Checkbutton(root, text=&quot;\u9009\u98791&quot;, variable=var1, command=on_check)<\/p>\n<p>cb2 = tk.Checkbutton(root, text=&quot;\u9009\u98792&quot;, variable=var2, command=on_check)<\/p>\n<h2><strong>\u4f7f\u7528place\u5e03\u5c40\u7ba1\u7406\u5668\u5c06\u9009\u62e9\u6846\u6dfb\u52a0\u5230\u4e3b\u7a97\u53e3<\/strong><\/h2>\n<p>cb1.place(x=50, y=50)<\/p>\n<p>cb2.place(x=50, y=80)<\/p>\n<h2><strong>\u542f\u52a8\u4e3b\u4e8b\u4ef6\u5faa\u73af<\/strong><\/h2>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>place<\/code>\u5e03\u5c40\u7ba1\u7406\u5668\u901a\u8fc7<code>x<\/code>\u548c<code>y<\/code>\u53c2\u6570\u6307\u5b9a\u9009\u62e9\u6846\u5728\u7a97\u53e3\u4e2d\u7684\u7edd\u5bf9\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u793a\u4f8b\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Tkinter\u5e93\u521b\u5efa\u9009\u62e9\u6846\uff0c\u5e76\u901a\u8fc7\u53d8\u91cf\u7ed1\u5b9a\u548c\u56de\u8c03\u51fd\u6570\u5904\u7406\u9009\u62e9\u72b6\u6001\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u5c55\u793a\u4e86\u5982\u4f55\u8bbe\u7f6e\u9ed8\u8ba4\u9009\u62e9\u72b6\u6001\u548c\u4f7f\u7528\u4e0d\u540c\u7684\u5e03\u5c40\u7ba1\u7406\u5668\u6765\u63a7\u5236\u9009\u62e9\u6846\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Tkinter\u5e93\u3001\u521b\u5efaCheckbutton\u7ec4\u4ef6\u3001\u8bbe\u7f6e\u53d8\u91cf\u7ed1\u5b9a<\/strong>\u662f\u5b9e\u73b0\u9009\u62e9\u6846\u52fe\u9009\u529f\u80fd\u7684\u5173\u952e\u6b65\u9aa4\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u5e76\u7ba1\u7406\u9009\u62e9\u6846\u7ec4\u4ef6\uff0c\u4ece\u800c\u5b9e\u73b0\u7528\u6237\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u5982\u679c\u60a8\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u8bf7\u968f\u65f6\u4e0e\u6211\u8054\u7cfb\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u9009\u62e9\u6846\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Tkinter\u5e93\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\uff0c\u5176\u4e2d\u5305\u62ec\u9009\u62e9\u6846\uff08Checkbutton\uff09\u3002\u4f7f\u7528Tkinter\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efaCheckbutton\u63a7\u4ef6\u5e76\u5c06\u5176\u4e0e\u4e00\u4e2a\u53d8\u91cf\u5173\u8054\u6765\u5b9e\u73b0\u9009\u62e9\u6846\u3002\u53ef\u4ee5\u4f7f\u7528\u6574\u578b\u6216\u5e03\u5c14\u578b\u53d8\u91cf\u6765\u8868\u793a\u9009\u62e9\u6846\u7684\u52fe\u9009\u72b6\u6001\u3002<\/p>\n<p><strong>\u9009\u62e9\u6846\u7684\u52fe\u9009\u72b6\u6001\u5982\u4f55\u83b7\u53d6\uff1f<\/strong><br \/>\u901a\u8fc7\u5173\u8054\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u9009\u62e9\u6846\u7684\u52fe\u9009\u72b6\u6001\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9009\u62e9\u6846\u88ab\u52fe\u9009\uff0c\u53d8\u91cf\u7684\u503c\u901a\u5e38\u4f1a\u88ab\u8bbe\u7f6e\u4e3a1\uff08\u6216True\uff09\uff0c\u800c\u5982\u679c\u672a\u52fe\u9009\uff0c\u5219\u4e3a0\uff08\u6216False\uff09\u3002\u5728\u4f7f\u7528Tkinter\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>get()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u53d8\u91cf\u7684\u5f53\u524d\u503c\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5728\u9009\u62e9\u6846\u4e2d\u653e\u7f6e\u81ea\u5b9a\u4e49\u6587\u672c\u6216\u56fe\u6807\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0cTkinter\u7684Checkbutton\u5141\u8bb8\u60a8\u81ea\u5b9a\u4e49\u6587\u672c\u3001\u56fe\u6807\u6216\u5176\u4ed6\u5185\u5bb9\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>text<\/code>\u53c2\u6570\u6765\u6dfb\u52a0\u6587\u672c\uff0c\u6216\u8005\u4f7f\u7528<code>image<\/code>\u53c2\u6570\u6765\u653e\u7f6e\u56fe\u50cf\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>indicatoron<\/code>\u53c2\u6570\u6765\u63a7\u5236\u662f\u5426\u663e\u793a\u9009\u62e9\u6846\u7684\u6307\u793a\u5668\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u591a\u7684\u81ea\u5b9a\u4e49\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u9009\u62e9\u6846\u52fe\u9009\u529f\u80fd\uff0c\u5982\u4f7f\u7528Tkinter\u3001PyQt\u3001wxPython\u7b49\u5e93\u5b9e\u73b0\u3002\u672c\u6587 [&hellip;]","protected":false},"author":3,"featured_media":1071417,"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\/1071408"}],"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=1071408"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071408\/revisions"}],"predecessor-version":[{"id":1071419,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071408\/revisions\/1071419"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1071417"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1071408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1071408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1071408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}