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、效果图

相关推荐
上海_彭彭18 分钟前
【提效工具开发】Python功能模块执行和 SQL 执行 需求整理
开发语言·python·sql·测试工具·element
3345543227 分钟前
element动态表头合并表格
开发语言·javascript·ecmascript
沈询-阿里31 分钟前
java-智能识别车牌号_基于spring ai和开源国产大模型_qwen vl
java·开发语言
残月只会敲键盘44 分钟前
面相小白的php反序列化漏洞原理剖析
开发语言·php
ac-er88881 小时前
PHP弱类型安全问题
开发语言·安全·php
ac-er88881 小时前
PHP网络爬虫常见的反爬策略
开发语言·爬虫·php
爱吃喵的鲤鱼1 小时前
linux进程的状态之环境变量
linux·运维·服务器·开发语言·c++
DARLING Zero two♡1 小时前
关于我、重生到500年前凭借C语言改变世界科技vlog.16——万字详解指针概念及技巧
c语言·开发语言·科技
7年老菜鸡1 小时前
策略模式(C++)三分钟读懂
c++·qt·策略模式
Gu Gu Study1 小时前
【用Java学习数据结构系列】泛型上界与通配符上界
java·开发语言