日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python通过tkinter和json界面库实现考研知识点统计

發(fā)布時間:2023/12/4 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python通过tkinter和json界面库实现考研知识点统计 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

python通過tkinter和json界面庫實現(xiàn)考研知識點統(tǒng)計

使用下列代碼前必須安裝tkinter和json庫

一、實現(xiàn)簡單界面:

""" from tkinter import * import test class mainView():def __init__(self):self.initializeUI()def initializeUI(self):self.window = Tk()self.window.title('22考研數(shù)學真題統(tǒng)計')self.window.geometry('1000x600')Label1 =Label(self.window, text="輸入要統(tǒng)計的鍵值對數(shù)據(jù):",fg = 'blue',font=('宋體',20,'italic'))Label1.pack()self.text = Text(self.window, width=80, height=1,font=('宋體',18,'italic'))self.text.pack()Button(self.window, text="開始統(tǒng)計",font=('Helvetica', '20'), command=self.begin).pack()Button(self.window, text="退出",font=('Helvetica', '20'), command=self.end).pack()mainloop()def begin(self):flag=Trueif len(self.text.get('0.0','end').split('\n'))!=2:print('不能換行輸入數(shù)據(jù)!')flag=Falseself.window.destroy()if flag:answer=self.text.get('0.0','end').strip('\n')test.main(answer)self.window.destroy()def end(self):self.window.destroy() def main():mainView()if __name__ == '__main__':main()

二、實現(xiàn)統(tǒng)計代碼:

import json def init():result={}index = [i for i in range(235)]for i in range(len(index)):result[i]=0with open('kaoYanMath22.json', 'w') as f:json.dump(result, f)def calculate(changeValue):#先讀with open('kaoYanMath22.json', 'r') as f:result = json.load(f)#備份with open('kaoYanMath22-1.json', 'w') as f:json.dump(result, f)#再寫for i in range(len(changeValue)):result[str(changeValue[i][0])]+=changeValue[i][1]with open('kaoYanMath22.json', 'w') as f:json.dump(result, f)def readMath():with open('kaoYanMath22.json', 'r') as f:result = json.load(f)print(result)def main(string):# readMath()# string = input()# print(string.split(','))# 對異常輸入進行限制flag = Truefor keyValue in string.split(','):if len(keyValue) == 0:print('輸入統(tǒng)計數(shù)據(jù)有誤!,請重新輸入')flag = Falsebreakelif len(keyValue.split(':'))!=2:print('輸入統(tǒng)計數(shù)據(jù)有誤!,請重新輸入')flag = Falsebreakif flag:changeValue = [[int(keyValue.split(':')[0])-1, int(keyValue.split(':')[1])] for keyValue in string.split(',')]for index in range(len(string.split(','))):if int(string.split(',')[index].split(':')[0]) < 1 or int(string.split(',')[index].split(':')[0]) > 235 or int(string.split(',')[index].split(':')[1]) < 0 or int(string.split(',')[index].split(':')[1]) > 20:print('輸入統(tǒng)計數(shù)據(jù)有誤!,請重新輸入')flag = Falsebreakif flag:print('changeValue',changeValue)calculate(changeValue)if __name__ == '__main__':#main()pass

總結:
在本文的稍微地實現(xiàn)了部分異常處理,如用備份防止數(shù)據(jù)隨意篡改,對輸入數(shù)據(jù)進行約束;

備注:
如果本文存在的不足之處,希望留言評論區(qū),謝謝!

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結

以上是生活随笔為你收集整理的python通过tkinter和json界面库实现考研知识点统计的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。