[Pyside6-Study] Chapter-3
1.创建主窗口
在第一步,我们将会使用子类QMainWindow创建主窗口,QMainWindow的构造函数与QWidget类类似;
PySide6.QtWidgets.QMainWindow([parent=None[,flags=0]])
下面的代码展示如何创建最基础的主窗口程序;
# Import required modules import sys from PySide6.QtWidgets import QApplication, QMainWindow class MainWindow(QMainWindow): """ Our Main Window Class """ def __init__(self): """ Constructor Fucntion """ QMainWindow.__init__(self) self.setWindowTitle("Main Window") self.setGeometry(300, 250, 400, 300) if __name__ == '__main__': # Exception Handling try: myApp = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.show() myApp.exec() sys.exit(0) except NameError: print("Name Error:", sys.exc_info()[1]) except SystemExit: print("Closing Window...") except Exception: print(sys.exc_info()[1])
以下是个人习惯
import sys from PySide6.QtWidgets import QApplication, QMainWindow,QLabel,QStatusBar class Main: def __init__(self) -> None: self.app = QApplication(sys.argv) self.win = QMainWindow() def GUI(self): self.win.setGeometry(300, 250, 400, 300) QLabel(self.win,text="bili:崩析") myStatusBar = QStatusBar() myStatusBar.showMessage('Ready',2000) self.win.setStatusBar(myStatusBar) def run(self): self.GUI() self.win.show() self.app.exec() if __name__=="__main__": xe = Main() xe.run()
2.状态栏
状态栏是一个水平的信息区域,通常位于GUI窗口的底部。
它的主要任务是显示有关窗口当前状态的信息。
状态栏还可以分为几个部分,每个部分向用户显示不同的信息。
在PySide中,状态栏可以通过调用函数添加到QMainWindow类中
QMainWindow.setStatusBar(状态栏)。它接受PySide6.QtWidgets的对象。 QStatusBar作为参数。 状态栏的属性由该类定义,并返回该类的一个对象来设置状态栏。 将此参数设置为0将从主窗口中删除状态栏。 状态栏可以显示以下三种信息:- Temporary:简要占据状态栏的大部分,主要用于解释工具提示文本、菜单项等
- Normal:占用状态栏的一部分,可能被临时消息暂时隐藏,用于显示当前窗口信息、页面号、行号等
- Permanent:通常占用空间较小,用于指示重要的模式信息、大写锁定指示灯、拼写检查信息等
创建一个QStatusBar对象并设置参数 ,然后把这个对象使用setStatusBar函数设置到主窗口上:
此状态信息会在两秒后消失;
def CreateStatusBar(self): """ Function to create Status Bar """ self.myStatusBar = QStatusBar() self.myStatusBar.showMessage('Ready', 2000) self.setStatusBar(self.myStatusBar)
All_Code:
# Import required modules import sys from PySide6.QtWidgets import QApplication, QMainWindow,QStatusBar class MainWindow(QMainWindow): """ Our Main Window Class """ def __init__(self): """ Constructor Fucntion """ QMainWindow.__init__(self) self.setWindowTitle("Main Window") self.setGeometry(300, 250, 400, 300) def CreateStatusBar(self): """ Function to create Status Bar """ self.myStatusBar = QStatusBar() self.myStatusBar.showMessage('Ready', 2000) self.setStatusBar(self.myStatusBar) if __name__ == '__main__': # Exception Handling try: myApp = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.CreateStatusBar() mainWindow.show() myApp.exec() sys.exit(0) except NameError: print("Name Error:", sys.exc_info()[1]) except SystemExit: print("Closing Window...") except Exception: print(sys.exc_info()[1])
如果想要进度条显示在状态栏;只需要做如下修改;
def __init__(self): ... self.setGeometry(300, 250, 400, 300) self.statusLabel = QLabel('Showing Progress') self.progressBar = QProgressBar() self.progressBar.setMinimum(0) self.progressBar.setMaximum(100) ...
def CreateStatusBar(self): """ Function to create Status Bar """ self.myStatusBar = QStatusBar() self.progressBar.setValue(10)#新增 self.myStatusBar.addWidget(self.statusLabel, 1)#新增 self.myStatusBar.addWidget(self.progressBar, 2)#新增 self.setStatusBar(self.myStatusBar)
现所有代码如下:
# Import required modules import sys from PySide6.QtWidgets import QApplication, QMainWindow,QStatusBar,QLabel,QProgressBar class MainWindow(QMainWindow): """ Our Main Window Class """ def __init__(self): """ Constructor Fucntion """ QMainWindow.__init__(self) self.setWindowTitle("Main Window") ... self.setGeometry(300, 250, 400, 300) self.statusLabel = QLabel('Showing Progress') self.progressBar = QProgressBar() self.progressBar.setMinimum(0) self.progressBar.setMaximum(100) ... def CreateStatusBar(self): """ Function to create Status Bar """ self.myStatusBar = QStatusBar() self.progressBar.setValue(10)#新增 self.myStatusBar.addWidget(self.statusLabel, 1)#新增 self.myStatusBar.addWidget(self.progressBar, 2)#新增 self.setStatusBar(self.myStatusBar) if __name__ == '__main__': # Exception Handling try: myApp = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.CreateStatusBar() mainWindow.show() myApp.exec() sys.exit(0) except NameError: print("Name Error:", sys.exc_info()[1]) except SystemExit: print("Closing Window...") except Exception: print(sys.exc_info()[1])
效果如下
3.菜单栏
为了体现状态栏,使用一个控件占据中央位置;
def SetupComponents(self): """ Setting the central widget """ textEdit = QTextEdit() self.setCentralWidget(textEdit)
添加菜单栏
使用QMenuBar创建一个菜单栏;
使用QMainWindow函数.menuBar().addMenu()添加菜单按钮,返回按钮对象;
def CreateMenus(self): """ Function to create actual menu bar """ self.fileMenu = self.menuBar().addMenu("&File") self.editMenu = self.menuBar().addMenu("&Edit") self.helpMenu = self.menuBar().addMenu("&Help")
创建动作QAction内容,其中triggered为调用函数,statusTip信息显示在状态栏里;
def CreateActions(self): """ Function to create actions for menus """ self.newAction = QAction( QIcon('new.png'), '&New',self, shortcut=QKeySequence.New,statusTip="Create a New File",triggered=self.newFile) self.exitAction = QAction( QIcon('exit.png'), 'E&xit',self, shortcut="Ctrl+Q",statusTip="Exit the Application",triggered=self.exitFile) self.copyAction = QAction( QIcon('copy.png'), 'C&opy',self, shortcut="Ctrl+C",statusTip="Copy",triggered=self.textEdit.copy) self.pasteAction = QAction( QIcon('paste.png'), '&Paste',self, shortcut="Ctrl+V",statusTip="Paste",triggered=self.textEdit.paste) self.aboutAction = QAction( QIcon('about.png'), 'A&bout',self, statusTip="Displays info about text editor",triggered=self.aboutHelp)
为按钮添加动作;
self.fileMenu.addAction(self.newAction)
self.fileMenu.addSeparator()
self.fileMenu.addAction(self.exitAction)
self.editMenu.addAction(self.copyAction)
self.fileMenu.addSeparator()
self.editMenu.addAction(self.pasteAction)
self.helpMenu.addAction(self.aboutAction)
所有代码如下:
# Import required modules import sys from PySide6.QtWidgets import * from PySide6.QtGui import QAction,QIcon,QKeySequence class MainWindow(QMainWindow): """ Our Main Window Class """ def __init__(self): """ Constructor Fucntion """ QMainWindow.__init__(self) self.setWindowTitle("Main Window") ... self.setGeometry(300, 250, 400, 300) self.statusLabel = QLabel('Showing Progress') self.progressBar = QProgressBar() self.progressBar.setMinimum(0) self.progressBar.setMaximum(100) ... def CreateStatusBar(self): """ Function to create Status Bar """ self.myStatusBar = QStatusBar() self.progressBar.setValue(10)#新增 self.myStatusBar.addWidget(self.statusLabel, 1)#新增 self.myStatusBar.addWidget(self.progressBar, 2)#新增 self.setStatusBar(self.myStatusBar) # Actual menu bar item creation def CreateMenus(self): """ Function to create actual menu bar """ self.fileMenu = self.menuBar().addMenu("&File") self.editMenu = self.menuBar().addMenu("&Edit") self.helpMenu = self.menuBar().addMenu("&Help") def SetupComponents(self): """ Function to setup status bar, central widget, menu bar """ self.myStatusBar = QStatusBar() self.setStatusBar(self.myStatusBar) self.myStatusBar.showMessage('Ready', 10000) self.textEdit = QTextEdit() self.setCentralWidget(self.textEdit) self.CreateActions() self.CreateMenus() self.fileMenu.addAction(self.newAction) self.fileMenu.addSeparator() self.fileMenu.addAction(self.exitAction) self.editMenu.addAction(self.copyAction) self.fileMenu.addSeparator() self.editMenu.addAction(self.pasteAction) self.helpMenu.addAction(self.aboutAction) def CreateActions(self): """ Function to create actions for menus """ self.newAction = QAction( QIcon('new.png'), '&New',self, shortcut=QKeySequence.New,statusTip="Create a New File",triggered=self.newFile) self.exitAction = QAction( QIcon('exit.png'), 'E&xit',self, shortcut="Ctrl+Q",statusTip="Exit the Application",triggered=self.exitFile) self.copyAction = QAction( QIcon('copy.png'), 'C&opy',self, shortcut="Ctrl+C",statusTip="Copy",triggered=self.textEdit.copy) self.pasteAction = QAction( QIcon('paste.png'), '&Paste',self, shortcut="Ctrl+V",statusTip="Paste",triggered=self.textEdit.paste) self.aboutAction = QAction( QIcon('about.png'), 'A&bout',self, statusTip="Displays info about text editor",triggered=self.aboutHelp) # Slots called when the menu actions are triggered def newFile(self): self.textEdit.setText('') def exitFile(self): self.close() def aboutHelp(self): QMessageBox.about(self, "About Simple Text Editor","This example demonstrates the use ""of Menu Bar") if __name__ == '__main__': # Exception Handling try: myApp = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.SetupComponents() mainWindow.CreateStatusBar() mainWindow.show() myApp.exec() sys.exit(0) except NameError: print("Name Error:", sys.exc_info()[1]) except SystemExit: print("Closing Window...") except Exception: print(sys.exc_info()[1])
效果如下
添加工具栏
添加工具栏的方法很简单,就是一个方法,它让一些菜单栏的动作直接展示出来;
def CreateToolBar(self): """ Function to create tool bar """ self.mainToolBar = self.addToolBar('Main')
添加动作也很简单
...
self.helpMenu.addAction(self.aboutAction)
self.mainToolBar.addAction(self.newAction)
self.mainToolBar.addSeparator()
self.mainToolBar.addAction(self.copyAction)
self.mainToolBar.addAction(self.pasteAction)
所有代码如下
# Import required modules import sys from PySide6.QtWidgets import * from PySide6.QtGui import QAction,QIcon,QKeySequence class MainWindow(QMainWindow): """ Our Main Window Class """ def __init__(self): """ Constructor Fucntion """ QMainWindow.__init__(self) self.setWindowTitle("Main Window") ... self.setGeometry(300, 250, 400, 300) self.statusLabel = QLabel('Showing Progress') self.progressBar = QProgressBar() self.progressBar.setMinimum(0) self.progressBar.setMaximum(100) ... def CreateStatusBar(self): """ Function to create Status Bar """ self.myStatusBar = QStatusBar() self.progressBar.setValue(10)#新增 self.myStatusBar.addWidget(self.statusLabel, 1)#新增 self.myStatusBar.addWidget(self.progressBar, 2)#新增 self.setStatusBar(self.myStatusBar) # Actual menu bar item creation def CreateMenus(self): """ Function to create actual menu bar """ self.fileMenu = self.menuBar().addMenu("&File") self.editMenu = self.menuBar().addMenu("&Edit") self.helpMenu = self.menuBar().addMenu("&Help") def SetupComponents(self): """ Function to setup status bar, central widget, menu bar """ self.myStatusBar = QStatusBar() self.setStatusBar(self.myStatusBar) self.myStatusBar.showMessage('Ready', 10000) self.textEdit = QTextEdit() self.setCentralWidget(self.textEdit) self.CreateActions() self.CreateMenus() self.fileMenu.addAction(self.newAction) self.fileMenu.addSeparator() self.fileMenu.addAction(self.exitAction) self.editMenu.addAction(self.copyAction) self.fileMenu.addSeparator() self.editMenu.addAction(self.pasteAction) self.helpMenu.addAction(self.aboutAction) self.mainToolBar.addAction(self.newAction) self.mainToolBar.addSeparator() self.mainToolBar.addAction(self.copyAction) self.mainToolBar.addAction(self.pasteAction) def CreateToolBar(self): """ Function to create tool bar """ self.mainToolBar = self.addToolBar('Main') def CreateActions(self): """ Function to create actions for menus """ self.newAction = QAction( QIcon('new.png'), '&New',self, shortcut=QKeySequence.New,statusTip="Create a New File",triggered=self.newFile) self.exitAction = QAction( QIcon('exit.png'), 'E&xit',self, shortcut="Ctrl+Q",statusTip="Exit the Application",triggered=self.exitFile) self.copyAction = QAction( QIcon('copy.png'), 'C&opy',self, shortcut="Ctrl+C",statusTip="Copy",triggered=self.textEdit.copy) self.pasteAction = QAction( QIcon('paste.png'), '&Paste',self, shortcut="Ctrl+V",statusTip="Paste",triggered=self.textEdit.paste) self.aboutAction = QAction( QIcon('about.png'), 'A&bout',self, statusTip="Displays info about text editor",triggered=self.aboutHelp) # Slots called when the menu actions are triggered def newFile(self): self.textEdit.setText('') def exitFile(self): self.close() def aboutHelp(self): QMessageBox.about(self, "About Simple Text Editor","This example demonstrates the use ""of Menu Bar") if __name__ == '__main__': # Exception Handling try: myApp = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.CreateToolBar() mainWindow.SetupComponents() mainWindow.CreateStatusBar() mainWindow.show() myApp.exec() sys.exit(0) except NameError: print("Name Error:", sys.exc_info()[1]) except SystemExit: print("Closing Window...") except Exception: print(sys.exc_info()[1])
效果如下
懒,就没有设置图标ㄟ( ▔, ▔ )ㄏ
4.布局管理
绝对定位相对定位;
字面意思,绝对定位通过像素坐标定位,相对则是相对其它部件定位,适应性更强;
QGridLayout
# Import required modules import sys from PySide6.QtWidgets import * class MainWindow(QMainWindow): """ Our Main Window Class """ def __init__(self): """ Constructor Fucntion """ QMainWindow.__init__(self) self.setWindowTitle("Main Window") self.setGeometry(300, 250, 400, 300) self.wgt = QWidget(self)#博客园:戳人痛处 bili:崩析 self.setCentralWidget(self.wgt)#博客园:戳人痛处 bili:崩析 def SetLayout(self): gridLayout = QGridLayout(self) gButton1 = QPushButton('Button 1', self) gButton2 = QPushButton('Button 2', self) gButton3 = QPushButton('Button 3', self) gButton4 = QPushButton('Button 4', self) gButton5 = QPushButton('Button 5', self) gridLayout.addWidget(gButton1, 0, 0) gridLayout.addWidget(gButton2, 0, 1) gridLayout.addWidget(gButton3, 1, 0,1,2) gridLayout.addWidget(gButton4, 2, 0) gridLayout.addWidget(gButton5, 2, 1) self.wgt.setLayout(gridLayout)#博客园:戳人痛处 bili:崩析 if __name__ == '__main__': # Exception Handling try: myApp = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.SetLayout() mainWindow.show() myApp.exec() sys.exit(0) except NameError: print("Name Error:", sys.exc_info()[1]) except SystemExit: print("Closing Window...") except Exception: print(sys.exc_info()[1])
QFormLayout
# Import required modules import sys from PySide6.QtWidgets import * class MainWindow(QMainWindow): """ Our Main Window Class """ def __init__(self): """ Constructor Fucntion """ QMainWindow.__init__(self) self.setWindowTitle("Main Window") self.setGeometry(300, 250, 400, 300) self.wgt = QWidget(self)#博客园:戳人痛处 bili:崩析 self.setCentralWidget(self.wgt)#博客园:戳人痛处 bili:崩析 def SetLayout(self): formLayout = QFormLayout(self) labelUsername = QLabel("Username") txtUsername = QLineEdit() labelPassword = QLabel("Password") txtPassword = QLineEdit() formLayout.addRow(labelUsername, txtUsername) formLayout.addRow(labelPassword, txtPassword) self.wgt.setLayout(formLayout)#博客园:戳人痛处 bili:崩析 if __name__ == '__main__': # Exception Handling try: myApp = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.SetLayout() mainWindow.show() myApp.exec() sys.exit(0) except NameError: print("Name Error:", sys.exc_info()[1]) except SystemExit: print("Closing Window...") except Exception: print(sys.exc_info()[1])
QHBoxLayout
# Import required modules import sys from PySide6.QtWidgets import * class MainWindow(QMainWindow): """ Our Main Window Class """ def __init__(self): """ Constructor Fucntion """ QMainWindow.__init__(self) self.setWindowTitle("Main Window") self.setGeometry(300, 250, 400, 300) self.wgt = QWidget(self)#博客园:戳人痛处 bili:崩析 self.setCentralWidget(self.wgt)#博客园:戳人痛处 bili:崩析 def SetLayout(self): """ Function to add buttons and set the layout """ horizontalLayout = QHBoxLayout(self) hButton1 = QPushButton('Button 1', self) hButton2 = QPushButton('Button 2', self) hButton3 = QPushButton('Button 3', self) hButton4 = QPushButton('Button 4', self) horizontalLayout.addWidget(hButton1) horizontalLayout.addWidget(hButton2) horizontalLayout.addWidget(hButton3) horizontalLayout.addWidget(hButton4) self.wgt.setLayout(horizontalLayout)#博客园:戳人痛处 bili:崩析 if __name__ == '__main__': # Exception Handling try: myApp = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.SetLayout() mainWindow.show() myApp.exec() sys.exit(0) except NameError: print("Name Error:", sys.exc_info()[1]) except SystemExit: print("Closing Window...") except Exception: print(sys.exc_info()[1])
QVBoxLayout
# Import required modules import sys from PySide6.QtWidgets import * class MainWindow(QMainWindow): """ Our Main Window Class """ def __init__(self): """ Constructor Fucntion """ QMainWindow.__init__(self) self.setWindowTitle("Main Window") self.setGeometry(300, 250, 400, 300) self.wgt = QWidget(self)#博客园:戳人痛处 bili:崩析 self.setCentralWidget(self.wgt)#博客园:戳人痛处 bili:崩析 def SetLayout(self): verticalLayout = QVBoxLayout(self) vButton1 = QPushButton('Button 1', self) vButton2 = QPushButton('Button 2', self) vButton3 = QPushButton('Button 3', self) vButton4 = QPushButton('Button 4', self) verticalLayout.addWidget(vButton1) verticalLayout.addWidget(vButton2) verticalLayout.addStretch() verticalLayout.addWidget(vButton3) verticalLayout.addWidget(vButton4) self.wgt.setLayout(verticalLayout)#博客园:戳人痛处 bili:崩析 if __name__ == '__main__': # Exception Handling try: myApp = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.SetLayout() mainWindow.show() myApp.exec() sys.exit(0) except NameError: print("Name Error:", sys.exc_info()[1]) except SystemExit: print("Closing Window...") except Exception: print(sys.exc_info()[1])
5.一个简单的文本编辑器
# Import required modules import sys from PySide6.QtWidgets import * from PySide6.QtGui import * class MainWindow(QMainWindow): """ Our Main Window class """ def __init__(self, fileName=None): """ Constructor Function """ QMainWindow.__init__(self) self.setWindowTitle("A Simple Text Editor") self.setWindowIcon(QIcon('appicon.png')) self.setGeometry(100, 100, 800, 600) self.textEdit = QTextEdit() self.setCentralWidget(self.textEdit) self.fileName = None self.filters = "Text files (*.txt)" def SetupComponents(self): """ Function to setup status bar, central widget, menu bar, tool bar """ self.myStatusBar = QStatusBar() self.setStatusBar(self.myStatusBar) self.myStatusBar.showMessage('Ready', 10000) #博客园:戳人痛处 bili:崩析 self.CreateActions() self.CreateMenus() self.CreateToolBar() self.fileMenu.addAction(self.newAction) self.fileMenu.addAction(self.openAction) self.fileMenu.addAction(self.saveAction) self.fileMenu.addSeparator() self.fileMenu.addAction(self.exitAction) self.editMenu.addAction(self.cutAction) self.editMenu.addAction(self.copyAction) self.editMenu.addAction(self.pasteAction) self.editMenu.addSeparator() self.editMenu.addAction(self.undoAction) self.editMenu.addAction(self.redoAction) self.editMenu.addSeparator() self.editMenu.addAction(self.selectAllAction) self.formatMenu.addAction(self.fontAction) self.helpMenu.addAction(self.aboutAction) self.helpMenu.addSeparator() self.helpMenu.addAction(self.aboutQtAction) self.mainToolBar.addAction(self.newAction) self.mainToolBar.addAction(self.openAction) self.mainToolBar.addAction(self.saveAction) self.mainToolBar.addSeparator() self.mainToolBar.addAction(self.cutAction) self.mainToolBar.addAction(self.copyAction) self.mainToolBar.addAction(self.pasteAction) self.mainToolBar.addSeparator() self.mainToolBar.addAction(self.undoAction) self.mainToolBar.addAction(self.redoAction) # Slots called when the menu actions are triggered def newFile(self): self.textEdit.setText('') self.fileName = None def openFile(self): self.fileName, self.filterName = QFileDialog.getOpenFileName(self) self.textEdit.setText(open(self.fileName).read()) def saveFile(self):#博客园:戳人痛处 bili:崩析 if self.fileName == None or self.fileName == '': self.fileName, self.filterName = QFileDialog.getSaveFileName(self, filter=self.filters) if(self.fileName != ''): file = open(self.fileName, 'w') file.write(self.textEdit.toPlainText()) self.statusBar().showMessage("File saved", 2000) def exitFile(self): self.close() def fontChange(self): (font, ok) = QFontDialog.getFont(QFont("Helvetica [Cronyx]", 10), self) if ok: self.textEdit.setCurrentFont(font) def aboutHelp(self): QMessageBox.about(self, "About Simple Text Editor","A Simple Text Editor where you can edit and save files") def CreateActions(self): """ Function to create actions for menus """#博客园:戳人痛处 bili:崩析 self.newAction = QAction( QIcon('new.png'), '&New',self, shortcut=QKeySequence.New,statusTip="Create a New File",triggered=self.newFile) self.openAction = QAction( QIcon('open.png'), 'O&pen',self, shortcut=QKeySequence.Open,statusTip="Open an existing file",triggered=self.openFile) self.saveAction = QAction( QIcon('save.png'), '&Save',self, shortcut=QKeySequence.Save,statusTip="Save the current file to disk",triggered=self.saveFile) self.exitAction = QAction( QIcon('exit.png'), 'E&xit',self, shortcut="Ctrl+Q",statusTip="Exit the Application",triggered=self.exitFile) self.cutAction = QAction( QIcon('cut.png'), 'C&ut',self, shortcut=QKeySequence.Cut,statusTip="Cut the current selection to clipboard",triggered=self.textEdit.cut) self.copyAction = QAction( QIcon('copy.png'), 'C&opy',self, shortcut=QKeySequence.Copy,statusTip="Copy the current selection to clipboard",triggered=self.textEdit.copy) self.pasteAction = QAction( QIcon('paste.png'), '&Paste',self, shortcut=QKeySequence.Paste,statusTip="Paste the clipboard's content in current location",triggered=self.textEdit.paste) self.selectAllAction = QAction( QIcon('selectAll.png'), 'Select All',self, statusTip="Select All",triggered=self.textEdit.selectAll) self.redoAction = QAction( QIcon('redo.png'),'Redo', self,shortcut=QKeySequence.Redo,statusTip="Redo previous action",triggered=self.textEdit.redo) self.undoAction = QAction( QIcon('undo.png'),'Undo', self,shortcut=QKeySequence.Undo,statusTip="Undo previous action",triggered=self.textEdit.undo) self.fontAction = QAction( 'F&ont', self,statusTip = "Modify font properties",triggered = self.fontChange) self.aboutAction = QAction( QIcon('about.png'), 'A&bout',self, statusTip="Displays info about text editor",triggered=self.aboutHelp) self.aboutQtAction = QAction("About &Qt", self,statusTip="Show the Qt library's About box",triggered=qApp.aboutQt) def CreateMenus(self): self.fileMenu = self.menuBar().addMenu("&File") self.editMenu = self.menuBar().addMenu("&Edit") self.formatMenu = self.menuBar().addMenu("F&ormat") self.helpMenu = self.menuBar().addMenu("&Help") def CreateToolBar(self): self.mainToolBar = self.addToolBar('Main') if __name__ == '__main__': # Exception Handling try:#博客园:戳人痛处 bili:崩析 myApp = QApplication(sys.argv) mainWindow = MainWindow() mainWindow.SetupComponents() mainWindow.show() myApp.exec() sys.exit(0) except NameError: print("Name Error:", sys.exc_info()[1]) except SystemExit: print("Closing Window...") except Exception: print(sys.exc_info()[1])
为啥没图标? 因为懒得设置ㄟ( ▔, ▔ )ㄏ