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 相关文章推荐
Python字符转换
Sep 06 Python
Python获取网页上图片下载地址的方法
Mar 11 Python
Python中转换角度为弧度的radians()方法
May 18 Python
python结合shell查询google关键词排名的实现代码
Feb 27 Python
浅析python实现scrapy定时执行爬虫
Mar 04 Python
python 日期操作类代码
May 05 Python
基于wxPython的GUI实现输入对话框(1)
Feb 27 Python
python在openstreetmap地图上绘制路线图的实现
Jul 11 Python
python打印n位数“水仙花数”(实例代码)
Dec 25 Python
pytorch 改变tensor尺寸的实现
Jan 03 Python
浅谈pymysql查询语句中带有in时传递参数的问题
Jun 05 Python
Python使用pycharm导入pymysql教程
Sep 16 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
第十一节--重载
2006/11/16 PHP
php chr() ord()中文截取乱码问题解决方法
2008/09/08 PHP
PHP实现的封装验证码类详解
2013/06/18 PHP
json的键名为数字时的调用方式(示例代码)
2013/11/15 PHP
PHP+APACHE实现网址伪静态
2015/02/22 PHP
Opcache导致php-fpm崩溃nginx返回502
2015/03/02 PHP
PHP CURL post数据报错 failed creating formpost data
2016/10/16 PHP
PHP并发查询MySQL的实例代码
2017/08/09 PHP
PHP基于ip2long实现IP转换整形
2020/12/11 PHP
js中关于一个分号的崩溃示例
2013/11/11 Javascript
JS将所有对象s的属性复制给对象r(原生js+jquery)
2014/01/25 Javascript
jQuery学习笔记之jQuery原型属性和方法
2014/06/09 Javascript
JavaScript实现大数的运算
2014/11/24 Javascript
浅谈轻量级js模板引擎simplite
2015/02/13 Javascript
javascript基于prototype实现类似OOP继承的方法
2015/12/16 Javascript
javascript创建cookie、读取cookie
2016/03/31 Javascript
完美解决axios在ie下的兼容性问题
2018/03/05 Javascript
JavaScript实现滚动加载更多
2020/12/27 Javascript
[01:13]这,就是刀塔
2014/07/16 DOTA
[49:43]VG vs FNATIC 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
python3+PyQt5使用数据库窗口视图
2018/04/24 Python
Python中安装easy_install的方法
2018/11/18 Python
python执行CMD指令,并获取返回的方法
2018/12/19 Python
Python数据可视化库seaborn的使用总结
2019/01/15 Python
python爬虫租房信息在地图上显示的方法
2019/05/13 Python
Python企业编码生成系统之主程序模块设计详解
2019/07/26 Python
python 密码学示例——凯撒密码的实现
2020/09/21 Python
Python中对象的比较操作==和is区别详析
2021/02/12 Python
CSS3径向渐变之大鱼吃小鱼之孤单的大鱼
2016/04/26 HTML / CSS
英国最大的汽车交易网站:Auto Trader UK
2016/09/23 全球购物
大学生自我鉴定评语
2014/01/27 职场文书
体育节口号
2014/06/19 职场文书
小学综治宣传月活动总结
2014/07/02 职场文书
民主生活会对照检查材料
2014/09/22 职场文书
python实现socket简单通信的示例代码
2021/04/13 Python
如何利用golang运用mysql数据库
2022/03/13 Golang