You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python直接基于数据库字段计算算术平均值(不使用SQL)

算术平均值计算器问题修复

以下是针对你遇到的报错和逻辑问题的修正方案:

已知问题整理

  • 计算函数调用时未传入要求的位置参数,触发TypeError
  • 数据库查询仅执行了cursor.execute未拉取结果,直接操作游标对象无法拿到实际数值
  • 对局数统计逻辑错误,未按选中的左侧玩家过滤数据,无法统计指定玩家的对应对局数量
  • 按钮绑定逻辑错误,无法同时触发两个计算函数,也不会自动把计算结果更新到界面
  • 未读取下拉框选中的玩家组合,无法实现按玩家筛选数据的需求

修正后完整代码

from tkinter import *
from tkinter import ttk
import tkinter as tk
import sqlite3

app=Tk()
app.title("糖果平均值计算器")
app.geometry("300x400")

#####################################
# 数据库连接
# 路径不要加前缀/,否则会读取系统根目录下的文件
con = sqlite3.connect('database.db')
cursor = con.cursor()

#####################################
# 整合后的计算逻辑
def calculate_mean():
    # 读取下拉框选中的玩家组合
    selected = combo.get()
    if selected == "Select Clashes":
        return
    # 拆分出左侧玩家名称
    left_player = selected.split("-")[0]
    
    # 查询指定左侧玩家的扔出糖果总和、对应对局数
    cursor.execute('SELECT SUM(Candy_launched), COUNT(*) FROM clashes WHERE box_left = ?', (left_player,))
    launched_sum, launched_count = cursor.fetchone()
    mean_launched = round(launched_sum / launched_count, 2) if launched_count != 0 else 0
    
    # 查询指定左侧玩家的收到糖果总和、对应对局数
    cursor.execute('SELECT SUM(Candy_received), COUNT(*) FROM clashes WHERE box_left = ?', (left_player,))
    received_sum, received_count = cursor.fetchone()
    mean_received = round(received_sum / received_count, 2) if received_count != 0 else 0
    
    # 更新界面结果显示
    Result_Mean_Candy_launched.config(text=str(mean_launched))
    Result_Mean_Candy_received.config(text=str(mean_received))

##################################
# GUI部分
# 下拉框加载所有不重复的玩家组合
cursor.execute('SELECT DISTINCT box_left || "-" || box_right FROM clashes')
values = [row[0] for row in cursor.fetchall()]

combo = ttk.Combobox(app, width=21, values=values)
combo.set("Select Clashes")
combo.place(x=20, y=20)

# 结果展示控件
BoxLeft = Label(app, text="BOX LEFT", foreground='black',  font='arial 12 bold')
BoxLeft.place(x=30, y=130)

Mean_Candy_launched = Label(app, text="Mean Candy launched:", foreground='black',  font='arial 11')
Mean_Candy_launched.place(x=30, y=160)
Mean_Candy_received = Label(app, text="Mean Candy received:", foreground='black',  font='arial 11')
Mean_Candy_received.place(x=30, y=190)

Result_Mean_Candy_launched = Label(app, text=" ? ", foreground='black',  font='arial 11')
Result_Mean_Candy_launched.place(x=190, y=160)

Result_Mean_Candy_received = Label(app, text=" ? ", foreground='black',  font='arial 11')
Result_Mean_Candy_received.place(x=190, y=190)

# 计算按钮绑定整合后的计算函数
button = Button(app, text="Calculate", command = calculate_mean)
button.place(x=30, y=260)

app.mainloop()

关键修改说明

  • 合并计算逻辑到同一个函数,自动读取下拉框选中的玩家,不需要额外传参,解决参数缺失报错
  • 直接在数据库层面用SUM和COUNT做统计,不用拉取全量数据自行计算,效率更高也更准确
  • 新增了空值判断,避免没有对应对局时出现除以0的报错
  • 计算完成后直接修改Label的text属性更新界面显示
  • 下拉框改为加载所有不重复的玩家组合,如果需要仅显示最近2条可以改回原SQL逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 10:36:03