日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python Qt GUI快速编程第六章代码分析

發布時間:2025/3/19 python 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python Qt GUI快速编程第六章代码分析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這是我敲的Python Qt Gui快速編程第六章上的代碼,以后可能要用到其中一些,如果忘記了就來這里看看。 #coding=utf-8 #d import os import platform import sys from PyQt4.QtGui import * from PyQt4.QtCore import * __version__= "1.0.0"class MainWindow(QMainWindow):def fileNew(self):print 1def addActions(self, target,actions):for action in actions:if action is None:target.addSeparator()else :target.addAction(action)def createAction(self,text,slot=None,shortcut=None,icon=None,tip=None,checkable=False,signal="triggered()"):action = QAction(text,self)if icon is not None:action.setIcon(QIcon("web.png"))if shortcut is not None:action.setShortcut(shortcut)if tip is not None:action.setToolTip(tip)action.setStatusTip(tip)if slot is not None:self.connect(action,SIGNAL(signal),slot)if checkable:action.setCheckable(True)return actiondef __init__(self,parent=None):super(MainWindow, self).__init__(parent)self.image = QImage()self.dirty = False #作為標志用來說明圖片是否未保存修改。self.filename = None #作為標志,用來說明是沒有圖片還是有尚未保存的新創建圖片。self.mirroredvertically = False #在實現鏡像的時候會用到。self.mirroredhorizontally = False#設置主窗口部件self.imageLabel = QLabel("123456")self.imageLabel.setMinimumSize(200,200)self.imageLabel.setAlignment(Qt.AlignCenter)#設置圖片水平和垂直居中對齊。self.imageLabel.setContextMenuPolicy(Qt.ActionsContextMenu) #查閱了API文檔,明白了self.setCentralWidget(self.imageLabel) #查閱了 API文檔,明白了這個方法可以對主窗口中心區域的窗口部件進行布局,也可以重定義該窗口的父對象#設置停靠窗口,logDockWidget = QDockWidget("Log", self) #停靠窗口不能放進布局中,所以除了窗口標題外,還要給定父對象。logDockWidget.setObjectName("LogDockWidget")logDockWidget.setAllowedAreas(Qt.LeftDockWidgetArea|#用這個方法限制其停靠區域Qt.RightDockWidgetArea)self.listWidget = QListWidget() #創建列表窗口logDockWidget.setWidget(self.listWidget) #將列表窗口放進停靠窗口中self.addDockWidget(Qt.RightDockWidgetArea, logDockWidget) #將停靠窗口添加到主窗口中#設置狀態欄信息self.sizeLabel = QLabel("456")self.sizeLabel.setFrameStyle(QFrame.StyledPanel|QFrame.Sunken)status = self.statusBar()status.setSizeGripEnabled(False)#status.addPermanentWidget(self.sizeLabel,0) #在查閱了API文檔之后,明白了這個意思status.showMessage("Ready",10000)#創建各個動作,File旗下的和Edit旗下的fileNewAction = self.createAction("&New",self.fileNew,QKeySequence.New,"web","Create an image file")fileOpenAction = self.createAction("&Open",self.fileNew,"Ctrl+O","web","open an image file")editZoomAction = self.createAction("&Zoom",self.fileNew,"Alt+Z","web","Zoom the image")editInvertAction = self.createAction("&Invert",self.fileNew,"Ctrl+I","web","Invert the image's colors",True,"toggled(bool)")editSwapAndBlue = self.createAction("Sw&ap Red and Blue",self.fileNew,"Ctrl+A","web","Swap the colors of red and blue",True,"toggled(bool)")mirrorGroup = QActionGroup(self)editUnMirrorAction = self.createAction("&Unmirror",self.fileNew,"Ctrl+U","web","Unmirror the image",True,"toggled(bool)")mirrorHorizon = self.createAction("Mirror &Horizon",self.fileNew,"Ctrl+H","web","Mirror horizon the image",False,"toggled(bool)")mirrorVertical = self.createAction("Mirror &Vertical",self.fileNew,"Ctrl+V","web","Mirror vetrical the image",False,"toggled(bool)")mirrorGroup.addAction(editUnMirrorAction)mirrorGroup.addAction(mirrorHorizon)mirrorGroup.addAction(mirrorVertical)menubar = self.menuBar()fileMenu = menubar.addMenu('&File')fileMenu.addAction(fileNewAction)editMenu = menubar.addMenu("&Edit")self.addActions(editMenu,(editInvertAction,editSwapAndBlue,editZoomAction))mirrorMenu = editMenu.addMenu(QIcon("web"),"&Mirror")self.addActions(mirrorMenu,(editUnMirrorAction,mirrorHorizon,mirrorVertical))fileToolbar = self.addToolBar("file") #file是這個工具欄的標題fileToolbar.setObjectName("FileToolBar") #FileToolBar是這個工具欄對象的名字self.addActions(fileToolbar,(fileNewAction,fileOpenAction))editToolbar = self.addToolBar("Edit")editToolbar.setObjectName("EditToolBar")self.addActions(editToolbar,(editInvertAction,editSwapAndBlue,editUnMirrorAction,mirrorHorizon,mirrorVertical))self.zoomSpinBox = QSpinBox()self.zoomSpinBox.setRange(1,400)self.zoomSpinBox.setSuffix("%")self.zoomSpinBox.setValue(100)self.zoomSpinBox.setToolTip("Zoom the image")self.zoomSpinBox.setStatusTip(self.zoomSpinBox.toolTip())#self.zoomSpinBox.setFocusPolicy(Qt.NoFocus)self.connect(self.zoomSpinBox,SIGNAL("valueChanged(int)"),self.showImage)editToolbar.addWidget(self.zoomSpinBox)self.addActions(self.imageLabel,(editInvertAction,editSwapAndBlue,editUnMirrorAction,mirrorVertical,mirrorHorizon))def showImage(self):print 2app = QApplication(sys.argv) mainwindow = MainWindow() mainwindow.show() app.exec_()
1.action = QAction(text,self)-----------第21句 每個QObject子類都要有一個父對象(除了頂級窗口),對于窗口部件來說,它們可以通過布局來獲得父對象,而對于純粹的數據對象比如QAction來說,則必須明確地規定其父對象——self 像Qimage不是QObject的子類,所以可以直接:sekf.image=QImage()--------第36句 2.self.setCentralWidget(self.imageLabel)----------第47句 經參考API文檔:

