代码块整理笔记

1、检查文件夹是否存在(不存在创建),在当前文件夹下的指定目录创建txt文件并写入数据:

import os #文件操作相关

root_path = os.path.dirname(os.path.realpath(__file__))+'\\'+'HISTORY_DATA'
if not (os.path.exists(root_path)):
       os.makedirs(root_path)
filename = root_path + '\\' + currtime + '.txt'
with open(filename, 'a') as file_object:
       print(data,file=file_object)

#以日期方式命名,注意':''/'等是windows文件命名的非法字符,命名的时候需要注意
#否则会出现OSError: [Errno 22]Invalid argument:
currtime = time.strftime('%Y%m%d-%Hh%Mm%Ss', time.localtime(time.time()))
#'\'是转义符,在字符串前面加r可以自动将转义字符进行转义
#等价于:'D:\\Users\\JN\\PycharmProjects\\App\\HISTORY_DATA'
root_path=r'D:\Users\JN\PycharmProjects\App\HISTORY_DATA'
filename=root_path+'\\'+currtime+'.txt'

 

2、pyqt5窗体风格

#查看支持的窗体风格:
print(QStyleFactory.keys())
#设置窗体风格:
QApplication.setStyle("Fusion")

3、QTableView单行不允许编辑的方法

#在qtdesigner中设置这个选项就可以了
setEditTriggers(QAbstractItemView::NoEditTriggers);

4、QMessageBox 使用方法

#弹出提示性的窗口
QMessageBox.critical(self,"错误","请检查端口号")
#有选择的
reply = QMessageBox.question(self,
                                     '提示',#标题框名称
                                     "是否要退出系统?",#显示信息
                                     QMessageBox.Yes | QMessageBox.No, #自定义按钮
                                     QMessageBox.Yes) #默认按钮
        if reply == QMessageBox.Yes:
             self.close_all()
             event.accept()
        else:
             event.ignore()

#窗口类型
QMessageBox::information(this, tr("Title"), tr("Content"));

QMessageBox::question(this, tr("Title"), tr("Content"));

QMessageBox::warning(this, tr("Title"), tr("Content"));

QMessageBox::critical(this, tr("Title"), tr("Content"));

QMessageBox::about(this, tr("Title"), tr("Content"));

5、QTableView

参考了很多https://blog.csdn.net/jia666666/article/details/81624259

参考了一些https://blog.csdn.net/cloveses/article/details/80943496

#将表格数据导入到txt文件(当前目录下的某一文件夹)
    def history_to_txt(self):
        r=self.model.rowCount()
        c=self.model.columnCount()
        currtime = time.strftime('%Y%m%d-%Hh%Mm%Ss', time.localtime(time.time()))
        root_path = os.path.dirname(os.path.realpath(__file__))+'\\'+'HISTORY_DATA'
        if not (os.path.exists(root_path)):
            os.makedirs(root_path)
        filename = root_path + '\\' + currtime + '.txt'
        for i in range(r):
            for j in range(c):
                item=self.model.index(i, j)
                with open(filename, 'a') as file_object:
                    print(item.data(),file=file_object)
        about_message='导出已完成,\n文件路径:'+filename
        QMessageBox.about(self,'提示',about_message)

6、TableWidget

没试过,但是感觉写的很好https://www.jianshu.com/p/962b572a216c

7、导出为excel文件(使用Python写入数据到Excel文件中)

https://www.cnblogs.com/beginner-boy/p/7239696.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值