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
| import tkinter as tk 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') data = df.values
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()
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)
B1.pack() B2.pack() text.pack() root.mainloop()
|