parent
f97f9a5d1c
commit
293d25f643
@ -0,0 +1,39 @@
|
|||||||
|
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()
|
@ -1,37 +1,33 @@
|
|||||||
# -*- coding: gbk -*-
|
# -*- coding: gbk -*-
|
||||||
from typing import List
|
import pymysql
|
||||||
|
import pandas as pd
|
||||||
|
# -*- coding: <encoding name> -*-
|
||||||
|
|
||||||
import mysql
|
|
||||||
import mysql.connector
|
|
||||||
import csv
|
|
||||||
|
|
||||||
db_config = {
|
db_config = {
|
||||||
'host': 'localhost',
|
'host': 'localhost',
|
||||||
|
'port': 3306,
|
||||||
'user': 'root',
|
'user': 'root',
|
||||||
'password': '2141203017',
|
'password': '21412030117',
|
||||||
'database': 'word',
|
'database': 'word',
|
||||||
|
'charset': 'utf8mb4',
|
||||||
}
|
}
|
||||||
|
|
||||||
cnx = mysql.connector.connect(**db_config)
|
|
||||||
cursor = cnx.cursor()
|
|
||||||
|
|
||||||
# 读取CSV
|
|
||||||
with open('extracted_data.csv', mode='r', encoding='utf-8') as file:
|
|
||||||
reader = csv.reader(file)
|
|
||||||
headers = next(reader, None)
|
|
||||||
if headers is not None: # 过表头
|
|
||||||
for row in reader:
|
|
||||||
sql = "INSERT IGNORE INTO word(xuhao,neirong) VALUES (%s, %s)"
|
|
||||||
cursor.execute(sql, (row[0], row[1])) #设csv与数据库对应
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# ...(原来的执行代码)
|
connection = pymysql.connect(**db_config)
|
||||||
cnx.commit()
|
cursor = connection.cursor()
|
||||||
print("数据导入成功!")
|
extracted='data.csv'
|
||||||
except mysql.connector.Error as err:
|
data_to_insert= pd.read_csv(extracted)
|
||||||
print(f"数据导入失败: {err}")
|
|
||||||
finally:
|
|
||||||
cursor.close()
|
for item in data_to_insert:
|
||||||
cnx.close()
|
xuhao, neirong = item
|
||||||
|
insert_sql = "INSERT INTO word (xuhao, neirong) VALUES (%s, %s)"
|
||||||
|
cursor.execute(insert_sql, (xuhao, neirong))
|
||||||
|
|
||||||
|
|
||||||
|
connection.commit()
|
||||||
|
print("数据插入成功!")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"数据插入失败,错误信息:{e}")
|
File diff suppressed because it is too large
Load Diff
@ -1,36 +1,32 @@
|
|||||||
import pymysql
|
import tkinter as tk
|
||||||
#import cryptography
|
from tkinter import messagebox
|
||||||
# 数据库连接配置
|
|
||||||
db_config = {
|
|
||||||
'host': 'localhost', # 数据库地址
|
|
||||||
'user': 'Mysql', # 数据库用户名
|
|
||||||
'password': '2141203017', # 数据库密码
|
|
||||||
'database': 'word', # 数据库名
|
|
||||||
'charset': 'utf8mb4', # 字符编码
|
|
||||||
}
|
|
||||||
|
|
||||||
# 待插入的数据
|
# 单词列表
|
||||||
data_to_insert = [
|
words = ["apple", "banana", "cherry", "date", "elderberry"]
|
||||||
('1', 'abandon vt.旬弃;形弃,萨瑟'),
|
|
||||||
('2', 'ability n.能力;能耐,乃炸'),
|
|
||||||
# ... 其他数据 ...
|
|
||||||
# 注意:这里的每一项都是一个元组,格式为(序号, 内容)
|
|
||||||
]
|
|
||||||
|
|
||||||
# 连接数据库并尝试插入数据
|
class WordApp(tk.Tk):
|
||||||
try:
|
def __init__(self):
|
||||||
connection = pymysql.connect(**db_config)
|
super().__init__()
|
||||||
cursor = connection.cursor()
|
self.title("单词学习")
|
||||||
|
self.current_word_index = 0
|
||||||
|
self.word_label = tk.Label(self, text="", font=("Helvetica", 20))
|
||||||
|
self.word_label.pack(pady=20)
|
||||||
|
self.create_buttons()
|
||||||
|
|
||||||
# 执行批量插入
|
def create_buttons(self):
|
||||||
for item in data_to_insert:
|
self.know_button = tk.Button(self, text="认识", command=self.show_next_word, width=10)
|
||||||
xuhao, neirong = item
|
self.not_know_button = tk.Button(self, text="不认识", command=self.show_next_word, width=10)
|
||||||
insert_sql = "INSERT INTO word (xuhao, neirong) VALUES (%s, %s)"
|
self.know_button.pack(side=tk.LEFT, padx=10, pady=10)
|
||||||
cursor.execute(insert_sql, (xuhao, neirong))
|
self.not_know_button.pack(side=tk.RIGHT, padx=10, pady=10)
|
||||||
|
|
||||||
# 提交事务
|
def show_next_word(self):
|
||||||
connection.commit()
|
if self.current_word_index < len(words):
|
||||||
print("数据插入成功!")
|
self.word_label.config(text=words[self.current_word_index])
|
||||||
except Exception as e:
|
self.current_word_index += 1
|
||||||
print(f"数据插入失败,错误信息:{e}")
|
else:
|
||||||
|
messagebox.showinfo("结束", "所有单词已学习完毕!")
|
||||||
|
self.destroy()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app = WordApp()
|
||||||
|
app.mainloop()
|
@ -0,0 +1,47 @@
|
|||||||
|
import tkinter as tk
|
||||||
|
import csv
|
||||||
|
import random
|
||||||
|
from struct import pack
|
||||||
|
|
||||||
|
|
||||||
|
# 从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, word_index, total_words):
|
||||||
|
if word_index < total_words:
|
||||||
|
number, word = word_index + 1, words[word_index][1]
|
||||||
|
tk.Label(window, text=f"{number}: {word}", font=("Arial", 12)).pack(pady=5)
|
||||||
|
tk.Button(window, text="认识", command=lambda: next_word(window, word_index +1)).pack(side=tk.LEFT, padx=10, pady=5)
|
||||||
|
tk.Button(window, text="不认识", command=lambda: next_word(window, word_index)).pack(side=tk.RIGHT, padx=1, pady=5)
|
||||||
|
else:
|
||||||
|
tk.Label(window, text="所有单词已显示完毕", font=("Arial", 14, 'bold')).pack()
|
||||||
|
|
||||||
|
def next_word(window, word_index, total_words):
|
||||||
|
window.destroy()
|
||||||
|
new_window = tk.Toplevel()
|
||||||
|
new_window.title("单词学习")
|
||||||
|
display_words(new_window, word_index, total_words)
|
||||||
|
|
||||||
|
# 主程序
|
||||||
|
def main():
|
||||||
|
# 读取单词
|
||||||
|
words = read_words_from_csv('words.csv')
|
||||||
|
window = tk.Tk()
|
||||||
|
window.title("单词学习器")
|
||||||
|
tk.Button(window, text="开始背单词",
|
||||||
|
command=lambda: display_words(tk.Toplevel(), 0, len(words))),
|
||||||
|
pack(pady=10)
|
||||||
|
window.mainloop()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__=='__main__':
|
||||||
|
main()
|
|
Loading…
Reference in new issue