pyqt5 实现 下拉菜单 + 打开文件的示例代码


Posted in Python onJune 20, 2019

如下所示:

# -*- coding: utf-8 -*-
import sys
import os
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import QtCore, QtGui, QtWidgets
 
from PyQt5.QtCore import QRect, QSize, QMetaObject, QCoreApplication,\
  QPropertyAnimation
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QGridLayout, QPushButton,\
  QApplication, QMainWindow
 
class Ui_MainWindow(object):
  def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(800, 681)
    MainWindow.setMinimumSize(QtCore.QSize(0, 651))
    MainWindow.setStyleSheet("background-image: url(:/新前缀/backpic.png);")
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.widget = QtWidgets.QWidget(self.centralwidget)
    self.widget.setGeometry(QtCore.QRect(0, -580, 195, 631))
    self.widget.setObjectName("widget")
    self.scrollArea = QtWidgets.QScrollArea(self.widget)
    self.scrollArea.setGeometry(QtCore.QRect(0, 0, 195, 581))
    self.scrollArea.setWidgetResizable(True)
    self.scrollArea.setObjectName("scrollArea")
    self.scrollAreaWidgetContents = QtWidgets.QWidget()
    self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 178, 681))
    self.scrollAreaWidgetContents.setMinimumSize(QtCore.QSize(177, 681))
    self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
    self.verticalLayoutWidget = QtWidgets.QWidget(self.scrollAreaWidgetContents)
    self.verticalLayoutWidget.setGeometry(QtCore.QRect(0, -10, 181, 691))
    self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
    self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
    self.verticalLayout.setContentsMargins(0, 0, 0, 0)
    self.verticalLayout.setSpacing(20)
    self.verticalLayout.setObjectName("verticalLayout")
    self.pushButton_2 = QtWidgets.QPushButton(self.verticalLayoutWidget)
    self.pushButton_2.setObjectName("pushButton_2")
    self.verticalLayout.addWidget(self.pushButton_2)
    self.label_2 = QtWidgets.QLabel(self.verticalLayoutWidget)
    self.label_2.setObjectName("label_2")
    self.verticalLayout.addWidget(self.label_2)
    self.pushButton_4 = QtWidgets.QPushButton(self.verticalLayoutWidget)
    self.pushButton_4.setObjectName("pushButton_4")
    self.verticalLayout.addWidget(self.pushButton_4)
    self.label_3 = QtWidgets.QLabel(self.verticalLayoutWidget)
    self.label_3.setObjectName("label_3")
    self.verticalLayout.addWidget(self.label_3)
    self.pushButton_7 = QtWidgets.QPushButton(self.verticalLayoutWidget)
    self.pushButton_7.setObjectName("pushButton_7")
    self.verticalLayout.addWidget(self.pushButton_7)
    self.label_4 = QtWidgets.QLabel(self.verticalLayoutWidget)
    self.label_4.setObjectName("label_4")
    self.verticalLayout.addWidget(self.label_4)
    self.pushButton_6 = QtWidgets.QPushButton(self.verticalLayoutWidget)
    self.pushButton_6.setObjectName("pushButton_6")
    self.verticalLayout.addWidget(self.pushButton_6)
    self.label_5 = QtWidgets.QLabel(self.verticalLayoutWidget)
    self.label_5.setObjectName("label_5")
    self.verticalLayout.addWidget(self.label_5)
    self.pushButton_5 = QtWidgets.QPushButton(self.verticalLayoutWidget)
    self.pushButton_5.setObjectName("pushButton_5")
    self.verticalLayout.addWidget(self.pushButton_5)
    self.label_6 = QtWidgets.QLabel(self.verticalLayoutWidget)
    self.label_6.setObjectName("label_6")
    self.verticalLayout.addWidget(self.label_6)
    self.pushButton = QtWidgets.QPushButton(self.verticalLayoutWidget)
    self.pushButton.setObjectName("pushButton")
    self.verticalLayout.addWidget(self.pushButton)
    self.label_7 = QtWidgets.QLabel(self.verticalLayoutWidget)
    self.label_7.setObjectName("label_7")
    self.verticalLayout.addWidget(self.label_7)
    self.scrollArea.setWidget(self.scrollAreaWidgetContents)
    self.closeButton = QtWidgets.QPushButton(self.widget)
    self.closeButton.setGeometry(QtCore.QRect(50, 580, 61, 32))
    self.closeButton.setStyleSheet("border-image: url(:/新前缀/v.png);")
    self.closeButton.setText("")
    self.closeButton.setObjectName("closeButton")
    self.openButton = QtWidgets.QPushButton(self.centralwidget)
    self.openButton.setGeometry(QtCore.QRect(50, 0, 61, 32))
    self.openButton.setStyleSheet("border-image: url(:/新前缀/xia.png);")
    self.openButton.setText("")
    self.openButton.setObjectName("openButton")
    self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
    self.graphicsView.setGeometry(QtCore.QRect(0, 0, 801, 641))
    self.graphicsView.setObjectName("graphicsView")
    self.widget.raise_()
    self.graphicsView.raise_()
    self.openButton.raise_()
    MainWindow.setCentralWidget(self.centralwidget)
    self.menubar = QtWidgets.QMenuBar(MainWindow)
    self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
    self.menubar.setObjectName("menubar")
    self.menu = QtWidgets.QMenu(self.menubar)
    self.menu.setObjectName("menu")
    MainWindow.setMenuBar(self.menubar)
    self.statusbar = QtWidgets.QStatusBar(MainWindow)
    self.statusbar.setObjectName("statusbar")
    MainWindow.setStatusBar(self.statusbar)
    self.action = QtWidgets.QAction(MainWindow)
    self.action.setObjectName("action")
    self.action_2 = QtWidgets.QAction(MainWindow)
    self.action_2.setObjectName("action_2")
    self.action_3 = QtWidgets.QAction(MainWindow)
    self.action_3.setObjectName("action_3")
    self.menu.addAction(self.action)
    self.menu.addAction(self.action_2)
    self.menu.addAction(self.action_3)
    self.menubar.addAction(self.menu.menuAction())
 
    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)
 
  def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    self.pushButton_2.setText(_translate("MainWindow", "PushButton"))
    self.label_2.setText(_translate("MainWindow", "TextLabel"))
    self.pushButton_4.setText(_translate("MainWindow", "PushButton"))
    self.label_3.setText(_translate("MainWindow", "TextLabel"))
    self.pushButton_7.setText(_translate("MainWindow", "PushButton"))
    self.label_4.setText(_translate("MainWindow", "TextLabel"))
    self.pushButton_6.setText(_translate("MainWindow", "PushButton"))
    self.label_5.setText(_translate("MainWindow", "TextLabel"))
    self.pushButton_5.setText(_translate("MainWindow", "PushButton"))
    self.label_6.setText(_translate("MainWindow", "TextLabel"))
    self.pushButton.setText(_translate("MainWindow", "PushButton"))
    self.label_7.setText(_translate("MainWindow", "TextLabel"))
    self.menu.setTitle(_translate("MainWindow", "人脑架构分析"))
 
    self.action.setText(_translate("MainWindow", "导入矩阵"))
    self.action_2.setText(_translate("MainWindow", "退出系统"))
    self.action_3.setText(_translate("MainWindow", "开发者信息"))
 
 
