From a732fc342ca58f098be87a3f284cedc0f18e95fd Mon Sep 17 00:00:00 2001 From: gyt200300 <2761891041@qq.com> Date: Thu, 30 May 2024 16:26:08 +0800 Subject: [PATCH] 5-30 --- pythonProject/1.py | 39 --------------------- pythonProject/2.py | 33 ------------------ pythonProject/mysqlconnect.py | 65 ++++++++++++++++++----------------- pythonProject/test1.py | 47 ++++++++++++++++++++----- pythonProject/test2.py | 47 ------------------------- 5 files changed, 72 insertions(+), 159 deletions(-) delete mode 100644 pythonProject/1.py delete mode 100644 pythonProject/2.py delete mode 100644 pythonProject/test2.py diff --git a/pythonProject/1.py b/pythonProject/1.py deleted file mode 100644 index 296f707..0000000 --- a/pythonProject/1.py +++ /dev/null @@ -1,39 +0,0 @@ -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() diff --git a/pythonProject/2.py b/pythonProject/2.py deleted file mode 100644 index c76a83d..0000000 --- a/pythonProject/2.py +++ /dev/null @@ -1,33 +0,0 @@ -# -*- coding: gbk -*- -import pymysql -import pandas as pd -# -*- coding: -*- - - -db_config = { - 'host': 'localhost', - 'port': 3306, - 'user': 'root', - 'password': '21412030117', - 'database': 'word', - 'charset': 'utf8mb4', -} - - -try: - connection = pymysql.connect(**db_config) - cursor = connection.cursor() - extracted='data.csv' - data_to_insert= pd.read_csv(extracted) - - - for item in data_to_insert: - 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}") \ No newline at end of file diff --git a/pythonProject/mysqlconnect.py b/pythonProject/mysqlconnect.py index 99e2509..b3591d2 100644 --- a/pythonProject/mysqlconnect.py +++ b/pythonProject/mysqlconnect.py @@ -1,32 +1,33 @@ -import tkinter as tk -from tkinter import messagebox - -# 单词列表 -words = ["apple", "banana", "cherry", "date", "elderberry"] - -class WordApp(tk.Tk): - def __init__(self): - super().__init__() - 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): - self.know_button = tk.Button(self, text="认识", command=self.show_next_word, width=10) - self.not_know_button = tk.Button(self, text="不认识", command=self.show_next_word, width=10) - self.know_button.pack(side=tk.LEFT, padx=10, pady=10) - self.not_know_button.pack(side=tk.RIGHT, padx=10, pady=10) - - def show_next_word(self): - if self.current_word_index < len(words): - self.word_label.config(text=words[self.current_word_index]) - self.current_word_index += 1 - else: - messagebox.showinfo("结束", "所有单词已学习完毕!") - self.destroy() - -if __name__ == "__main__": - app = WordApp() - app.mainloop() \ No newline at end of file +# -*- coding: gbk -*- +import pymysql +import pandas as pd +# -*- coding: -*- + + +db_config = { + 'host': 'localhost', + 'port': 3306, + 'user': 'root', + 'password': '21412030117', + 'database': 'word', + 'charset': 'utf8mb4', +} + + +try: + connection = pymysql.connect(**db_config) + cursor = connection.cursor() + extracted='words.csv' + data_to_insert= pd.read_csv(extracted) + + + for item in data_to_insert: + 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}") \ No newline at end of file diff --git a/pythonProject/test1.py b/pythonProject/test1.py index 4c5eb7e..291cf9f 100644 --- a/pythonProject/test1.py +++ b/pythonProject/test1.py @@ -1,8 +1,39 @@ -序号,内容 -1,abandon vt.丢弃;放弃,抛弃 -2,ability n.能力;能耐,本领 -3,abnormal a.不正常的;变态的 -4,aboard ad.在船(车)上;上船 -5,abroad ad.(在)国外;到处 -以上的csv文件,创建一个窗口,并设置一个按钮实现记单词的功能,随机依次弹出固定数量的单词,比如一次性弹出五个单词, -将五个单词依次弹出,并在单词下面创建两个按钮,认识和不认识,当点击按钮后显示下一个单词,直到五个单词显示完 \ No newline at end of file +import tkinter as tk +from tkinter import messagebox +import csv + +class WordApp(tk.Tk): + def __init__(self): + super().__init__() + self.title("单词学习") + self.current_word_index = 0 + self.words = self._read_words_from_csv() # 确保方法名正确且定义在类内部 + self.word_label = tk.Label(self, text="", font=("Helvetica", 20)) + self.word_label.pack(pady=20) + self.create_buttons() + + def _read_words_from_csv(self): # 假设这个方法已被定义在这里 + words = [] + with open('words.csv', mode='r', encoding='utf-8') as file: + reader = csv.reader(file) + for row in reader: + words.append(row[1]) # 假设单词在CSV的第一列 + return words + + def create_buttons(self): + self.know_button = tk.Button(self, text="认识", command=self.show_next_word, width=10) + self.not_know_button = tk.Button(self, text="不认识", command=self.show_next_word, width=10) + self.know_button.pack(side=tk.LEFT, padx=10, pady=10) + self.not_know_button.pack(side=tk.RIGHT, padx=10, pady=10) + + def show_next_word(self): + if self.current_word_index < len(self.words): + self.word_label.config(text=self.words[self.current_word_index]) + self.current_word_index += 1 + else: + messagebox.showinfo("结束", "所有单词已学习完毕!") + self.destroy() + +if __name__ == "__main__": + app = WordApp() + app.mainloop() \ No newline at end of file diff --git a/pythonProject/test2.py b/pythonProject/test2.py deleted file mode 100644 index 82e3858..0000000 --- a/pythonProject/test2.py +++ /dev/null @@ -1,47 +0,0 @@ -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() \ No newline at end of file