pyqt5中常用元件、小方法的使用记录(打开文件、显示图片、背景加图片、提示框)

使用习惯

变量命名

使用元件时最好给每个元件单独加后缀
在这里插入图片描述

各个元件的基本使用

按钮

相关资料:《快速掌握PyQt5》第七章 各种按钮介绍

class Test_window(QtWidgets.QMainWindow,Test_UI.Ui_MainWindow):
    def __init__(self,parent=None):
        super(Test_window,self).__init__(parent)
        self.setupUi(self)

        self.pushButton_OpenFile.clicked.connect(self.button_test)

    def button_test(self):
        print(self.pushButton_OpenFile.isChecked())

Label

参考资料:PyQt5 标签居中显示图片(QLabel)

常用小功能

打开文件或文件夹

基本方法

1.单个文件打开

QFileDialog.getOpenFileName() 

2.多个文件打开

QFileDialog.getOpenFileNames() 

3.文件夹选取

QFileDialog.getExistingDirectory() 

4.文件保存

QFileDialog.getSaveFileName() 

示例:

from PyQt5 import QtWidgets,QtCore
from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5.QtWidgets import QFileDialog
import sys

import Test_UI

class Test_window(QtWidgets.QMainWindow,Test_UI.Ui_MainWindow):
    def __init__(self,parent=None):
        super(Test_window,self).__init__(parent)
        self.setupUi(self)

        self.pushButton_OpenFile.clicked.connect(self.msg)

    def msg(self):
        directory1 = QFileDialog.getExistingDirectory(self,
                                                      "选取文件夹",
                                                      "./")  # 起始路径
        print(directory1)

        fileName1, filetype = QFileDialog.getOpenFileName(self,
                                                          "选取文件",
                                                          "./",
                                                          "All Files (*);;Text Files (*.txt)")  # 设置文件扩展名过滤,注意用双分号间隔
        print(fileName1, filetype)

        files, ok1 = QFileDialog.getOpenFileNames(self,
                                                  "多文件选择",
                                                  "./",
                                                  "All Files (*);;Text Files (*.txt)")
        print(files, ok1)

        fileName2, ok2 = QFileDialog.getSaveFileName(self,
                                                     "文件保存",
                                                     "./",
                                                     "All Files (*);;Text Files (*.txt)")



if __name__ == '__main__':
    app = QApplication(sys.argv)
    mytest = Test_window()
    mytest.show()
    app.exec_()

打开文件且显示图片

导入关键头文件QtGui

from PyQt5 import QtWidgets,QtCore,QtGui
class Test_window(QtWidgets.QMainWindow,Test_UI.Ui_MainWindow):
    def __init__(self,parent=None):
        super(Test_window,self).__init__(parent)
        self.setupUi(self)

        self.pushButton_OpenFile.clicked.connect(self.openimage)

    def openimage(self):
        imgName, imgType = QFileDialog.getOpenFileName(self, "打开图片", "", "*.jpg;;*.png;;All Files(*)")
        jpg = QtGui.QPixmap(imgName).scaled(self.label_ShowImg.width(), self.label_ShowImg.height())
        self.label_ShowImg.setPixmap(jpg)

在这里插入图片描述

插入背景图片qcc

创建一个.qrc文件

<!DOCTYPE RCC>
<RCC version="1.0">
<qresource prefix="/">
    <file>images/logo.png</file>

</qresource>
</RCC>

然后使用PTQCC转换,就可以直接在QtDesigner里面使用这些图片
在这里插入图片描述

提示框

可以设置不同提示框

QMessageBox.information,提示的样式

QMessageBox.question,询问的样式

QMessageBox.warning,警告的样式

QMessageBox.critical,错误的样式

使用方法

QMessageBox.information()由3个参数组成,第一个是窗口,第二个是标题,第三个是提示信息,第四个是按钮

from PyQt5.QtWidgets import QMessageBox, QWidget
widget = QWidget()
QMessageBox.warning(widget,'警告','没有路径无法打开!',QMessageBox.Close,)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玛卡巴卡_qin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值