diff --git a/code.py b/code.py index 42ffc32..6402cba 100644 --- a/code.py +++ b/code.py @@ -1,144 +1,140 @@ -import PySimpleGUI as sg -import openpyxl -import xlrd -import random - -def TakeRoll(): - book = xlrd.open_workbook('sample.xlsx') - sheet = book.sheet_by_index(0) - book = openpyxl.load_workbook(filename='sample.xlsx') - ins_sheet = book.active - if sheet.ncols == 2: - ins_sheet['C1'] = 'Scredit' - idx = random.randint(2, sheet.nrows) - stu = str(ins_sheet.cell(row=idx, column=1).value) + ins_sheet.cell(row=idx, column=2).value - i = 2 - while i <= sheet.nrows: - ins_sheet.cell(row=i, column=3).value = 0 - i += 1 - book.save(filename='sample.xlsx') - else: - max = 0 - credit = ins_sheet['C'] - i = 1 - while i <= sheet.nrows-1: - credit[i].value *= 2 - i += 1 - i = 1 - while i <= sheet.nrows-1: - if credit[i].value > max: - max = credit[i].value - i += 1 - ''' - while i<=sheet.nrows: - if ins_sheet.cell(row=i, column=3).value > max: - max = ins_sheet.cell(row=i, column=3).value - i += 1 - ''' - max += 1 - slct = [] - i = 1 - while i <= sheet.nrows-1: - credit[i].value = max - credit[i].value - r = 1 - while r <= int(credit[i].value): - slct.append(i) - r += 1 - i += 1 - idx = random.randint(0, len(slct)-1) - stu = str(ins_sheet.cell(row=slct[idx]+1, column=1).value) + ins_sheet.cell(row=slct[idx]+1, column=2).value - return stu, slct[idx]+1 - -def CreditUpdate(stu, isInc, point): - book = openpyxl.load_workbook(filename='sample.xlsx') - sheet = book.active - if isInc == True: - sheet.cell(row=stu, column=3).value += point - else: - sheet.cell(row=stu, column=3).value -= point - book.save(filename='sample.xlsx') - -layoutA = [ - [sg.Text('被点到的同学:'), sg.Text('', key = 'roll')], - [sg.Button('点名')] -] - -layoutB1 = [ - [sg.Button('是'), sg.Button('否')], -] - -layoutB2 = [ - [sg.Button('是'), sg.Button('否')], -] - -layoutB3 = [ - [sg.Button('是'), sg.Button('否')], -] - -layoutC = [ - [sg.Button('回答问题'), sg.Button('请ta坐下')], -] - -List = [0.5, 1.0, 1.5, 2.0, 2.5, 3.0] -layoutD = [ - [sg.Drop(List, - default_value = None, - size = (30, 6), - enable_events = True, - key = 'point' - )], - [sg.Button('确认')] -] - -window = sg.Window('点名', layoutA) -while True: - event, values = window.read() - if event == sg.WINDOW_CLOSED: - break - if event == '点名': - ret, rownum = TakeRoll() - window['roll'].update(value=ret) - window = sg.Window('该同学是否到位?', layoutB1) - while True: - event, values = window.read() - if event in (sg.WINDOW_CLOSED, '否'): - break - if event == '是': - CreditUpdate(rownum, True, 1.0) - window = sg.Window('接下来...', layoutC) - while True: - event, values = window.read() - if event in (sg.WINDOW_CLOSED, '请ta坐下'): - break - if event == '回答问题': - window = sg.Window('该同学能否复述问题?', layoutB2) - while True: - event, values = window.read() - if event == sg.WINDOW_CLOSED: - break - if event == '是': - CreditUpdate(rownum, True, 0.5) - window = sg.Window('该同学答对问题了吗?', layoutB3) - while True: - event, values = window.read() - if event in (sg.WINDOW_CLOSED, '否'): - break - if event == '是': - window = sg.Window('给ta打分:', layoutD) - while True: - event, values = window.read() - if event == sg.WINDOW_CLOSED: - break - if event == '确认': - CreditUpdate(rownum, True, values['point']) - break - break - break - if event == '否': - CreditUpdate(rownum, False, 1.0) - break - break - break - break - -window.close() \ No newline at end of file +import PySimpleGUI as sg +import openpyxl +import xlrd +import random + +def TakeRoll(): + book = xlrd.open_workbook('sample.xlsx') + sheet = book.sheet_by_index(0) + book = openpyxl.load_workbook(filename='sample.xlsx') + ins_sheet = book.active + if sheet.ncols == 2: + ins_sheet['C1'] = 'Scredit' + idx = random.randint(2, sheet.nrows) + stu = str(ins_sheet.cell(row=idx, column=1).value) + ins_sheet.cell(row=idx, column=2).value + i = 2 + while i <= sheet.nrows: + ins_sheet.cell(row=i, column=3).value = 0 + i += 1 + book.save(filename='sample.xlsx') + return stu, idx + else: #学生表中含有已创建的SCredit列 + max = 0 + credit = ins_sheet['C'] #将SCredit列存入redit数组 + i = 1 + while i <= sheet.nrows-1: + credit[i].value *= 2 #将所有积分值整数化(所有非整数积分值小数部分均为0.5) + i += 1 + i = 1 + while i <= sheet.nrows-1: + if credit[i].value > max: + max = credit[i].value + i += 1 + max += 1 #求出最大积分值并加1 + slct = [] + i = 1 + while i <= sheet.nrows-1: + credit[i].value = max - credit[i].value #倒转积分值,将最大的积分值置为1,其余积分依次增大 + r = 1 + while r <= int(credit[i].value): + slct.append(i) + r += 1 #第i个学生倒转后的积分值即为slct数组中对应第i个学生的元素个数,和该生被选中的概率呈负相关 + i += 1 + idx = random.randint(0, len(slct)-1) + stu = str(ins_sheet.cell(row=slct[idx]+1, column=1).value) + ins_sheet.cell(row=slct[idx]+1, column=2).value + return stu, slct[idx]+1 + +def CreditUpdate(stu, isInc, point): + book = openpyxl.load_workbook(filename='sample.xlsx') + sheet = book.active + if isInc == True: + sheet.cell(row=stu, column=3).value += point + else: + sheet.cell(row=stu, column=3).value -= point + book.save(filename='sample.xlsx') + +layoutA = [ + [sg.Text('被点到的同学:'), sg.Text('', key = 'roll')], + [sg.Button('点名')] +] + +layoutB1 = [ + [sg.Button('是'), sg.Button('否')], +] + +layoutB2 = [ + [sg.Button('是'), sg.Button('否')], +] + +layoutB3 = [ + [sg.Button('是'), sg.Button('否')], +] + +layoutC = [ + [sg.Button('回答问题'), sg.Button('请ta坐下')], +] + +List = [0.5, 1.0, 1.5, 2.0, 2.5, 3.0] +layoutD = [ + [sg.Drop(List, + default_value = None, + size = (30, 6), + enable_events = True, + key = 'point' + )], + [sg.Button('确认')] +] + +window = sg.Window('点名', layoutA) +while True: + event, values = window.read() + if event == sg.WINDOW_CLOSED: + break + if event == '点名': + ret, rownum = TakeRoll() + window['roll'].update(value=ret) + window = sg.Window('该同学是否到位?', layoutB1) + while True: + event, values = window.read() + if event in (sg.WINDOW_CLOSED, '否'): + break + if event == '是': + CreditUpdate(rownum, True, 1.0) + window = sg.Window('接下来...', layoutC) + while True: + event, values = window.read() + if event in (sg.WINDOW_CLOSED, '请ta坐下'): + break + if event == '回答问题': + window = sg.Window('该同学能否复述问题?', layoutB2) + while True: + event, values = window.read() + if event == sg.WINDOW_CLOSED: + break + if event == '是': + CreditUpdate(rownum, True, 0.5) + window = sg.Window('该同学答对问题了吗?', layoutB3) + while True: + event, values = window.read() + if event in (sg.WINDOW_CLOSED, '否'): + break + if event == '是': + window = sg.Window('给ta打分:', layoutD) + while True: + event, values = window.read() + if event == sg.WINDOW_CLOSED: + break + if event == '确认': + CreditUpdate(rownum, True, values['point']) + break + break + break + if event == '否': + CreditUpdate(rownum, False, 1.0) + break + break + break + break + +window.close() +