一个用python PyQT写的背单词小程序

主要用到了QGridLayout, QTableWidget

python 复制代码
import sys
import os
import pandas as pd
from PyQt5.QtWidgets import *


class DataFrameExample(QWidget):
	def __init__(self):
		super().__init__()
		self.initUI()

	def initUI(self):
		self.setWindowTitle('DataFrame Example')
		self.setGeometry(100, 100, 800, 400)

		self.layout = QGridLayout()  # 使用网格布局

		# 左侧文本框
		self.text_edit = QTextEdit()
		self.layout.addWidget(self.text_edit, 0, 0, 2, 1)  # 放大文本框所占的行数

		# 中间按钮
		self.button_layout = QVBoxLayout()  # 按钮布局

		self.show_button = QPushButton('Show Next Row')
		self.show_button.clicked.connect(self.showNextRow)
		self.button_layout.addWidget(self.show_button)

		self.explain_button = QPushButton('Show Explain')
		self.explain_button.clicked.connect(self.showExplain)
		self.button_layout.addWidget(self.explain_button)

		self.move_to_table_button = QPushButton('Move to Table')
		self.move_to_table_button.clicked.connect(self.moveToTable)
		self.button_layout.addWidget(self.move_to_table_button)

		self.save_table_button = QPushButton('Save Unknown Word')
		self.save_table_button.clicked.connect(self.save_unknown_words)
		self.button_layout.addWidget(self.save_table_button)

		self.back_button = QPushButton('Back to Last Word')
		self.back_button.clicked.connect(self.back2LastRow)
		self.button_layout.addWidget(self.back_button)

		# 添加一个空白的占位符,使按钮布局竖着排列
		self.button_layout.addStretch()

		self.layout.addLayout(self.button_layout, 0, 1, 2, 1)  # 放大按钮布局所占的行数

		# 右侧表格
		self.table = QTableWidget()
		self.table.setColumnCount(1)
		self.table.setHorizontalHeaderLabels(['Data'])
		self.layout.addWidget(self.table, 0, 2, 2, 1)  # 放大表格所占的行数

		# self.data = pd.DataFrame({'A': range(1, 101), 'B': range(101, 201), 'C': range(201, 301), 'D': range(301, 401)})
		self.data = self.load_data()
		self.row_index = -1

		self.setLayout(self.layout)
		self.show()

	def showNextRow(self):
		self.row_index += 1
		if self.row_index < len(self.data):
			self.text_edit.clear()
			row_data = self.data.iloc[self.row_index, 2]
			self.text_edit.setPlainText(row_data)
			print("word {} : {}".format(self.row_index, row_data))
		else:
			print("learn completed!")

	def back2LastRow(self):
		self.row_index -= 1
		if self.row_index < len(self.data):
			self.text_edit.clear()
			row_data = self.data.iloc[self.row_index, 2]
			self.text_edit.setPlainText(row_data)
			print("word {} : {}".format(self.row_index, row_data))
		else:
			print("error")

	def showExplain(self):
		row_data = self.data.iloc[self.row_index].to_string()
		self.text_edit.setPlainText(row_data)

	def moveToTable(self):
		current_text = self.data.iloc[self.row_index, 2]
		if current_text:
			rowPosition = self.table.rowCount()
			self.table.insertRow(rowPosition)
			newItem = QTableWidgetItem(current_text)
			self.table.setItem(rowPosition, 0, newItem)

		tmp = pd.DataFrame(self.data.iloc[self.row_index, :]).T
		word = tmp.iloc[0, 2]
		if word not in self.df_learn.values:
			self.df_learn = pd.concat([self.df_learn, tmp], ignore_index=True)
			print("{} 加入生词表\n".format(word))

	def load_data(self):
		df = pd.read_excel('/Users/username/Desktop/N1Words.xlsx', sheet_name=0)
		# random_sample = df.sample(n=10, random_state=1)		# 设置随机种子,使结果可重复
		random_sample = df.sample(n=150)

		folder_path = "/Users/username/Desktop"  # 将此路径替换为你要检查的文件夹的实际路径
		# 指定要检查的文件名
		file_name = "unknown_word.xlsx"  # 将此文件名替换为你要检查的文件名
		# 使用 os.path.join() 将文件夹路径和文件名拼接成完整的文件路径
		self.file_path = os.path.join(folder_path, file_name)
		# 使用 os.path.exists() 来检查文件是否存在
		if os.path.exists(self.file_path):
			print(f"文件 '{file_name}' 存在于文件夹 '{folder_path}' 中.")
			self.df_learn = pd.read_excel(self.file_path, sheet_name=0)
		else:
			print(f"文件 '{file_name}' 不存在于文件夹 '{folder_path}' 中.")
			self.df_learn = pd.DataFrame(columns=df.columns)

		return random_sample

	def save_unknown_words(self):
		self.df_learn.to_excel(self.file_path, index=False)
		print("file saved!")


if __name__ == '__main__':
	app = QApplication(sys.argv)
	ex = DataFrameExample()
	sys.exit(app.exec_())
相关推荐
FYKJ_20101 分钟前
springboot游泳馆管理系统79222-计算机课程设计、毕业设计
vue.js·spring boot·后端·python·mysql·django·课程设计
“AI国潮设计-小江”9 小时前
《Python+SDXL实战:用ControlNet精准控制“英歌舞翻糖吐司”构图,附批量生成脚本》
开发语言·人工智能·python·prompt·aigc
wenwen_chao9 小时前
Python学习与开发资源合集:实用技巧与工具推荐
python
省钱兄--zs9 小时前
北京24小时自助健身房系统软件开发实战:从架构设计到部署指南
java·数据仓库·spring boot·小程序·需求分析
for_ever_love__10 小时前
函数——参数、lambda、*args与作用域
python·函数·大模型开发·编程基础
东莞市云毅网络有限公司10 小时前
企业资料时效性判定:Last-Modified、正文日期与版本字段三路交叉
python·sqlite·自动化运维·geo·数据监测
Query*10 小时前
深入浅出LangGraph【一】_基础篇
python·ai·langchain
XiaoMaqqqq11 小时前
市面上正规的IP驱动产业新场景新工具哪家强
运维·python·网络协议·tcp/ip
ShyanZh11 小时前
【Python3基础】19-Socket 与 TCP、UDP 编程
python·网络协议·tcp/ip·udp
Ai-_Man12 小时前
您您这可以把Grok的多个会话比如说。左侧的多个会话一次性导出吗?不是单条会话里面的多次会对话。AI导出鸭
javascript·人工智能·ai·小程序·电脑