# -*- coding: utf-8 -*-
import sip #程序打包需要
import decimal #程序打包需要
from PyQt4 import QtCore, QtGui
from PyQt4 import QtWebKit
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
from PyQt4.QtWebKit import QWebView, QWebPage, QWebSettings
from PyQt4 import QtWebKit
QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))
class MyTool(QMainWindow):
def createActions(self):
self.fileOpenAction=QAction(QIcon("./image/fileopen.png"),self.tr("打开"),self)
self.fileOpenAction.setShortcut("Ctrl+O")
self.fileOpenAction.setStatusTip(self.tr("打开一个文件"))
self.connect(self.fileOpenAction,SIGNAL("triggered()"),self.slotOpenFile)
self.fileNewAction=QAction(QIcon("./image/filenew.png"),self.tr("新建"),self)
self.fileNewAction.setShortcut("Ctrl+N")
self.fileNewAction.setStatusTip(self.tr("新建一个文件"))
self.connect(self.fileNewAction,SIGNAL("triggered()"),self.slotNewFile)
self.fileSaveAction=QAction(QIcon("./image/filesave.png"),self.tr("保存"),self)
self.fileSaveAction.setShortcut("Ctrl+S")
self.fileSaveAction.setStatusTip(self.tr("保存文件"))
self.connect(self.fileSaveAction,SIGNAL("triggered()"),self.slotSaveFile)
self.exitAction=QAction(QIcon("./image/filequit.png"),self.tr("退出"),self)
self.exitAction.setShortcut("Ctrl+Q")
self.setStatusTip(self.tr("退出"))
self.connect(self.exitAction,SIGNAL("triggered()"),self.close)
self.cutAction=QAction(QIcon("./image/editcut.png"),self.tr("剪切"),self)
self.cutAction.setShortcut("Ctrl+X")
self.cutAction.setStatusTip(self.tr("剪切到粘贴板"))
#self.connect(self.cutAction,SIGNAL("triggered()"),self.text.cut)
self.copyAction=QAction(QIcon("./image/editcopy.png"),self.tr("复制"),self)
self.copyAction.setShortcut("Ctrl+C")
self.copyAction.setStatusTip(self.tr("复制到粘贴板"))
#self.connect(self.copyAction,SIGNAL("triggered()"),self.text.copy)
self.pasteAction=QAction(QIcon("./image/editpaste.png"),self.tr("粘贴"),self)
self.pasteAction.setShortcut("Ctrl+V")
self.pasteAction.setStatusTip(self.tr("粘贴内容到当前处"))
#self.connect(self.pasteAction,SIGNAL("triggered()"),self.text.paste)
self.aboutAction=QAction(self.tr("关于"),self)
self.connect(self.aboutAction,SIGNAL("triggered()"),self.slotAbout)
def slotNewFile(self):
newWin=MainWindow()
newWin.show()
def slotOpenFile(self):
fileName=QFileDialog.getOpenFileName(self)
if fileName.isEmpty()==False:
if self.text.document().isEmpty():
self.loadFile(fileName)
else:
newWin=MainWindow()
newWin.show()
newWin.loadFile(fileName)
def loadFile(self,fileName):
file=QFile(fileName)
if file.open(QIODevice.ReadOnly|QIODevice.Text):
textStream=QTextStream(file)
while textStream.atEnd()==False:
self.text.append(textStream.readLine())
def slotSaveFile(self):
pass
def slotAbout(self):
QMessageBox.about("about me",self.tr("这是我们的第一个例子"))
def createToolBars(self):
fileToolBar=self.addToolBar("File")
fileToolBar.addAction(self.fileNewAction)
fileToolBar.addAction(self.fileOpenAction)
fileToolBar.addAction(self.fileSaveAction)
editTool=self.addToolBar("Edit")
editTool.addAction(self.copyAction)
editTool.addAction(self.cutAction)
editTool.addAction(self.pasteAction)
def createMenus(self):
fileMenu=self.menuBar().addMenu(self.tr("文件"))
fileMenu.addAction(self.fileNewAction)
fileMenu.addAction(self.fileOpenAction)
fileMenu.addAction(self.fileSaveAction)
fileMenu.addAction(self.exitAction)
editMenu=self.menuBar().addMenu(self.tr("编辑"))
editMenu.addAction(self.copyAction)
editMenu.addAction(self.cutAction)
editMenu.addAction(self.pasteAction)
aboutMenu=self.menuBar().addMenu(self.tr("帮助"))
aboutMenu.addAction(self.aboutAction)
class MainWeb(QtGui.QMainWindow):
def __init__(self, url):
QtGui.QMainWindow.__init__(self)
self.sb=self.statusBar()
self.pbar = QtGui.QProgressBar()
self.pbar.setMaximumWidth(120)
self.wb=QtWebKit.QWebView(loadProgress = self.pbar.setValue, loadFinished = self.pbar.hide, loadStarted = self.pbar.show, titleChanged = self.setWindowTitle)
self.setCentralWidget(self.wb)
self.tb=self.addToolBar("Main Toolbar")
for a in (QtWebKit.QWebPage.Back, QtWebKit.QWebPage.Forward, QtWebKit.QWebPage.Reload):
self.tb.addAction(self.wb.pageAction(a))
self.url = QtGui.QLineEdit(returnPressed = lambda:self.wb.setUrl(QtCore.QUrl.fromUserInput(self.url.text())))
self.tb.addWidget(self.url)
self.wb.urlChanged.connect(lambda u: self.url.setText(u.toString()))
self.wb.urlChanged.connect(lambda: self.url.setCompleter(QtGui.QCompleter(QtCore.QStringList([QtCore.QString(i.url().toString()) for i in self.wb.history().items()]), caseSensitivity = QtCore.Qt.CaseInsensitive)))
self.wb.statusBarMessage.connect(self.sb.showMessage)
self.wb.page().linkHovered.connect(lambda l: self.sb.showMessage(l, 3000))
self.search = QtGui.QLineEdit(returnPressed = lambda: self.wb.findText(self.search.text()))
self.search.hide()
self.showSearch = QtGui.QShortcut("Ctrl+F", self, activated = lambda: (self.search.show() , self.search.setFocus()))
self.hideSearch = QtGui.QShortcut("Esc", self, activated = lambda: (self.search.hide(), self.wb.setFocus()))
self.quit = QtGui.QShortcut("Ctrl+Q", self, activated = self.close)
self.zoomIn = QtGui.QShortcut("Ctrl++", self, activated = lambda: self.wb.setZoomFactor(self.wb.zoomFactor()+.2))
self.zoomOut = QtGui.QShortcut("Ctrl+-", self, activated = lambda: self.wb.setZoomFactor(self.wb.zoomFactor()-.2))
self.zoomOne = QtGui.QShortcut("Ctrl+=", self, activated = lambda: self.wb.setZoomFactor(1))
self.wb.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled, True)
self.sb.addPermanentWidget(self.search)
self.sb.addPermanentWidget(self.pbar)
self.wb.load(url)
class MainWindow(QMainWindow):
def __init__(self,parent=None):
super(MainWindow,self).__init__(parent)
self.setWindowTitle(self.tr("钰晴管理平台"))
app.setWindowIcon(QIcon("./image/icon.png"))
#第一个tal
self.tabWidget = QTabWidget(self)
#画一个浏览器
self.verticalLayoutWidget = QtGui.QWidget()
self.verticalLayoutWidget.setGeometry(QtCore.QRect(9, 50, 971, 761))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setObjectName("verticalLayout")
url = QtCore.QUrl(‘http://www.baidu.com‘)
wb=MainWeb(url)
self.verticalLayout.addWidget(wb)
#加到tab中
self.tabWidget.addTab(self.verticalLayoutWidget, self.tr("欢迎使用"))
self.tabWidget.setTabsClosable(True)
self.connect(self.tabWidget,SIGNAL("tabCloseRequested(int)"),self.removeTab);
#加到一个区块中
self.stack=QStackedWidget()
self.stack.setFrameStyle(QFrame.Panel|QFrame.Raised)
self.stack.addWidget(self.tabWidget)
self.setCentralWidget(self.stack)
#菜单
toolbar = MyTool()
toolbar.createActions()
toolbar.createMenus()
toolbar.createToolBars()
#停靠窗口1
dock1=QDockWidget(self.tr("停靠窗口1"),self)
dock1.setFeatures(QDockWidget.DockWidgetMovable)
dock1.setAllowedAreas(Qt.LeftDockWidgetArea|Qt.RightDockWidgetArea)
te1=QTextEdit(self.tr("窗口1,可在Main Window的左部和右部停靠,不可浮动,不可关闭"))
dock1.setWidget(te1)
self.addDockWidget(Qt.RightDockWidgetArea,dock1)
#停靠窗口2
dock2=QDockWidget(self.tr("停靠窗口2"),self)
dock2.setFeatures(QDockWidget.DockWidgetFloatable|QDockWidget.DockWidgetClosable)
te2=QTextEdit(self.tr("窗口2,只可浮动"))
dock2.setWidget(te2)
self.addDockWidget(Qt.RightDockWidgetArea,dock2)
#停靠窗口3
dock3=QDockWidget(self.tr("停靠窗口3"),self)
dock3.setFeatures(QDockWidget.AllDockWidgetFeatures)
te3=QTextEdit(self.tr("窗口3,可在Main Window任意位置停靠,可浮动,可关闭"))
dock3.setWidget(te3)
self.addDockWidget(Qt.BottomDockWidgetArea,dock3)
listWidget_1=QListWidget()
listWidget_1.insertItem(0,self.tr("个人基本资料"))
listWidget_1.insertItem(1,self.tr("联系方式"))
listWidget_1.insertItem(2,self.tr("详细信息"))
groupbox1=QGroupBox()
vlayout1=QVBoxLayout(groupbox1)
vlayout1.setMargin(10)
vlayout1.setAlignment(Qt.AlignCenter)
vlayout1.addWidget(listWidget_1)
vlayout1.addStretch()
self.listWidget_2=QListWidget()
self.listWidget_2.insertItem(0,self.tr("个人基本资料"))
self.listWidget_2.insertItem(1,self.tr("联系方式"))
self.listWidget_2.insertItem(2,self.tr("详细信息"))
groupbox2=QGroupBox()
vlayout2=QVBoxLayout(groupbox2)
vlayout2.setMargin(10)
vlayout2.setAlignment(Qt.AlignCenter)
vlayout2.addWidget(self.listWidget_2)
toolbar = QToolBox()
toolbar.addItem(groupbox1,self.tr("我的好友"))
toolbar.addItem(groupbox2,self.tr("黑名单"))
#self.connect(listWidget_1,SIGNAL("currentItemChanged(QListWidgetItem,QListWidgetItem"),
# self.changePage)
self.connect(listWidget_1,SIGNAL("currentRowChanged(int)"),self.textDialog)
#self.connect(self.stack,SIGNAL("currentChanged(int)"),self.fadeInWidget)
#停靠窗口4
dock4=QDockWidget(self.tr("停靠窗口3"),self)
dock4.setFeatures(QDockWidget.AllDockWidgetFeatures)
dock4.setWidget(toolbar)
self.addDockWidget(Qt.LeftDockWidgetArea,dock4)
QThread.sleep(1)
def search(self):
address = str(self.myurl.text())
if address:
if address.find(‘://‘) == -1:
address = ‘http://‘ + address
self.myurl.setText(address)
self.myweb.load(QUrl(address))
def removeTab(self, index):
if index != -1:
self.tabWidget.removeTab(index)
def textDialog(self,index):
self.add_tab(QCalendarWidget(), title =u‘新增Tab‘, index = index )
#QMessageBox.information(self,"Information",
# self.tr("填写任意想告诉于用户的信息!"))
def add_tab(self, widget, title, index=None):
try:
if index:
inserted_index = self.tabWidget.insertTab(index, widget, self.tr(title))
else:
inserted_index = self.tabWidget.addTab(widget, self.tr(title))
self.tabWidget.setCurrentIndex(inserted_index)
widget.setFocus()
return inserted_index
except AttributeError, reason:
print reason
print("Widget couldn‘t be added, doesn‘t inherit from ITabWidget")
app=QApplication(sys.argv)
splash=QSplashScreen(QPixmap("./1.jpg"))
splash.show()
app.processEvents()
main=MainWindow()
main.show()
splash.finish(main)
app.connect(app, SIGNAL("lastWindowClosed()")
, app
, SLOT("quit()")
)
app.exec_()