pyqt5使用Designer实现按钮上传图片

pyqt5使用Designer实现按钮上传图片

1、ui界面

2、ui转py代码

其中uploadimg.py代码如下:

python 复制代码
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'uploadimg.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.img_show = QtWidgets.QLabel(self.centralwidget)
        self.img_show.setGeometry(QtCore.QRect(41, 50, 711, 351))
        self.img_show.setObjectName("img_show")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(30, 10, 72, 15))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(50, 430, 41, 31))
        self.label_3.setObjectName("label_3")
        self.img_path = QtWidgets.QLineEdit(self.centralwidget)
        self.img_path.setGeometry(QtCore.QRect(100, 430, 371, 31))
        self.img_path.setObjectName("img_path")
        self.btn_upload = QtWidgets.QPushButton(self.centralwidget)
        self.btn_upload.setGeometry(QtCore.QRect(480, 430, 93, 28))
        self.btn_upload.setObjectName("btn_upload")
        self.img_list = QtWidgets.QComboBox(self.centralwidget)
        self.img_list.setGeometry(QtCore.QRect(100, 480, 371, 31))
        self.img_list.setObjectName("img_list")
        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(30, 480, 61, 31))
        self.label_4.setObjectName("label_4")
        self.btn_sure = QtWidgets.QPushButton(self.centralwidget)
        self.btn_sure.setGeometry(QtCore.QRect(480, 480, 93, 28))
        self.btn_sure.setObjectName("btn_sure")
        self.btn_next = QtWidgets.QPushButton(self.centralwidget)
        self.btn_next.setGeometry(QtCore.QRect(590, 480, 93, 28))
        self.btn_next.setObjectName("btn_next")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 27))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.img_show.setText(_translate("MainWindow", "图片"))
        self.label_2.setText(_translate("MainWindow", "图片"))
        self.label_3.setText(_translate("MainWindow", "路径"))
        self.btn_upload.setText(_translate("MainWindow", "加载"))
        self.label_4.setText(_translate("MainWindow", "图片列表"))
        self.btn_sure.setText(_translate("MainWindow", "确定"))
        self.btn_next.setText(_translate("MainWindow", "下一张"))

3、upload_main.py主函数代码

upload_main.py代码如下:

python 复制代码
# -*- coding: utf-8 -*-

"""
@contact: 微信 1257309054
@file: upload_main.py
@time: 2023/12/10 11:48
@author: LDC
"""
import os
import sys

from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QMainWindow, QApplication

from upload_img.uploadimg import Ui_MainWindow


class Window(QMainWindow, Ui_MainWindow):
    def __init__(self, app):
        super(QMainWindow, self).__init__()
        self.app = app
        self.img_len = 0  # 图片列表
        self.setup_ui()  # 渲染画布
        self.connect_signals()  # 绑定触发事件

    def setup_ui(self):
        self.setupUi(self)

    def connect_signals(self):
        # 绑定触发事件
        self.btn_upload.clicked.connect(self.btn_upload_clicked)
        self.btn_sure.clicked.connect(self.btn_sure_clicked)
        self.btn_next.clicked.connect(self.btn_next_clicked)

    def btn_upload_clicked(self):
        # 点击加载按钮
        self.file_path = self.img_path.text()
        img_list = os.listdir(self.file_path)
        self.img_len = len(img_list)
        self.img_list.addItems([img_list[i] for i in range(self.img_len)])

    def btn_sure_clicked(self):
        # 点击确定按钮
        img = self.img_list.currentText()
        pix = QPixmap(self.file_path + "\\" + img)
        self.img_show.setPixmap(pix)
        self.img_show.setScaledContents(True)

    def btn_next_clicked(self):
        # 点击下一张
        if not self.img_len:
            return
        current_next = self.img_list.currentIndex() + 1

        if current_next >= self.img_len:
            current_next = 0
        self.img_list.setCurrentIndex(current_next)
        self.btn_sure_clicked()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mywindow = Window(app)
    mywindow.show()
    sys.exit(app.exec_())

4、效果图

相关推荐
tangliang_cn20 分钟前
java入门 自定义springboot starter
java·开发语言·spring boot
程序猿阿伟20 分钟前
《智能指针频繁创建销毁:程序性能的“隐形杀手”》
java·开发语言·前端
新知图书31 分钟前
Rust编程与项目实战-模块std::thread(之一)
开发语言·后端·rust
威威猫的栗子33 分钟前
Python Turtle召唤童年:喜羊羊与灰太狼之懒羊羊绘画
开发语言·python
力透键背33 分钟前
display: none和visibility: hidden的区别
开发语言·前端·javascript
bluefox197934 分钟前
使用 Oracle.DataAccess.Client 驱动 和 OleDB 调用Oracle 函数的区别
开发语言·c#
ö Constancy1 小时前
c++ 笔记
开发语言·c++
墨染风华不染尘1 小时前
python之开发笔记
开发语言·笔记·python
徐霞客3201 小时前
Qt入门1——认识Qt的几个常用头文件和常用函数
开发语言·c++·笔记·qt
LKID体2 小时前
Python操作neo4j库py2neo使用之py2neo 删除及事务相关操作(三)
开发语言·python·neo4j