PyQt4 模拟记事本基本功能(保存,打开文件)

完成功能:
1. 默认【保存】按钮enable
2. 修改文本的内容后,【enable】
3. 解决字符乱码问题:utf-8
4. 提示:如果修改了文件没有保存的时候,又尝试打开新的文件,给出相关的提示信息
操作步骤:
1. 在Designer中设计,生产**.ui文件
2. 切换到.ui文件所在目录,运行如下命令,生成.py文件
pyuic4 **.ui > **.py
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui
from edytor import Ui_notepad
from os.path import isfile
import codecs class StartQt4(QtGui.QMainWindow):
def __init__(self, parent=None, flags=0):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_notepad()
self.ui.setupUi(self)
''' click点击 button_open 执行file_dialog'''
QtCore.QObject.connect(self.ui.button_open, QtCore.SIGNAL("clicked()"), self.file_dialog)
''' click点击 button_save 执行file_save保存'''
QtCore.QObject.connect(self.ui.button_save, QtCore.SIGNAL("clicked()"), self.file_save)
''' editor_window的内容发送改变时,执行enable_save函数,enable = true'''
QtCore.QObject.connect(self.ui.editor_window, QtCore.SIGNAL("textChanged()"),self.enable_save ) def file_dialog(self):
response = False
# buttton texts
SAVE = 'Save'
DISCARD = 'Discard'
CANCEL = 'Cancel' # if we have changes then ask about them
if self.ui.button_save.isEnabled() and self.filename:
message = QtGui.QMessageBox(self)
message.setText('What to do about unsaved changes ?')
message.setWindowTitle('Notpad')
message.setIcon(QtGui.QMessageBox.Question)
message.addButton(SAVE, QtGui.QMessageBox.AcceptRole)
message.addButton(DISCARD, QtGui.QMessageBox.DestructiveRole)
message.addButton(CANCEL, QtGui.QMessageBox.RejectRole)
message.setDetailedText('Unsaved changes in file: '+ str(self.filename))
message.exec_()
response = message.clickedButton().text() # save file
if response == SAVE:
self.file_save()
self.ui.button_save.setEnabled(False) # descard changes
elif response == DISCARD:
self.ui.button_save.setEnabled(False) # if we didn't cancelled show the file dialogue if response != CANCEL:
''' 使用QFileDialog来选择文件'''
fd = QtGui.QFileDialog(self)
''' 使用getOpenFileName()弹出一个文件选择框
fd.getOpenFileName()用于返回我们选择文件的名字。如果没有选择文件的话,会得打一个空的文件名
'''
self.filename = fd.getOpenFileName()
if isfile(self.filename):
text = codecs.open(self.filename, 'r', 'utf-8').read()
self.ui.editor_window.setPlainText(text)
self.ui.button_save.setEnabled(False)
'''文本没有被保存的时候,显示提示消息 —— 通过save是否被禁用得到文本是否被保存''' def file_save(self):
if isfile(self.filename):
file = codecs.open(self.filename, 'w', 'utf-8')
file.write(unicode(self.ui.editor_window.toPlainText()))
file.close()
self.ui.button_save.setEnabled(False) def enable_save(self):
self.ui.button_save.setEnabled(True) if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQt4()
myapp.show()
sys.exit(app.exec_())
PyQt4 模拟记事本基本功能(保存,打开文件)的更多相关文章
- python2.7+pyqt4实现记事本基本功能
记事本程序: # coding:utf-8 import sys from PyQt4.QtGui import QMainWindow from PyQt4.QtGui import QApplic ...
- Xftp设置指定记事本(notepad++)打开文件
右键
- Linux内核分析:打开文件描述符实现
在Linux中每一个进程的数据是存储在一个task_struct结构(定义在sched.h中)中的. struct task_struct { volatile long state; /* -1 u ...
- WebBrowser自动填充打开文件对话框
WebBrowser自动填充打开文件对话框 在使用WebBrowser编写自动表单填写软件的时候,不知道大家是否遇到国填写文件选择表单的情况.遇到这种情况的时候,无法直接队Html元素赋值,必须模 ...
- linux 文件描述符表 打开文件表 inode vnode
在Linux中,进程是通过文件描述符(file descriptors,简称fd)而不是文件名来访问文件的,文件描述符实际上是一个整数.Linux中规定每个进程能最多能同时使用NR_OPEN个文件 ...
- C#打开文件
C#中经常用到的功能,打开文件: /// <summary> /// 打开文件,可选择多个文件 /// </summary> /// <param name=" ...
- 用汇编语言给XP记事本添加“自动保存”功能 good
[文章标题]: 用汇编语言给XP记事本添加“自动保存”功能 [文章作者]: newjueqi [作者邮箱]:zengjiansheng1@126.com [作者QQ]:190678908 [使用工具] ...
- java中文件保存、打开文件对话框
package com.soft.test; //AWT: FileDialog类 + FilenameFilter类 可以实现本功能 //Swing: JFileChooser类 + FileFil ...
- CFileDialog 打开文件夹文件 保存文件夹文件
格式说明: explicit CFileDialog( BOOL bOpenFileDialog, //TRUE 为打开, FALSE 为保存 L ...
随机推荐
- Java后台实现方法
Java后台实现方法 首先后台结构分为四个部分(以表schedule为例) entity>mapper>service>controller 1. 在entity里面写好实体,新建目 ...
- B. Order Book(Codeforces Round #317 )
B. Order Book time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- java多线程编程核心技术——第六章总结
目录 1.0立即加载/"饿汉式" 2.0延迟加载/"懒汉式" 3.0使用静态内置类实现单例模式 4.0序列化与反序列化的单例模式实现 5.0使用static代码 ...
- java开发收藏
一.java工具 1.jenkins 项目集成工具 2.launch4j java打包成exe工具 二.json库 1.jsoniter 比以下都快 2.dsljson 3.fastjson 4.gs ...
- Natas Wargame Level27 Writeup(SQL表的注入/溢出与截取)
前端: <html> <head> <!-- This stuff in the header has nothing to do with the level --&g ...
- C#:将.csv格式文件转换成.xlsx格式文件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 让你高效的理解JavaScript中的同步、异步和事件循环
"同步请求","异步请求"相信这两词在程序猿的世界中频频出现,到底是词性的妖娆,还是撸代码的基础要求,下面直接分享本人学习的好东西,保证让你深入浅出,爽得不要不 ...
- TypeScript学习笔记之类
TypeScript的类,简单地定义如下: class Person { x: number; // 默认为public类型 y: number; constructor(x1: number, y1 ...
- 《认知与设计:理解UI设计准则》【PDF】下载
<认知与设计:理解UI设计准则>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382276 内容介绍 <图灵交互设计丛书·认 ...
- 函数的非固定参数-Day3
一.函数非固定参数 1.默认函数,我们在传参之前,选给参数指定一个默认的值.默认参数特点是非必须传递的. def test(x,y=2): print(x) print(y) print(" ...