zoukankan      html  css  js  c++  java
  • python图形化界面开发学习(1)

    主要内容

    继承Pyqt5 QMainWindow类

    添加菜单栏及菜单项目

    添加状态栏

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

    效果如图

     代码如下

    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
  • 相关阅读:
    Linux中的官方源、镜像源汇总
    提示"libc.so.6: version `GLIBC_2.14' not found"
    win8.1 安装msi软件出现 2503、2502
    平均负载(Load average)
    oracle 导入报错:field in data file exceeds maximum length
    一个命令的输出作为另外一个命令的输入
    Http 状态码
    Linux 命令总结
    ORA-12505: TNS: 监听程序当前无法识别连接描述符中所给出的SID等错误解决方法
    轻松应对IDC机房带宽突然暴涨问题
  • 原文地址:https://www.cnblogs.com/findz/p/13060566.html
Copyright © 2011-2022 走看看