1. import sys
  2. from PyQt5.QtGui import QIcon
  3. from PyQt5.QtWidgets import QMainWindow, QMenuBar, QToolBar, QTextEdit, QAction, QApplication, qApp, QMessageBox
  4. from PyQt5.QtCore import Qt
  5.  
  6. import threading
  7. import time
  8.  
  9. songs = ['爱情买卖','朋友','回家过年','好日子']
  10. films = ['阿凡达','猩球崛起']
  11.  
  12. class MyWindow(QMainWindow):
  13. def __init__(self):
  14. super().__init__()
  15.  
  16. self.resize(677, 442)
  17. self.setWindowTitle("我的程序")
  18.  
  19. self.createUI()
  20. self.createAction()
  21. self.createStatusbar()
  22. self.createMenu()
  23. self.createToolbar()
  24.  
  25. def createUI(self):
  26. self.textedit = QTextEdit()
  27. self.setCentralWidget(self.textedit)
  28.  
  29. # 动作
  30. def createAction(self):
  31. self.exit_action = QAction(QIcon("ico_new.jpg"), "退出", self, triggered=qApp.quit)
  32. self.exit_action.setStatusTip("退出程序")
  33. self.exit_action.setShortcut("Ctrl+Q")
  34. self.exit_action.triggered.connect(qApp.quit)
  35.  
  36. # 状态栏
  37. def createStatusbar(self):
  38. self.statusBar()
  39.  
  40. # 菜单栏
  41. def createMenu(self):
  42. #menubar = QMenuBar(self)
  43. menubar = self.menuBar()
  44. menu = menubar.addMenu("文件(F)")
  45. menu.addAction(QAction(QIcon("ico_new_16_16.jpg"), "新建", self, triggered=qApp.quit)) # 带图标,文字
  46. menu.addAction(QAction(QIcon("ico_open_16_16.jpg"), "打开", self, triggered=qApp.quit))
  47. menu.addAction(QAction(QIcon("ico_save_16_16.jpg"), "保存", self, triggered=qApp.quit))
  48. menu.addSeparator()
  49. menu.addAction(QAction(QIcon("ico_close_16_16.jpg"), "关闭", self, triggered=lambda :QMessageBox.about(self, '关闭','关闭。。。')))
  50.  
  51. menu = menubar.addMenu("编辑(E)")
  52. menu.addAction(QAction("撤销", self, triggered=qApp.quit)) # 不带图标
  53. menu.addAction(QAction("剪切", self, triggered=qApp.quit))
  54. menu.addAction(QAction("复制", self, triggered=qApp.quit))
  55. menu.addAction(QAction("粘贴", self, triggered=qApp.quit))
  56.  
  57. menu = menubar.addMenu("娱乐(S)")
  58. menu.addAction(QAction("音乐", self, triggered=lambda :self.thread_it(self.music, songs))) # 线程
  59. menu.addAction(QAction("电影", self, triggered=lambda :self.thread_it(self.movie, films)))
  60.  
  61. menu = menubar.addMenu("帮助(H)")
  62. menu.addAction('&New', lambda :QMessageBox.about(self, 'New','新建。。。'), Qt.CTRL + Qt.Key_N) # 注意快捷键
  63. menu.addAction('关于', lambda :QMessageBox.about(self, '关于','关于。。。'), Qt.CTRL + Qt.Key_Q)
  64.  
  65. # 工具栏
  66. def createToolbar(self):
  67. toolbar = self.addToolBar('文件')
  68. toolbar.addAction(QAction(QIcon("ico_new_16_16.jpg"), "新建", self, triggered=qApp.quit)) # 带图标,文字
  69. toolbar.addAction(QAction(QIcon("ico_open_16_16.jpg"), "打开", self, triggered=qApp.quit))
  70. toolbar.addSeparator()
  71. toolbar.addAction(QAction(QIcon("ico_save_16_16.jpg"), "打开", self, triggered=qApp.quit))
  72.  
  73. toolbar = self.addToolBar("编辑")
  74. toolbar.addAction(QAction("撤销", self, triggered=qApp.quit)) # 不带图标
  75. toolbar.addAction(QAction("剪切", self, triggered=qApp.quit))
  76.  
  77. # 逻辑:听音乐
  78. def music(self, songs):
  79. for x in songs:
  80. self.textedit.append("听音乐:%s \t-- %s" %(x, time.ctime()))
  81. time.sleep(3)
  82.  
  83. # 逻辑:看电影
  84. def movie(self, films):
  85. for x in films:
  86. self.textedit.append("看电影:%s \t-- %s" %(x, time.ctime()))
  87. time.sleep(5)
  88.  
  89. # 打包进线程(耗时的操作)
  90. @staticmethod
  91. def thread_it(func, *args):
  92. t = threading.Thread(target=func, args=args)
  93. t.setDaemon(True) # 守护--就算主界面关闭,线程也会留守后台运行(不对!)
  94. t.start() # 启动
  95. # t.join() # 阻塞--会卡死界面!
  96.  
  97. app = QApplication(sys.argv)
  98. win = MyWindow()
  99. win.show()
  100. sys.exit(app.exec_())

