欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-08-27 23:31:47
如下所示: # -*- coding: utf-8 -*- import sys import os from pyqt5 import qtcore, q...

如下所示:

# -*- 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 实现 下拉菜单 + 打开文件的示例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。