class Window(QMainWindow, Ui_MainWindow):
  def __init__(self, *args, **kwargs):
    super(Window, self).__init__(*args, **kwargs)
 
 
    self.setupUi(self)
    self.outani = QPropertyAnimation(self.widget, b"geometry") # 背后隐藏的控件
    self.outani.setDuration(1000) # 1s
    self.outani.setEndValue(QRect(0, 30, 178, 681)) # 只是x坐标变化
 
    # 移进去动画
    self.inani = QPropertyAnimation(self.widget, b"geometry") # 背后隐藏的控件
    self.inani.setDuration(1000) # 1s
    self.inani.setEndValue(QRect(0, -580, 178, 681))
    self.inani.finished.connect(self.onFinish)
 
    # 绑定打开关闭事件
    self.openButton.clicked.connect(self.onOpen)
    self.closeButton.clicked.connect(self.onClose)
 
    #打开文件
    self.action.triggered.connect(self.openFile)
    self.action.setStatusTip('导入矩阵')
    self.action.setShortcut('Ctrl+Q')
 
    #退出系统
    self.action_2.triggered.connect(self.close)
    self.action_2.setStatusTip('退出系统')
    self.action_2.setShortcut('Ctrl+E')
 
    #开发者信息
    self.action_3.setStatusTip('唐伟泽 谢家柯 2016.12.3')
 
  def onOpen(self):
    # 设置按钮不可见
    self.openButton.setVisible(False)
    self.widget.setVisible(True) # 背后的待拉出来的控件可见
    self.graphicsView.setGeometry(QtCore.QRect(195, 1, 611, 641))
    self.outani.start() # 开启动画效果
 
  def onClose(self):
    self.outani.stop() # 如果移动到一半则停止
    self.inani.start() # 退回去
 
  def onFinish(self): # 关闭动画结束
    self.widget.setVisible(False)
    self.openButton.setVisible(True)
    self.graphicsView.setGeometry(QtCore.QRect(0, 0, 801, 641))
 
  def openFile(self):
    fileName1, filetype = QFileDialog.getOpenFileName(self,"选取文件","/Users/Kelisiya/Desktop","All Files (*);;Text Files (*.txt)")
    print(fileName1, filetype)
 
 
 
