Guest User

PyQt5 ply

a guest
Oct 15th, 2021
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.67 KB | None | 0 0
  1. import random
  2. import pygame
  3. import math
  4. import sys
  5. import os
  6. from PyQt5 import QtCore, QtGui, QtWidgets, uic
  7. from PyQt5.QtWidgets import *
  8. from PyQt5.Qt import *
  9.  
  10. from pygame import mixer
  11.  
  12.  
  13. class Ui_MainWindow(object):
  14.     def setupUi(self, MainWindow):
  15.         MainWindow.setObjectName("MainWindow")
  16.         MainWindow.setFixedSize(640, 402)
  17.         MainWindow.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.182, y1:0.102409, x2:1, y2:1, stop:0.113636 rgba(0, 62, 79, 147), stop:0.306818 rgba(28, 14, 68, 156));")
  18.         self.centralwidget = QtWidgets.QWidget(MainWindow)
  19.         self.centralwidget.setObjectName("centralwidget")
  20.         self.label = QtWidgets.QLabel(self.centralwidget)
  21.         self.label.setGeometry(QtCore.QRect(-130, -40, 1031, 491))
  22.         self.label.setText("")
  23.         self.label.setPixmap(QtGui.QPixmap("2HYI.gif"))
  24.         self.label.setObjectName("label")
  25.         self.widget = QtWidgets.QWidget(self.centralwidget)
  26.         self.widget.setGeometry(QtCore.QRect(0, 310, 641, 80))
  27.         self.widget.setObjectName("widget")
  28.         self.listWidget = QtWidgets.QListWidget(self.centralwidget) #Начиная отсюда list
  29.         self.listWidget.setGeometry(QtCore.QRect(0, 0, 121, 311))
  30.         self.listWidget.setStyleSheet("font: 75 italic 8pt \"MS Sans Serif\";\n"
  31. "color: rgb(121, 92, 121);")
  32.         self.listWidget.setObjectName("listWidget")    
  33.         MainWindow.setCentralWidget(self.centralwidget)
  34.         self.statusbar = QtWidgets.QStatusBar(MainWindow)
  35.         self.statusbar.setObjectName("statusbar")
  36.         MainWindow.setStatusBar(self.statusbar)
  37.         self.retranslateUi(MainWindow)
  38.         QtCore.QMetaObject.connectSlotsByName(MainWindow)
  39.  
  40.     def retranslateUi(self, MainWindow):
  41.         _translate = QtCore.QCoreApplication.translate
  42.         MainWindow.setWindowTitle(_translate("Player", "Player"))
  43.  
  44.  
  45.  
  46. class MainWindow(QMainWindow, Ui_MainWindow,):                
  47.     def __init__(self):                        
  48.         super().__init__()    
  49.        
  50.         self.setupUi(self)
  51.  
  52.         self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
  53.         self.old_pos = None
  54.  
  55.        
  56.         self.listWidget.clear()
  57.         self.listWidget.itemClicked.connect(self.onClicked)
  58.        
  59.         self.pushbutton_close = QPushButton('✖', self)
  60.  
  61.         self.pushbutton_stop = QPushButton('✘', self)
  62.        
  63.         self.pushbutton_pause = QPushButton('■', self)
  64.        
  65.         self.pushbutton_remove = QPushButton('⏵', self)
  66.  
  67.         self.btn = QPushButton('FILE', self)
  68.  
  69.    
  70.         self.itemlist = ['ВыборПапкиНаFile']
  71.         self.listWidget.addItems(self.itemlist)
  72.  
  73.         self.pushbutton_close.clicked.connect(self.close)
  74.         self.pushbutton_close.setStyleSheet("border:none;background-color: rgb(255, 51, 11);color: rgb(255, 255, 255);")
  75.         self.pushbutton_close.move(200,500)
  76.         self.pushbutton_close.setGeometry(560, 0, 40, 20)
  77.  
  78.         self.pushbutton_stop.clicked.connect(self.clickstop)
  79.         self.pushbutton_stop.setStyleSheet("background-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0.113636 rgba(0, 62, 79, 147), stop:0.306818 rgba(28, 14, 68, 156));background-color: rgb(130, 68, 206);")
  80.         self.pushbutton_stop.move(200,500)
  81.         self.pushbutton_stop.setGeometry(0, 310, 40, 20)
  82.        
  83.         self.pushbutton_pause.clicked.connect(self.clickpause)
  84.         self.pushbutton_pause.setStyleSheet("background-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0.113636 rgba(0, 62, 79, 147), stop:0.306818 rgba(28, 14, 68, 156));background-color: rgb(130, 68, 206);")
  85.         self.pushbutton_pause.move(200,500)
  86.         self.pushbutton_pause.setGeometry(38, 310, 40, 20)
  87.        
  88.         self.pushbutton_remove.clicked.connect(self.clickremove)
  89.         self.pushbutton_remove.setStyleSheet("background-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0.113636 rgba(0, 62, 79, 147), stop:0.306818 rgba(28, 14, 68, 156));background-color: rgb(130, 68, 206);")
  90.         self.pushbutton_remove.move(200,500)
  91.         self.pushbutton_remove.setGeometry(0, 310, 40, 20)
  92.  
  93.         self.btn.clicked.connect(self.update_dir)
  94.         self.btn.setStyleSheet("border:none;background-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0.113636 rgba(0, 62, 79, 147), stop:0.306818 rgba(28, 14, 68, 156));background-color: rgb(130, 68, 206);")
  95.         self.btn.move(200,500)
  96.         self.btn.setGeometry(600, 0, 40, 20)
  97.        
  98.         self.pushbutton_stop.hide()
  99.         self.pushbutton_remove.hide()
  100.         self.pushbutton_pause.hide()
  101.         self.listWidget.itemClicked.connect(self.onClicked)
  102.  
  103.     def onClicked(self, item):      #Самая важная часть, при нажатии на элемент списка начинает проигрываться музыка из папки.
  104.         path='2HYI.gif'
  105.         gif = QtGui.QMovie(path)
  106.         self.label.setMovie(gif)
  107.         gif.start()
  108.         try:
  109.             path_file = item.data(QtCore.Qt.UserRole)
  110.             with open(path_file, 'r') as file:
  111.                 self.pushbutton_remove.hide()
  112.                 self.pushbutton_stop.show()
  113.                 self.pushbutton_pause.show()
  114.            
  115.                 pygame.init()
  116.                 name = item.text()
  117.                 pygame.mixer.music.load(name)
  118.                 pygame.mixer.music.play(1)
  119.         except Exception:
  120.             self.pushbutton_stop.hide()
  121.             self.pushbutton_pause.hide()
  122.             self.pushbutton_remove.hide()
  123.  
  124.  
  125.     def close(self):
  126.         exit()
  127.    
  128.     def clickstop(self):            #!!!
  129.         path='2HYI.gif'
  130.         gif = QtGui.QMovie(path)
  131.         self.label.setMovie(gif)
  132.         gif.stop()
  133.         self.label.setPixmap(QtGui.QPixmap("2HYI.gif"))
  134.         try:
  135.             self.pushbutton_stop.hide()
  136.             self.pushbutton_pause.hide()
  137.             self.pushbutton_remove.hide()
  138.             pygame.mixer.music.stop()
  139.             pygame.quit()
  140.         except Exception:
  141.             pass
  142.    
  143.     def clickpause(self):                   #!!!
  144.         path='2HYI.gif'
  145.         gif = QtGui.QMovie(path)
  146.         self.label.setMovie(gif)
  147.         gif.stop()
  148.         self.label.setPixmap(QtGui.QPixmap("2HYI.gif"))
  149.         try:
  150.             self.pushbutton_pause.hide()
  151.             self.pushbutton_remove.show()
  152.            
  153.             pygame.mixer.music.pause()
  154.         except Exception:
  155.             pass
  156.    
  157.     def clickremove(self):                  #!!!
  158.         path='2HYI.gif'
  159.         gif = QtGui.QMovie(path)
  160.         self.label.setMovie(gif)
  161.         gif.start()
  162.         try:
  163.             self.pushbutton_pause.show()
  164.             self.pushbutton_remove.hide()
  165.            
  166.             pygame.mixer.music.unpause()
  167.         except Exception:
  168.             pass
  169.        
  170.     def closeEvent(self, event):            #!!!
  171.         try:
  172.             pygame.mixer.music.stop()
  173.             pygame.quit()
  174.         except Exception:
  175.             pass
  176.         finally:
  177.             pygame.quit()
  178.  
  179.  
  180.  
  181.  
  182.     def update_dir(self):                                               #!!!
  183.         path='2HYI.gif'
  184.         gif = QtGui.QMovie(path)
  185.         self.label.setMovie(gif)
  186.         gif.stop()
  187.         self.label.setPixmap(QtGui.QPixmap("2HYI.gif"))
  188.         try:
  189.             self.pushbutton_pause.hide()
  190.             self.pushbutton_remove.show()
  191.            
  192.             pygame.mixer.music.pause()
  193.         except Exception:
  194.             pass
  195.         directory = QFileDialog.getExistingDirectory(
  196.             self,
  197.             "QFileDialog.getExistingDirectory()",
  198.             '.'
  199.         )
  200.         if directory:
  201.             _list = [ file for file in os.listdir(f"{directory}") if file.endswith(".mp3") ] #/*.txt")]
  202.             if not _list:
  203.                 return
  204.             self.listWidget.clear()
  205.             for i, item in enumerate(_list):
  206.                 self.listWidget.addItem(item)
  207.                 item_data = os.path.join(directory, item).replace('\\', '/')
  208.                 self.listWidget.item(i).setData(QtCore.Qt.UserRole, item_data)
  209.  
  210.     def mousePressEvent(self, event):        
  211.         if event.button() == QtCore.Qt.LeftButton:
  212.             self.old_pos = event.pos()
  213.  
  214.  
  215.     def mouseReleaseEvent(self, event):
  216.         if event.button() == QtCore.Qt.LeftButton:
  217.             self.old_pos = None
  218.  
  219.  
  220.     def mouseMoveEvent(self, event):
  221.         if not self.old_pos:
  222.             return
  223.         delta = event.pos() - self.old_pos
  224.         self.move(self.pos() + delta)
  225.  
  226.  
  227. if __name__ == "__main__":
  228.     import sys
  229.     app = QtWidgets.QApplication(sys.argv)
  230.     w = MainWindow()
  231.     w.show()
  232.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment