import tkinter
import pandas as pd
import openpyxl
Win = tkinter.Tk()
Win.title("简单的考试程序——单选题")
Win.geometry("700x400+350+80") # 这里的400x400是指窗口大小,+20+20是指窗口运行后距离屏幕左上角的位置
#设置窗口是否可变长、宽,True:可变,False:不可变
Win.resizable(width=False, height=False)
df = pd.read_excel('dati.xlsx')#定义第一列这索引列
label1 = tkinter.Label(Win,text = '请选择你的答案',
fg = 'red',
font = ('宋体',18),
relief = 'raised'
)
label1.pack()
label2 = tkinter.Label(Win,font = ('宋体',18),wraplength = 650,text = df.iloc[0,0])#wraplength 设置多少单位后开始换行
label2.place(x=20,y=50)
var = tkinter.IntVar()
def contrast():
#标准答案对照模块
dic = {0:'A',1:'B',2:'C',3:'D'}
# s = "你选择了"+dic.get(var.get())+"项" #var.get()得到鼠标按下的返回value值
your_answer = var.get()
if dic.get(your_answer) == df.iloc[a,6]:
label1.configure(text = '恭喜答对了!')
else:
label1.configure(text = '答错了,加油!')
def begin():
global a
a += 1
if a < df.shape[0]:
label1.configure(text = '请选择你的答案')
label2.configure(text = df.iloc[a,0])
rd1.configure(text = df.iloc[a,1])
rd2.configure(text = df.iloc[a,2])
rd3.configure(text = df.iloc[a,3])
rd4.configure(text = df.iloc[a,4])
#print(pd.isna(df.iloc[a,4]))
if pd.isna(df.iloc[a,4]):#判断数值是否为空可用,
rd4.place_forget()
else:
rd4.place(x=100,y=270)
else:
label1.configure(text = '最后一题了!')
a = df.shape[0]-1#将a的值保持在最后一题
def tiao():
global a
try: #限制只能输入数字 输入其会产生异常
a=int(e.get())-1
if a>=0 and a < df.shape[0]:
label2.configure(text = df.iloc[a,0])
rd1.configure(text = df.iloc[a,1])
rd2.configure(text = df.iloc[a,2])
rd3.configure(text = df.iloc[a,3])
rd4.configure(text = df.iloc[a,4])
#判断答案D是否为空,为空就不显示
if pd.isna(df.iloc[a,4]):
rd4.place_forget()
else:
rd4.place(x=100,y=270)
label1.configure(text = '已跳转至第'+e.get()+'题')
else:
label1.configure(text = '重新输入题号')
except ValueError:
label1.configure(text = '只能输入数字')
def before():
global a
a-=1
if a >= 0:
label1.configure(text = '请选择答案')
label2.configure(text = df.iloc[a,0])
rd1.configure(text = df.iloc[a,1])
rd2.configure(text = df.iloc[a,2])
rd3.configure(text = df.iloc[a,3])
rd4.configure(text = df.iloc[a,4])
if pd.isna(df.iloc[a,4]):
rd4.place_forget()
else:
rd4.place(x=100,y=270)
else:
label1.configure(text = '已经是第一题了')
a = 0#将a的值保持在第一题
a = 0
rd1 = tkinter.Radiobutton(Win,text=df.iloc[0,1],font = ('宋体',18),variable=var,value=0,command=contrast)
rd1.place(x=100,y=120)
rd2 = tkinter.Radiobutton(Win,text=df.iloc[0,2],font = ('宋体',18),variable=var,value=1,command=contrast)
rd2.place(x=100,y=170)
rd3 = tkinter.Radiobutton(Win,text=df.iloc[0,3],font = ('宋体',18),variable=var,value=2,command=contrast)
rd3.place(x=100,y=220)
rd4 = tkinter.Radiobutton(Win,text=df.iloc[0,4],font = ('宋体',18),variable=var,value=3,command=contrast)
rd4.place(x=100,y=270)
btn2 = tkinter.Button(Win, text='上一题', font = ('宋体',18),command=before)
btn2.place(x=100,y=320)
btn1 = tkinter.Button(Win, text='下一题',font = ('宋体',18), command=begin)
btn1.place(x=200,y=320)
s = '共' + str(df.shape[0]) + '题'
label3 = tkinter.Label(Win,text = s,font = ('宋体',18))
label3.place(x=450,y=320)
e=tkinter.Entry(Win,font = ('宋体',28),width = '2')
e.place(x=400,y=320)
btn3 = tkinter.Button(Win, text= '跳转至',font = ('宋体',18), command=tiao)
btn3.place(x=300,y=320)
Win.mainloop()
利用python编写的基于tkinter、pandas,excle写的单选题答题练习程序
版权申诉
5星 · 超过95%的资源 63 浏览量
2021-07-14
17:15:37
上传
评论 8
收藏 9KB RAR 举报
安安0924
- 粉丝: 1
- 资源: 1
最新资源
- python-leetcode面试题解之第157题用Read4读取N个字符-题解.zip
- python-leetcode面试题解之第156题上下翻转二叉树-题解.zip
- python-leetcode面试题解之第155题最小栈-题解.zip
- python-leetcode面试题解之第153题寻找旋转排序数组中的最小值-题解.zip
- python-leetcode面试题解之第152题乘积最大子数组-题解.zip
- python-leetcode面试题解之第151题反转字符串中的单词-题解.zip
- python-leetcode面试题解之第150题逆波兰表达式求值-题解.zip
- python-leetcode面试题解之第149题直线上最多的点数-题解.zip
- python-leetcode面试题解之第148题排序链表-题解.zip
- python-leetcode面试题解之第147题对链表进行插入排序-题解.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页