The widget argument has it's ownership transferred to Qt.

Sets the given widget to be the main window's central widget.

意思就是括號里面的參數窗口部件的所有權給了MainWindow,就是相當于給它設置了父對象,同時還將imageLabel放在父窗口中間。

3.self.imageLabel.setContextMenuPolicy(Qt.ActionsContextMenu)---------第46句

查得文檔,它是這樣說的,如果賦予這個參數,那么該窗口將會把該窗口所擁有的動作用在它的右菜單中。

4.logDockWidget = QDockWidget("Log", self) --------------第50句

QdockWidget是停靠窗口類,停靠窗口是不能加入布局的,所以這里需要給它指明父對象。

5.logDockWidget.setWidget(self.listWidget)-------第55句self.addDockWidget(Qt.RightDockWidgetArea, logDockWidget)-------第56句給停靠窗口里面添加窗口需要用setWidget(),其原型為:def setWidget(self, QWidget)給主窗口里面添加停靠窗口需要用addDockWidget,其原型為:def addDockWidget(self, Qt_DockWidgetArea, QDockWidget, Qt_Orientation=None) 6.status.setSizeGripEnabled(False)--------第62句

這個屬性保存的是在狀態條右下方的QSizeGrip是否有效。

可以讓狀態條右下方的QSizeGrip生效或者失效。默認它是生效的。

通過setSizeGripEnabled()設置屬性值并且通過isSizeGripEnabled()來獲得屬性值。