if __name__ == "__main__":
  import sys
  app = QApplication(sys.argv)
  w = Window()
  w.show()
  sys.exit(app.exec_())

以上这篇pyqt5 实现 下拉菜单 + 打开文件的示例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python3遍历目录树实现方法
May 22 Python
python使用xmlrpclib模块实现对百度google的ping功能
Jun 02 Python
python高手之路python处理excel文件(方法汇总)
Jan 07 Python
python取代netcat过程分析
Feb 10 Python
python按行读取文件,去掉每行的换行符\n的实例
Apr 19 Python
详解用TensorFlow实现逻辑回归算法
May 02 Python
python实现简单淘宝秒杀功能
May 03 Python
python-str,list,set间的转换实例
Jun 27 Python
Pytorch之contiguous的用法
Dec 31 Python
Python实现自动装机功能案例分析
Oct 22 Python
python+openCV对视频进行截取的实现
Nov 27 Python
python删除csv文件的行列
Apr 06 Python
pyqt5实现绘制ui,列表窗口,滚动窗口显示图片的方法
Jun 20 #Python
PyQt5 窗口切换与自定义对话框的实例
Jun 20 #Python
PyQt5笔记之弹出窗口大全
Jun 20 #Python
PyQt5创建一个新窗口的实例
Jun 20 #Python
pyQT5 实现窗体之间传值的示例
Jun 20 #Python
python3.6环境安装+pip环境配置教程图文详解
Jun 20 #Python
Python 3.6 -win64环境安装PIL模块的教程
Jun 20 #Python
You might like
火车头采集器3.0采集图文教程
2007/03/17 PHP
LINUX下PHP程序实现WORD文件转化为PDF文件的方法
2016/05/13 PHP
php利用fsockopen GET/POST提交表单及上传文件
2017/05/22 PHP
基于PHP+mysql实现新闻发布系统的开发
2020/08/06 PHP
excel操作之Add Data to a Spreadsheet Cell
2007/06/12 Javascript
编写高性能的JavaScript 脚本的加载与执行
2010/04/19 Javascript
jQuery EasyUI API 中文文档 - ValidateBox验证框
2011/10/06 Javascript
javascript中获取下个月一号,是星期几
2012/06/01 Javascript
简单的两种Extjs formpanel加载数据的方式
2013/11/09 Javascript
express的中间件bodyParser详解
2014/12/04 Javascript
Javascript中的方法链(Method Chaining)介绍
2015/03/15 Javascript
使用Node.js实现HTTP 206内容分片的教程
2015/06/23 Javascript
基于JavaScript代码实现微信扫一扫下载APP
2015/12/30 Javascript
Javascript点击其他任意地方隐藏关闭DIV实例
2016/06/21 Javascript
浅谈js中的in-for循环
2016/06/28 Javascript
Javascript 实现全屏滚动实例代码
2016/12/31 Javascript
js/jq仿window文件夹框选操作插件
2017/03/08 Javascript
垃圾回收器的相关知识点总结
2018/05/13 Javascript
vue异步加载高德地图的实现
2018/06/19 Javascript
[58:37]Serenity vs Fnatic 2018国际邀请赛淘汰赛BO1 8.21
2018/08/22 DOTA
[07:54]DOTA2-DPC中国联赛 正赛 iG vs VG 选手采访
2021/03/11 DOTA
python中lambda与def用法对比实例分析
2015/04/30 Python
python实现闹钟定时播放音乐功能
2018/01/25 Python
Pandas读取并修改excel的示例代码
2019/02/17 Python
Python3几个常见问题的处理方法
2019/02/26 Python
Python实现字符串匹配的KMP算法
2019/04/04 Python
利用Python发送邮件或发带附件的邮件
2020/11/12 Python
Python实现FTP文件定时自动下载的步骤
2020/12/19 Python
Internal修饰符有什么含义
2013/07/10 面试题
男女朋友协议书
2014/04/23 职场文书
学校先进集体事迹材料
2014/05/31 职场文书
个人向公司借款协议书
2016/03/19 职场文书
2016年党员创先争优承诺书
2016/03/25 职场文书
导游词之河北邯郸
2019/09/12 职场文书
详解MySQL主从复制及读写分离
2021/05/07 MySQL
Java死锁的排查
2022/05/11 Java/Android