0%

python写一个背单词程序

最近要考试阿,几千个单词叫我背,我怎么背嘛
碰巧周末不想干正事又闲的慌,花了两个小时(说实话两个小时里有一个多小时是在整理单词数据)写了一个python小程序用来背单词,顺便熟悉一下python tkinter的GUI操作
python的GUI简单用用还行,要和其他语言比还是个弟弟

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
import tkinter as tk # GUI
import pandas as pd
from pandas import DataFrame
import random as rd
import re
from PIL import ImageTk, Image

df = pd.read_excel('df.xlsx') # 读取excel表中单词
data = df.values
#data = DataFrame(data)
#data['A'] = data['A']+" "+data['B']

#print("\n{0}".format(df))

# 随机抽单词函数
def next_Random():
global cur_word
global count
count += 1
cur_word = rd.choice(data)
ch = extract_Chinese(str(cur_word))
text.configure(text = ch)
counter.configure(text = str(count))
root.update_idletasks()

# 把中文和英文分离
def extract_Chinese(str):
str = re.sub("[A-Za-z0-9\!\%\[\]\,\。]", "", str)
return str

# 显示答案
def show_Answer():
text.configure(text = str(cur_word))
root.update_idletasks()


# 初始化GUI主体
root = tk.Tk()
root.title('给老子背单词')


#背景画布,大小自己调
canvas = tk.Canvas(root, width=612,height=700,bd=0, highlightthickness=0)
imgpath = 'timg.gif'
img = Image.open(imgpath)
photo = ImageTk.PhotoImage(img)
canvas.create_image(306, 306, image=photo)
canvas.pack()

# 计数器,计已经背了多少个单词
count = 0
counter = tk.Label(root,fg='red',anchor='se')
counter.pack()
counter['text'] = str(count)

# 当前词汇
cur_word = rd.choice(data)
ch = extract_Chinese(str(cur_word))
text = tk.Label(root,text=ch,font=('Arial', 15)) # 创建组件
text['width'] = 25
text['height'] = 3

# 两个按钮 下一个和显示答案
B1 = tk.Button(root, text ="next", command = next_Random)
B2 = tk.Button(root, text ="answer", command = show_Answer)


# canvas.create_window(100, 50, width=100, height=20,
# window=text)

B1.pack()
B2.pack()
text.pack() # 将小部件放置到主窗口中
root.mainloop() # 进入消息循环

然后只需要在Windows计划任务里面设置定时执行这个python文件,就可以做到每天督促自己背单词了
后续考虑再加入一些收藏功能和增删功能吧,界面暂时就不优化了,反正只是自己用,丑就丑点

祝我高分