You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
951 B

6 months ago
import tkinter as tk
import csv
import random
# 从CSV读取数据
def read_words_from_csv(file_path):
with open(file_path, 'r', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
next(reader) # 跳过表头
return list(reader)
# 随机选取固定数量的单词
def select_random_words(words, count=5):
return random.sample(words, count)
# 创建并显示单词
def display_words(window, words):
for index, (number, word) in enumerate(words, start=1):
tk.Label(window, text=f"{number}: {word}", font=("Arial", 12)).pack(pady=5)
# 主程序
def main():
# 读取单词
words = read_words_from_csv('words.csv')
window = tk.Tk()
window.title("单词学习器")
tk.Button(window, text="开始背单词",
command=lambda: display_words(tk.Toplevel(), select_random_words(words, 5))).pack(pady=10)
window.mainloop()
if __name__ == "__main__":
main()