当前位置: 首页 > news >正文

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

主要用到了QGridLayout, QTableWidget

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 = -1self.setLayout(self.layout)self.show()def showNextRow(self):self.row_index += 1if 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 -= 1if 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, :]).Tword = 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_sampledef 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_())

http://www.lryc.cn/news/221862.html

相关文章:

  • AutoSAR配置与实践(深入篇)10.1 UDS刷写诊断服务解析(34/36/37服务)
  • 【机器学习】六、概率图模型
  • 机器视觉软件破解的背后是道高一尺,魔高一丈
  • 【I/O流之旅】File类-零基础入门指南
  • ArrayList和LinkedList的区别有哪些?
  • Pyhotn: Mac安装selenium没有chromedriver-114以上及chromedriver无法挪到/usr/bin目录下的问题
  • Java TCP服务端多线程接收RFID网络读卡器上传数据
  • SpringCloud——服务网关——GateWay
  • Linux程序的地址空间
  • Docker安装Minio(稳定版)
  • 大数据毕业设计选题推荐-超级英雄运营数据监控平台-Hadoop-Spark-Hive
  • 视频转码教程:轻松制作GIF动态图,一键高效剪辑操作
  • Seata分布式事务实现原理
  • Rasa NLU中的组件
  • redis笔记 三 redis持久化
  • k8s-----数据存储
  • macOS电池续航工具:Endurance中文
  • 栈(定义,基本操作,顺序存储,链式存储)
  • 在HTML单页面中,使用Bootstrap框架的多选框如何提交数据
  • 当爱好变成职业,会不会就失去了兴趣?
  • 3-知识补充-MVC框架
  • leetcode:141. 环形链表
  • 了解企业邮箱的外观和功能特点
  • 配置阿里云镜像加速器 -docker
  • 11 抽象向量空间
  • 干洗店洗鞋店管理系统app小程序;
  • NOIP2023模拟13联测34 总结
  • Python武器库开发-常用模块之subprocess模块(十九)
  • java验证 Map 的 key、value 是否可以为空
  • 编写MBR主引导记录