import threading, os
from playsound import *
from PyQt5 import QtCore, QtGui, QtWidgets
from os import path


class Ui_MainWindow(object):
    def play(self, path):
        try:
            print(f'Playing {path}')
            threading.Thread(target=playsound, args=(path,), daemon=True).start()
        except PlaysoundException:
            print(f'Error: No files named \'{path}\'')




    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1007, 766)
        self.main = QtWidgets.QWidget(MainWindow)
        self.main.setObjectName("main")
        self.verticalLayoutWidget = QtWidgets.QWidget(self.main)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(0, 0, 1001, 80))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.label = QtWidgets.QLabel(self.verticalLayoutWidget)
        font = QtGui.QFont()
        font.setFamily("Source Code Pro")
        font.setPointSize(26)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label)
        self.frame = QtWidgets.QFrame(self.main)
        self.frame.setGeometry(QtCore.QRect(0, 80, 1001, 681))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.frame.sizePolicy().hasHeightForWidth())
        self.frame.setSizePolicy(sizePolicy)
        self.frame.setMinimumSize(QtCore.QSize(1001, 681))
        self.frame.setMaximumSize(QtCore.QSize(1001, 681))
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.groupBox = QtWidgets.QGroupBox(self.frame)
        self.groupBox.setGeometry(QtCore.QRect(10, 10, 981, 101))
        self.groupBox.setObjectName("groupBox")
        self.play1 = QtWidgets.QPushButton(self.groupBox)
        self.play1.setGeometry(QtCore.QRect(870, 40, 84, 34))
        self.play1.setObjectName("play1")
        self.path1 = QtWidgets.QLineEdit(self.groupBox)
        self.path1.setGeometry(QtCore.QRect(20, 40, 191, 32))
        self.path1.setObjectName("path1")
        self.groupBox_2 = QtWidgets.QGroupBox(self.frame)
        self.groupBox_2.setGeometry(QtCore.QRect(10, 130, 981, 101))
        self.groupBox_2.setObjectName("groupBox_2")
        self.play2 = QtWidgets.QPushButton(self.groupBox_2)
        self.play2.setGeometry(QtCore.QRect(870, 40, 84, 34))
        self.play2.setObjectName("play2")
        self.path2 = QtWidgets.QLineEdit(self.groupBox_2)
        self.path2.setGeometry(QtCore.QRect(20, 40, 191, 32))
        self.path2.setObjectName("path2")
        self.groupBox_3 = QtWidgets.QGroupBox(self.frame)
        self.groupBox_3.setGeometry(QtCore.QRect(10, 250, 981, 101))
        self.groupBox_3.setObjectName("groupBox_3")
        self.play3 = QtWidgets.QPushButton(self.groupBox_3)
        self.play3.setGeometry(QtCore.QRect(870, 40, 84, 34))
        self.play3.setObjectName("play3")
        self.path3 = QtWidgets.QLineEdit(self.groupBox_3)
        self.path3.setGeometry(QtCore.QRect(20, 40, 191, 32))
        self.path3.setObjectName("path3")
        self.groupBox_4 = QtWidgets.QGroupBox(self.frame)
        self.groupBox_4.setGeometry(QtCore.QRect(10, 370, 981, 101))
        self.groupBox_4.setObjectName("groupBox_4")
        self.play4 = QtWidgets.QPushButton(self.groupBox_4)
        self.play4.setGeometry(QtCore.QRect(870, 40, 84, 34))
        self.play4.setObjectName("play4")
        self.path4 = QtWidgets.QLineEdit(self.groupBox_4)
        self.path4.setGeometry(QtCore.QRect(20, 40, 191, 32))
        self.path4.setObjectName("path4")
        self.groupBox_5 = QtWidgets.QGroupBox(self.frame)
        self.groupBox_5.setGeometry(QtCore.QRect(10, 490, 981, 101))
        self.groupBox_5.setObjectName("groupBox_5")
        self.play5 = QtWidgets.QPushButton(self.groupBox_5)
        self.play5.setGeometry(QtCore.QRect(870, 40, 84, 34))
        self.play5.setObjectName("play5")
        self.path5 = QtWidgets.QLineEdit(self.groupBox_5)
        self.path5.setGeometry(QtCore.QRect(20, 40, 191, 32))
        self.path5.setObjectName("path5")
        self.verticalLayoutWidget_2 = QtWidgets.QWidget(self.frame)
        self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(9, 609, 981, 61))
        self.verticalLayoutWidget_2.setObjectName("verticalLayoutWidget_2")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget_2)
        self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.stopall = QtWidgets.QPushButton(self.frame)
        self.stopall.setGeometry(QtCore.QRect(450, 620, 101, 34))
        self.stopall.setObjectName("stopall")
        MainWindow.setCentralWidget(self.main)

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

        self.play1.clicked.connect(lambda: self.play(self.path1.text()))
        self.play2.clicked.connect(lambda: self.play(self.path2.text()))
        self.play3.clicked.connect(lambda: self.play(self.path3.text()))
        self.play4.clicked.connect(lambda: self.play(self.path4.text()))
        self.play5.clicked.connect(lambda: self.play(self.path5.text()))

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "SoundBox by NameUndefined"))
        self.label.setText(_translate("MainWindow", "Sounds"))
        self.groupBox.setTitle(_translate("MainWindow", "Sound 1"))
        self.play1.setText(_translate("MainWindow", "Play"))
        self.path1.setPlaceholderText(_translate("MainWindow", "Path to sound"))
        self.groupBox_2.setTitle(_translate("MainWindow", "Sound 2"))
        self.play2.setText(_translate("MainWindow", "Play"))
        self.path2.setPlaceholderText(_translate("MainWindow", "Path to sound"))
        self.groupBox_3.setTitle(_translate("MainWindow", "Sound 3"))
        self.play3.setText(_translate("MainWindow", "Play"))
        self.path3.setPlaceholderText(_translate("MainWindow", "Path to sound"))
        self.groupBox_4.setTitle(_translate("MainWindow", "Sound 4"))
        self.play4.setText(_translate("MainWindow", "Play"))
        self.path4.setPlaceholderText(_translate("MainWindow", "Path to sound"))
        self.groupBox_5.setTitle(_translate("MainWindow", "Sound 5"))
        self.play5.setText(_translate("MainWindow", "Play"))
        self.path5.setPlaceholderText(_translate("MainWindow", "Path to sound"))
        self.stopall.setText(_translate("MainWindow", "Stop all"))


if __name__ == "__main__":
    import sys
    
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
