python图形化界面开发学习(1)

主要内容

继承Pyqt5 QMainWindow类

添加菜单栏及菜单项目

添加状态栏

快捷连接菜单项以关闭GUI窗口

效果如图

python图形化界面开发学习(1)_第1张图片

 代码如下

from PyQt5.QtWidgets import QApplication,QMainWindow,QAction
from PyQt5.QtGui import QIcon
import sys

class GUI(QMainWindow):#inherit from QMainwindow
    def __init__(self):
        super().__init__()#initialize super class,which creates the window
        self.initGUI() #refer to window as self

    def initGUI(self):#add widgets and change properties
        self.setWindowTitle('这不是周')#refer to window as self
        self.resize(400,400) #resize(width,height)
        self.add_menu_and_status()
        
    def add_menu_and_status(self):
        self.statusBar().showMessage('状态栏内容')

        menubar =self.menuBar() #create menu bar

        file_menu =menubar.addMenu('文件') #add menu to menu bar

        new_icon =QIcon('file.png') #create icon
        new_action =QAction(new_icon,'创建',self) #crteate an Action
        new_action.setStatusTip('创建新文件')#statusbar pdated
        file_menu.addAction(new_action)  # add Action to menu

        file_menu.addSeparator() #add separator line between menu

        exit_icon = QIcon('exit.png')  # create icon
        exit_action = QAction(exit_icon, '退出', self)  # crteate an Action
        exit_action.setStatusTip('点击退出系统')  # statusbar pdated
        exit_action.triggered.connect(self.close)#close application when clicked
        exit_action.setShortcut('Ctrl+Q') #keyboard shortcut to close application
        file_menu.addAction(exit_action)  # add Action to menu
        #-------------------------------------

        edit_menu =menubar.addMenu('编辑')#add second menu to menu bar
        self.resize(400,400)

if __name__=='__main__':
    app =QApplication(sys.argv) #create Application
    gui =GUI() #create instance of class
    gui.show()#show the constructed Qt window
    sys.exit(app.exec_())#execute the application

 

你可能感兴趣的:(python图形化界面开发学习(1))