pyqt5 菜单,工具栏,线程,matplotlib的更多相关文章

  1. 基于 SailingEase WinForm Framework 开发客户端程序(3:实现菜单/工具栏按钮的解耦及状态控制)

    本系列文章将详细阐述客户端应用程序的设计理念,实现方法. 本系列文章以  SailingEase WinForm Framework 为基础进行设计并实现,但其中的设计理念及方法,亦适用于任何类型的客 ...

  2. 菜单工具栏wxPython菜单与工具栏基础示例

    这两天一直在学习菜单工具栏之类的问题,上午正好有机会和大家讨论一下. 1.基本的api介绍 Package wx :: Class Menu Type Menu Method Summary Menu ...

  3. Python+PyQT5的子线程更新UI界面的实例《新手必学》

    今天小编就为大家分享一篇Python+PyQT5的子线程更新UI界面的实例,具有很好的参考价值,希望对大家有所帮助.一起跟随小编过来看看吧子线程里是不能更新UI界面的,在移动端方面.Android的U ...

  4. pyqt5 -——菜单和工具栏

    一. 状态栏 # -*- coding: utf-8 -*-# @Time : 2018/12/22 12:37# @Author : Bo# @Email : mat_wu@163.com# @Fi ...

  5. PyQt5 QSerialPort子线程操作

    环境: python3.6 pyqt5 只是简单的一个思路,请忽略脆弱的异常防护: # -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets im ...

  6. WindowsForm菜单工具栏--2016年12月6日

    ContextMenuStrip 添加控件后可在其他空间属性中进行绑定 MenuStrip       设置热键:在编辑的时候输入(&F)       设置快捷键:选中菜单项--右键属性--S ...

  7. 菜单 & 工具栏 & 状态栏

    MFC中ON_UPDATE_COMMAND_UI和ON_COMMAND消息区别 CCmdUI 加载状态栏 加载工具栏

  8. pyqt5 重启相同线程错误:QThread: Destroyed while thread is still running

    背景: 把一个基于QObject的类的槽运行在另一个线程,我们可以用moveToThread的方法. 1 新建一个子线程类,编写槽函数和信号,MyClass *m_MyClass=new MyClas ...

  9. PyQt 5菜单和工具栏

    QMainWindow类提供主要应用程序的窗口,有添加状态栏.工具栏.菜单栏等功能 状态栏 self.statusBar().showMessage('Ready') # 创建一个状态栏 # 状态栏显 ...

随机推荐

  1. psql: FATAL: role “postgres” does not exist

    I'm a postgres novice. I installed the postgres.app for mac. I was playing around with the psql comm ...

  2. tomcat多实例.md

    多tomcat实例 环境说明 操作系统:CentOS 6.6 JDK: # ll /usr/local/java lrwxrwxrwx 1 root root 22 Feb 27 17:43 /usr ...

  3. Ping服务

    什么是Ping服务 ping是基于XML_RPC标准协议的更新通告服务,用于博客把内容更新快速通知给百度,以便百度及时进行抓取和更新. Ping服务使用方法 你可以采取手动通知和自动通知两种方式使用p ...

  4. 【react】慕课网视频学习笔记

    1.JSX:语法糖,对语言的功能并没有影响,但更方便程序员使用,增强可读性. 2.jsFiddle:前端在线调试工具 3.为什么要把this额外声明成_self变量,因为window.setTimeo ...

  5. HTML 5中的文件处理之FileAPI

    在众多HTML5规范中,有一部分规范是跟文件处理有关的,在早期的浏览器技术中,处理小量字符串是js最擅 长的处理之一.但文件处理,尤其是二进制文件处理,一直是个空白.在一些情况下,我们不得不通过Fla ...

  6. ethereumjs/ethereumjs-vm-1-简介

    https://github.com/ethereumjs/ethereumjs-vm 其实这就是怎么自己使用该模块来生成一个类似geth客户端的以太坊虚拟机,然后进行各类区块链操作 SYNOPSIS ...

  7. (转)进程process和线程thread的关系

    写的很好很明白cpu每次只能执行一个进程,所以其他进程会挂起 在一个进程中,允许存在n个线程,n个线程共享这个进程中的资源 多个线程在共享的时候存在资源互斥,一次只能一个线程,会需要加锁 一次存在固定 ...

  8. Tomcat中的Filter

    Filter 节选部分源码.源码版本 Tomcat8.5 说明 filter 是 Servlet 规范 filter 是在 ,执行 Servlet.service方法之前执行 Filter相关接口 p ...

  9. spring aop,静态及动态代理例子

    @Aspect@Componentpublic class AopText { @Pointcut("execution(public * com.llf.service.*Service. ...

  10. [NOIP2016]换教室(概率期望$DP$)

    其实吧我老早就把这题切了--因为说实话,这道题确实不难啊--李云龙:比他娘的状压DP简单多了 今天我翻以前在Luogu上写的题解时,突然发现放错代码了,然后被一堆人\(hack\)--蓝瘦啊\(ORZ ...