You need to enable JavaScript to run this app. 基于Tkinter实现问题列表遍历与单选按钮答案记录的开发问题咨询
优惠活动
大模型
产品
解决方案
定价
更多

基于Tkinter实现问题列表遍历与单选按钮答案记录的开发问题咨询

解决方案

核心实现思路

  • 遍历问题列表:将Excel中读取的所有问题按你预设的5个分类(Governance、Eligibility等)拆分为子列表,每个分类维护独立的题目索引,避免硬编码固定行号的问题。
  • Next按钮跳转逻辑:每次点击按钮时先校验用户是否选择了答案,确认后存储当前题的作答结果,再索引+1加载下一题;如果当前分类的所有题目已答完,自动切换到下一个分类标签页,全部题目作答完成后自动导出为CSV文件。

完整修改代码

from tkinter import *
from tkinter import ttk, messagebox
import csv
from openpyxl import load_workbook

# 读取Excel问题
wb = load_workbook('Questions.xlsx')
ws = wb.active
all_questions = [cell.value for cell in ws['A'] if cell.value is not None]

# 按分类拆分问题,可根据你Excel的实际行数调整范围
# 示例:前10题是Governance,接下来10题Eligibility,以此类推,可自行修改范围
category_questions = [
    all_questions[0:10],  # Governance
    all_questions[10:20], # Eligibility
    all_questions[20:30], # Operations
    all_questions[30:40], # Monitoring
    all_questions[40:50]  # Continuing Improvement
]
category_names = ['1) Governance', '2) Eligibility', '3) Operations', '4) Monitoring', '5) Continuing Improvment']

# 存储作答结果
result_list = []
# 当前分类索引、当前题目索引
current_cat = 0
current_q = 0
response = None
question_label = None

# 导出结果为CSV
def export_csv():
    with open('question_results.csv', 'w', newline='', encoding='utf-8-sig') as f:
        writer = csv.writer(f)
        writer.writerow(['分类', '问题', '答案'])
        writer.writerows(result_list)
    messagebox.showinfo('完成', '所有题目已作答,结果已导出为question_results.csv')
    root.destroy()

# 加载当前题目
def load_question():
    global question_label, response
    # 更新题目标签
    current_question = category_questions[current_cat][current_q]
    question_label.config(text=current_question)
    # 重置单选按钮选择
    response.set('')

# Next按钮点击逻辑
def next_question():
    global current_cat, current_q
    # 校验是否选择答案
    select_val = response.get()
    if not select_val:
        messagebox.showwarning('提示', '请选择答案后再跳转下一题')
        return
    # 存储当前作答结果
    result_list.append([
        category_names[current_cat],
        category_questions[current_cat][current_q],
        select_val
    ])
    # 计算下一题索引
    current_q += 1
    # 判断当前分类是否还有未答题目
    if current_q < len(category_questions[current_cat]):
        load_question()
        return
    # 当前分类已答完,跳转到下一个分类
    current_cat += 1
    current_q = 0
    if current_cat < len(category_names):
        # 切换标签页
        tabControl.select(current_cat)
        load_question()
        return
    # 所有分类都已答完,导出CSV
    export_csv()

# 退出程序
def exit_app():
    root1.destroy()

# 开始答题
def start_app():
    global root1, root, tabControl, response, question_label
    root1.destroy()
    root = Tk()
    root.title("Question Tool")
    # 创建标签页
    tabControl = ttk.Notebook(root)
    tabs = []
    for name in category_names:
        tab = ttk.Frame(tabControl)
        tabControl.add(tab, text=name)
        tabs.append(tab)
    tabControl.pack(expand = 1, fill ="both")

    # 每个标签页通用布局
    for tab in tabs:
        # 题目标签
        question_label = ttk.Label(tab, text='')
        question_label.grid(column = 0,row = 0,padx = 30,pady = 30)
        # 单选按钮
        response = StringVar()
        RB1=ttk.Radiobutton(tab,text="Yes",value="YES",variable=response)
        RB1.grid(column=0,row=5,padx=10,pady=10)
        RB2=ttk.Radiobutton(tab,text="No",value="NO",variable=response)
        RB2.grid(column=0,row=10,padx=10,pady=10)
        # Next按钮
        ttk.Button(tab,text="Next",command=next_question).grid(column=0,row=20,padx=10,pady=10)

    # 加载第一题
    load_question()
    root.mainloop()

# 启动页
root1 = Tk()
label1=Label(root1,text="Question Tool")
label1.pack(pady=20)
Start_Button=Button(root1,text="Click to Begin",padx=10,pady=10, command=start_app)
Start_Button.pack(pady=10)
Exit_Button=Button(root1,text="Exit",padx=10,pady=10,command=exit_app)
Exit_Button.pack(pady=10)
root1.mainloop()

使用说明

  1. 调整代码中category_questions的切片范围,匹配你Excel中每个分类对应的问题行区间
  2. 导出的CSV文件采用UTF-8 BOM编码,可直接导入Power BI使用
  3. 增加了未选择答案的拦截提示,避免漏答

内容的提问来源于stack exchange,提问作者Bradley Hansen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 11:36:03