7.status.addPermanentWidget(self.sizeLabel,0)-------第63句

查閱參考文檔:

The widget argument has it's ownership transferred to Qt.

Adds the given widget permanently to this status bar, reparenting the widget if it isn't already a child of this QStatusBar object. The stretch parameter is used to compute a suitable size for the given widget as the status bar grows and shrinks. The default stretch factor is 0, i.e giving the widget a minimum of space.

Permanently means that the widget may not be obscured by temporary messages. It is is located at the far right of the status bar.

意思就是將self.sizeLabel的父對象設置成status,并且如果self.sizeLabel不是QStatusBar的子類的話,那么就重新排列結構。

后面的Int型參數代表占用status bar的相對位置大小,其中permanently意味著該窗口不會被臨時信息覆蓋,該窗口呆在狀態欄最右邊。

8.status.showMessage("Ready",10000)-----第64句。

后面的參數表示該信息在狀態欄顯示的時間,10000ms就是10s。

9.mirrorGroup = QActionGroup(self)-----第73句。

該句創建了一個動作群組的對象,同上,需要給它指定一個父對象。

動作群組可以管理一系列的可選型動作,并可確保它所管理的動作只要有一個是開的,那么其他的動作全為關的。

10.menubar = self.menuBar()----第83句

同創建狀態欄一樣,第一次調用menuBar的時候也會創建菜單欄。

menuBar()函數屬于QMainWindow這個類,其原型為:def menuBar(self),返回值類型為:menuBar(self) -> QMenuBar

statusBar()函數屬于QMainWindow這個類,其原型為:def statusBar(self),返回值類型為:

statusBar(self) -> QStatusBar 11.editMenu = menubar.addMenu("&Edit")----第86句mirrorMenu = editMenu.addMenu(QIcon("web"),"&Mirror")-----第89句self.addActions(mirrorMenu,(editUnMirrorAction,mirrorHorizon,mirrorVertical))---第90句第一句表示給主菜單里面添加“Edit”這個選項。第二句表示給editMenu里面添加一個子菜單。 第三句表示給子菜單里面添加動作。一二兩句同樣是addMenu這個函數,但是所屬的類是不同的,第一個函數屬于的類是QMenuBar,第二個函數屬于QMenu。 12.fileToolbar = self.addToolBar("file")-----第92句 主窗口里面添加工具欄利用addToolBar這個函數, 該句的原型addToolBar(self, QString) -> QToolBar,這個QString是該工具欄的標題 13.fileToolbar.setObjectName("FileToolBar")-----第93句該句給工具欄對象設置了名字,其作用是幫助PyQt區分多個數量的工具欄就像停靠窗口一樣,logDockWidget.setObjectName("LogDockWidget")----第51句 14.self.addActions(editToolbar,(editInvertAction,editSwapAndBlue,editUnMirrorAction,----第97句mirrorHorizon,mirrorVertical))給工具欄添加動作。QWidget類都有一個addAction方法,可被QMenu,QMenuBar,QToolBar類繼承,這就是為什么可以向這些類添加動作的原因 15.editToolbar.addWidget(self.zoomSpinBox)----109句查閱文檔:

The widget argument has it's ownership transferred to Qt.

Adds the given widget to the toolbar as the toolbar's last item.

The toolbar takes ownership of widget.

利用完此方法后,該選值框的父對象設定為工具欄,并把該選值框放到工具欄的最后一項。

16.self.addActions(self.imageLabel,(editInvertAction,editSwapAndBlue,editUnMirrorAction,mirrorVertical,mirrorHorizon))

將動作添加到imageLabel中,

self.imageLabel.setContextMenuPolicy(Qt.ActionsContextMenu)----第46句該句的右菜單為該imageLabel的動作。

所以在imageLabel中右鍵,則會彈出該窗口所有的動作指令。另外,只要動作里面沒有None就可以這樣執行,

因為QWidget類里面沒有addSeparator這個方法。

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的Python Qt GUI快速编程第六章代码分析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。