{"id":1079247,"date":"2025-01-08T12:19:06","date_gmt":"2025-01-08T04:19:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1079247.html"},"modified":"2025-01-08T12:19:08","modified_gmt":"2025-01-08T04:19:08","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%bc%96%e4%b8%80%e4%b8%aa%e9%97%ae%e5%8d%b7-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1079247.html","title":{"rendered":"\u5982\u4f55\u7528python\u7f16\u4e00\u4e2a\u95ee\u5377"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182403\/0aa60557-daf0-450f-a0d0-1570b5a802f3.webp\" alt=\"\u5982\u4f55\u7528python\u7f16\u4e00\u4e2a\u95ee\u5377\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u7f16\u5199\u4e00\u4e2a\u95ee\u5377\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u521b\u5efa\u95ee\u5377\u5bf9\u8c61\u3001\u5b9a\u4e49\u95ee\u9898\u3001\u8bb0\u5f55\u7b54\u6848\u3001\u5bfc\u51fa\u7ed3\u679c\u7b49\u3002\u5176\u4e2d\uff0c\u9009\u62e9\u4e00\u4e2a\u826f\u597d\u7684\u6570\u636e\u7ed3\u6784\u548c\u5e93\uff08\u5982<code>tkinter<\/code>\u6216<code>Flask<\/code>\uff09\u8fdb\u884c\u7528\u6237\u754c\u9762\u8bbe\u8ba1\u662f\u5173\u952e\u3002<\/strong> \u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528<code>tkinter<\/code>\u5e93\u6765\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62\u7528\u6237\u754c\u9762\uff08GUI\uff09\u95ee\u5377\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u521b\u5efa\u95ee\u5377\u5bf9\u8c61<\/p>\n<p>\u95ee\u5377\u5bf9\u8c61\u53ef\u4ee5\u662f\u4e00\u4e2a\u5305\u542b\u95ee\u9898\u548c\u7b54\u6848\u7684\u6570\u636e\u7ed3\u6784\u3002\u53ef\u4ee5\u4f7f\u7528\u7c7b\u6765\u5b9a\u4e49\u95ee\u5377\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u4e8e\u7ba1\u7406\u548c\u6269\u5c55\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Questionn<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>re:<\/p>\n<p>    def __init__(self, title):<\/p>\n<p>        self.title = title<\/p>\n<p>        self.questions = []<\/p>\n<p>        self.answers = []<\/p>\n<p>    def add_question(self, question):<\/p>\n<p>        self.questions.append(question)<\/p>\n<p>    def record_answer(self, answer):<\/p>\n<p>        self.answers.append(answer)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5b9a\u4e49\u95ee\u9898<\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u95ee\u9898\u7c7b\u6765\u5b9a\u4e49\u95ee\u9898\u3002\u95ee\u9898\u53ef\u4ee5\u5305\u62ec\u95ee\u9898\u6587\u672c\u3001\u9009\u9879\u548c\u7b54\u6848\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Question:<\/p>\n<p>    def __init__(self, text, options=None, answer_type=&#39;text&#39;):<\/p>\n<p>        self.text = text<\/p>\n<p>        self.options = options<\/p>\n<p>        self.answer_type = answer_type<\/p>\n<p>        self.answer = None<\/p>\n<p>    def set_answer(self, answer):<\/p>\n<p>        self.answer = answer<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762<\/p>\n<p>\u4f7f\u7528<code>tkinter<\/code>\u5e93\u6765\u8bbe\u8ba1\u7528\u6237\u754c\u9762\uff0c\u4f7f\u7528\u6237\u80fd\u591f\u56de\u7b54\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tkinter as tk<\/p>\n<p>class QuestionnaireApp:<\/p>\n<p>    def __init__(self, root, questionnaire):<\/p>\n<p>        self.root = root<\/p>\n<p>        self.questionnaire = questionnaire<\/p>\n<p>        self.current_question_index = 0<\/p>\n<p>        self.question_label = tk.Label(root, text=&quot;&quot;)<\/p>\n<p>        self.question_label.pack()<\/p>\n<p>        self.answer_entry = tk.Entry(root)<\/p>\n<p>        self.answer_entry.pack()<\/p>\n<p>        self.submit_button = tk.Button(root, text=&quot;Submit&quot;, command=self.submit_answer)<\/p>\n<p>        self.submit_button.pack()<\/p>\n<p>        self.show_question()<\/p>\n<p>    def show_question(self):<\/p>\n<p>        if self.current_question_index &lt; len(self.questionnaire.questions):<\/p>\n<p>            question = self.questionnaire.questions[self.current_question_index]<\/p>\n<p>            self.question_label.config(text=question.text)<\/p>\n<p>            self.answer_entry.delete(0, tk.END)<\/p>\n<p>    def submit_answer(self):<\/p>\n<p>        answer = self.answer_entry.get()<\/p>\n<p>        self.questionnaire.questions[self.current_question_index].set_answer(answer)<\/p>\n<p>        self.current_question_index += 1<\/p>\n<p>        if self.current_question_index &lt; len(self.questionnaire.questions):<\/p>\n<p>            self.show_question()<\/p>\n<p>        else:<\/p>\n<p>            self.show_results()<\/p>\n<p>    def show_results(self):<\/p>\n<p>        self.question_label.config(text=&quot;Thank you for completing the questionnaire!&quot;)<\/p>\n<p>        self.answer_entry.pack_forget()<\/p>\n<p>        self.submit_button.pack_forget()<\/p>\n<p>        for question in self.questionnaire.questions:<\/p>\n<p>            print(f&quot;Question: {question.text}, Answer: {question.answer}&quot;)<\/p>\n<p>root = tk.Tk()<\/p>\n<p>root.title(&quot;Questionnaire&quot;)<\/p>\n<p>questionnaire = Questionnaire(&quot;Sample Questionnaire&quot;)<\/p>\n<p>questionnaire.add_question(Question(&quot;What is your name?&quot;))<\/p>\n<p>questionnaire.add_question(Question(&quot;How old are you?&quot;))<\/p>\n<p>questionnaire.add_question(Question(&quot;What is your favorite color?&quot;))<\/p>\n<p>app = QuestionnaireApp(root, questionnaire)<\/p>\n<p>root.mainloop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u8bb0\u5f55\u7b54\u6848<\/p>\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u53ef\u4ee5\u8bb0\u5f55\u7528\u6237\u7684\u7b54\u6848\uff0c\u5e76\u5728\u95ee\u5377\u5b8c\u6210\u540e\u663e\u793a\u7ed3\u679c\u3002\u53ef\u4ee5\u6269\u5c55\u529f\u80fd\uff0c\u4f8b\u5982\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230\u6587\u4ef6\u3001\u6570\u636e\u5e93\u6216\u5728\u7ebf\u8868\u5355\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5bfc\u51fa\u7ed3\u679c<\/p>\n<p>\u53ef\u4ee5\u5c06\u7ed3\u679c\u4fdd\u5b58\u5230CSV\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\uff0c\u4ee5\u4fbf\u4e8e\u8fdb\u4e00\u6b65\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>def export_results(questionnaire, filename):<\/p>\n<p>    with open(filename, &#39;w&#39;, newline=&#39;&#39;) as csvfile:<\/p>\n<p>        fieldnames = [&#39;Question&#39;, &#39;Answer&#39;]<\/p>\n<p>        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)<\/p>\n<p>        writer.writeheader()<\/p>\n<p>        for question in questionnaire.questions:<\/p>\n<p>            writer.writerow({&#39;Question&#39;: question.text, &#39;Answer&#39;: question.answer})<\/p>\n<h2><strong>Example usage<\/strong><\/h2>\n<p>export_results(questionnaire, &#39;results.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u95ee\u5377\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u901a\u8fc7\u56fe\u5f62\u7528\u6237\u754c\u9762\u4e0e\u7528\u6237\u8fdb\u884c\u4ea4\u4e92\u3002\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u6269\u5c55\u529f\u80fd\uff0c\u4f8b\u5982\u6dfb\u52a0\u591a\u9009\u9898\u3001\u8bc4\u5206\u9898\u7b49\u3002\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efa\u95ee\u5377\u5bf9\u8c61\u3001\u5b9a\u4e49\u95ee\u9898\u3001\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u3001\u8bb0\u5f55\u7b54\u6848\u548c\u5bfc\u51fa\u7ed3\u679c\u3002\u4f7f\u7528<code>tkinter<\/code>\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0c\u4f7f\u7528\u6237\u80fd\u591f\u8f7b\u677e\u5730\u56de\u7b54\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u521b\u5efa\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u95ee\u5377\uff1f<\/strong><br \/>\u4f7f\u7528Python\u521b\u5efa\u4ea4\u4e92\u5f0f\u95ee\u5377\uff0c\u53ef\u4ee5\u5229\u7528<code>input()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u7528\u6237\u7684\u56de\u7b54\u3002\u4f60\u53ef\u4ee5\u8bbe\u8ba1\u4e00\u4e2a\u7b80\u5355\u7684\u547d\u4ee4\u884c\u95ee\u5377\uff0c\u9010\u6b65\u8be2\u95ee\u7528\u6237\u95ee\u9898\u5e76\u8bb0\u5f55\u4ed6\u4eec\u7684\u7b54\u6848\u3002\u4f7f\u7528\u5b57\u5178\u6216\u5217\u8868\u6765\u5b58\u50a8\u7528\u6237\u7684\u56de\u7b54\uff0c\u4ece\u800c\u65b9\u4fbf\u540e\u7eed\u7684\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u63a8\u8350\u7684Python\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u5236\u4f5c\u95ee\u5377\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u4e2d\u6709\u51e0\u4e2a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u7b80\u5316\u95ee\u5377\u5236\u4f5c\u7684\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c<code>Tkinter<\/code>\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u56fe\u5f62\u7528\u6237\u754c\u9762\uff0c\u800c<code>Flask<\/code>\u53ef\u4ee5\u7528\u4e8e\u6784\u5efa\u7f51\u7edc\u95ee\u5377\u3002<code>SurveyMonkey<\/code>\u548c<code>Google Forms<\/code>\u4e5f\u6709API\u53ef\u4ee5\u4e0ePython\u7ed3\u5408\uff0c\u5e2e\u52a9\u4f60\u66f4\u65b9\u4fbf\u5730\u6536\u96c6\u548c\u7ba1\u7406\u95ee\u5377\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u6536\u96c6\u5230\u7684\u95ee\u5377\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>csv<\/code>\u6216<code>json<\/code>\u5e93\u5c06\u95ee\u5377\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002\u901a\u8fc7\u5c06\u7528\u6237\u7684\u56de\u7b54\u5199\u5165CSV\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002\u82e5\u9009\u62e9<code>json<\/code>\u683c\u5f0f\uff0c\u53ef\u4ee5\u4fdd\u5b58\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u65b9\u4fbf\u540e\u7eed\u8bfb\u53d6\u548c\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u5206\u6790\u95ee\u5377\u6536\u96c6\u5230\u7684\u6570\u636e\uff1f<\/strong><br \/>\u5bf9\u95ee\u5377\u6570\u636e\u8fdb\u884c\u5206\u6790\uff0c\u53ef\u4ee5\u4f7f\u7528<code>Pandas<\/code>\u5e93\u3002\u8be5\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u80fd\u591f\u8f7b\u677e\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3001\u7edf\u8ba1\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002\u901a\u8fc7\u7ed8\u5236\u56fe\u8868\u548c\u751f\u6210\u62a5\u544a\uff0c\u53ef\u4ee5\u66f4\u76f4\u89c2\u5730\u5c55\u793a\u95ee\u5377\u7ed3\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u7f16\u5199\u4e00\u4e2a\u95ee\u5377\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u521b\u5efa\u95ee\u5377\u5bf9\u8c61\u3001\u5b9a\u4e49\u95ee\u9898\u3001\u8bb0\u5f55\u7b54\u6848\u3001\u5bfc\u51fa\u7ed3\u679c\u7b49\u3002\u5176\u4e2d\uff0c\u9009\u62e9\u4e00\u4e2a [&hellip;]","protected":false},"author":3,"featured_media":1079253,"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\/1079247"}],"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=1079247"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1079247\/revisions"}],"predecessor-version":[{"id":1079254,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1079247\/revisions\/1079254"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1079253"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1079247"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1079247"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1079247"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}