【问题标题】:python ERROR: can not show .gif in a qlabelpython 错误:无法在 qlabel 中显示 .gif
【发布时间】:2018-10-19 01:15:07
【问题描述】:

晚上好,我正在尝试通过按下按钮在 QLABEL 中生成动画,然后在该动画之后转到下一个窗口。

这是我正在做的一个总结示例,因为在我的原始项目中,由于它执行的过程,进入程序大约需要 5 秒,所以我的意图是,当那个时间在 QLABEL 中运行动画时.

为此我使用此代码:

main.py

from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import uic
from FirebaseLogin import *
from contraInicio import ContraInicioC
#from Principal import PrincipalC
#from MostrarDatosTabla import mostararDatos
#from CrearFirebase import crear_tabla
#from sucursalInicio import sucursal
#import sqlite3

class LoginC(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("Interfaz/Login.ui",self)

        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.l_permiso.setAttribute(QtCore.Qt.WA_NoMousePropagation)
        self.l_sucursal.setAttribute(QtCore.Qt.WA_NoMousePropagation)

        self.l_close.clicked.connect(self.close)

        self.contraseña = ContraInicioC()
        #self.principal = PrincipalC()
        self.l_entrar.clicked.connect(self.setDataPrincipal)
        self.l_permiso.currentIndexChanged.connect(lambda:self.cambioPermiso(self.l_permiso.currentText()))
        self.l_permiso.setCurrentText("Caja")
        self.suc = "Exxe 1"
        self.l_sucursal.setCurrentText(self.suc)
        #self.principal.pc_fecha.setDate(QtCore.QDate.currentDate())



    @QtCore.pyqtSlot(str)
    def cambioPermiso(self,permiso):
        if permiso == "Administrador":

            self.l_sucursal.setEnabled(True)
        else:

            self.l_sucursal.setEnabled(False)

    def setDataPrincipal(self):
        movi = QtGui.QMovie("giphy.gif")
        self.label.setMovie(movi)
        movi.start()

        self.estado = 0
        fechaActual = QtCore.QDate.currentDate().toPyDate()

        ref = db.reference("/"+str(fechaActual))
        ret = ref.get()

        if ret == None:

            self.contraseña.show()
        else:

            self.contraseña.show()



        self.close()

    def mousePressEvent(self,event):

        if event.button() == QtCore.Qt.LeftButton:
            self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
            event.accept()


    def mouseMoveEvent(self,event):
        if event.buttons() == QtCore.Qt.LeftButton:
            self.move(event.globalPos() - self.dragPosition)
            event.accept()

app = QApplication([])
l = LoginC()
l.show()
app.exec_()

contraInicio.py

from PyQt5.QtWidgets import QDialog, QApplication, QMessageBox
from PyQt5 import QtCore
from PyQt5 import uic
#from Contraseñas import passwordLogin
#from Principal import PrincipalC
#from MostrarDatosTabla import mostararDatos
#from CrearFirebase import crear_tabla


class ContraInicioC(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        uic.loadUi("Interfaz/Contraseña.ui",self)

        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        #self.principal = PrincipalC()
        #boton de validación
        self.validar.clicked.connect(self.validacion)
        self.c1Close.clicked.connect(self.close)

        self.permiso = None
        self.turno = None
        self.sucursal = None
        self.estado = None
        self.referencia = None


    def validacion(self):
        #self.datos(self.permiso,self.turno,self.sucursal,self.estado,self.referencia)
        if self.password.text() == "esther":
            print("ok")

        else:
            print("error")



    def mousePressEvent(self,event):

        if event.button() == QtCore.Qt.LeftButton:
            self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
            event.accept()


    def mouseMoveEvent(self,event):
        if event.buttons() == QtCore.Qt.LeftButton:
            self.move(event.globalPos() - self.dragPosition)
            event.accept()
        #mostararDatos(str(QtCore.QDate.currentDate().toPyDate()),sucursal,turno,self.principal.saldo,self.principal.tablaM,self.principal.inventario,referencia)




#app = QApplication([])
#l = ContraInicioC()
#l.show()
#app.exec_()

登录.ui

<?xml version="1.0" encoding="UTF-8"?>
  <ui version="4.0">
   <class>MainWindow</class>
   <widget class="QMainWindow" name="MainWindow">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>650</width>
      <height>370</height>
     </rect>
    </property>
    <property name="windowTitle">
     <string>MainWindow</string>
    </property>
    <property name="styleSheet">
     <string notr="true">background:url(Imagenes/escalado.jpg);
  background-position:center;
  background-repeat:none;</string>
    </property>
    <widget class="QWidget" name="centralwidget">
     <widget class="QPushButton" name="l_close">
      <property name="geometry">
       <rect>
        <x>620</x>
        <y>0</y>
        <width>31</width>
        <height>23</height>
       </rect>
      </property>
      <property name="font">
       <font>
        <pointsize>9</pointsize>
        <weight>75</weight>
        <bold>true</bold>
       </font>
      </property>
      <property name="styleSheet">
       <string notr="true">QPushButton#l_close{
  background:none;
  border:0px;
  color:grey;
  }
  QPushButton#l_close:hover{
  color:red;
  }</string>
      </property>
      <property name="text">
       <string>X</string>
      </property>
     </widget>
     <widget class="QPushButton" name="l_entrar">
      <property name="geometry">
       <rect>
        <x>550</x>
        <y>330</y>
        <width>75</width>
        <height>22</height>
       </rect>
      </property>
      <property name="styleSheet">
       <string notr="true">QPushButton#l_entrar{
  background:#41CD52;
  border:0px;
  color:white;
  }
  QPushButton#l_entrar:pressed{
  border-bottom:2px solid green;
  }
  </string>
      </property>
      <property name="text">
       <string>Entrar</string>
      </property>
     </widget>
     <widget class="QComboBox" name="l_sucursal">
      <property name="enabled">
       <bool>false</bool>
      </property>
      <property name="geometry">
       <rect>
        <x>440</x>
        <y>330</y>
        <width>69</width>
        <height>22</height>
       </rect>
      </property>
      <property name="styleSheet">
       <string notr="true">QComboBox#l_sucursal{
  background:none;
  border:0px;
  border-bottom: 1px solid lightblue;
  color:#05839C;
  }


  QComboBox QAbstractItemView
  {
      border:0px;
      background:white;
      color: grey;

  }
  QComboBox#l_sucursal::drop-down
  {
       subcontrol-origin: padding;
       subcontrol-position: top right;
       width: 15px;
       color: white;
       border-left-width: 0px;
       border-left-color: darkgray;
       border-left-style: solid; /* just a single line */
       border-top-right-radius: 3px; /* same radius as the QComboBox */
       border-bottom-right-radius: 3px;
       padding-left: 10px;
   }</string>
      </property>
      <item>
       <property name="text">
        <string>Exxe 1</string>
       </property>
      </item>
      <item>
       <property name="text">
        <string>Exxe 3</string>
       </property>
      </item>
     </widget>
     <widget class="QLabel" name="lsucursal">
      <property name="geometry">
       <rect>
        <x>380</x>
        <y>330</y>
        <width>51</width>
        <height>22</height>
       </rect>
      </property>
      <property name="styleSheet">
       <string notr="true">color:grey;
  background:none;</string>
      </property>
      <property name="text">
       <string>Sucursal:</string>
      </property>
     </widget>
     <widget class="QLabel" name="la">
      <property name="geometry">
       <rect>
        <x>220</x>
        <y>330</y>
        <width>51</width>
        <height>22</height>
       </rect>
      </property>
      <property name="styleSheet">
       <string notr="true">color:grey;
  background:none;</string>
      </property>
      <property name="text">
       <string>Permisos</string>
      </property>
     </widget>
     <widget class="QComboBox" name="l_permiso">
      <property name="geometry">
       <rect>
        <x>268</x>
        <y>330</y>
        <width>91</width>
        <height>22</height>
       </rect>
      </property>
      <property name="styleSheet">
       <string notr="true">QComboBox#l_permiso{
  background:none;
  border:0px;
  border-bottom: 1px solid lightblue;
  color:#05839C;
  }


  QComboBox QAbstractItemView
  {
      border:0px;
      background:white;
      color: grey;

  }
  QComboBox#l_permiso::drop-down
  {
       subcontrol-origin: padding;
       subcontrol-position: top right;
       width: 15px;
       color: white;
       border-left-width: 0px;
       border-left-color: darkgray;
       border-left-style: solid; /* just a single line */
       border-top-right-radius: 3px; /* same radius as the QComboBox */
       border-bottom-right-radius: 3px;
       padding-left: 10px;
   }</string>
      </property>
      <item>
       <property name="text">
        <string>Administrador</string>
       </property>
      </item>
      <item>
       <property name="text">
        <string>Caja</string>
       </property>
      </item>
     </widget>
     <widget class="QLabel" name="label">
      <property name="geometry">
       <rect>
        <x>110</x>
        <y>100</y>
        <width>231</width>
        <height>111</height>
       </rect>
      </property>
      <property name="text">
       <string/>
      </property>
     </widget>
    </widget>
   </widget>
   <resources/>
   <connections/>
  </ui>

Contraseña.ui

<?xml version="1.0" encoding="UTF-8"?>
  <ui version="4.0">
   <class>Dialog</class>
   <widget class="QDialog" name="Dialog">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>302</width>
      <height>156</height>
     </rect>
    </property>
    <property name="windowTitle">
     <string>Validación</string>
    </property>
    <property name="styleSheet">
     <string notr="true">background:rgb(116, 116, 116);</string>
    </property>
    <widget class="QLabel" name="label">
     <property name="geometry">
      <rect>
       <x>80</x>
       <y>20</y>
       <width>141</width>
       <height>20</height>
      </rect>
     </property>
     <property name="font">
      <font>
       <pointsize>9</pointsize>
      </font>
     </property>
     <property name="styleSheet">
      <string notr="true">color:white;</string>
     </property>
     <property name="text">
      <string>Ingrese clave de acceso</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
    </widget>
    <widget class="QLineEdit" name="password">
     <property name="geometry">
      <rect>
       <x>60</x>
       <y>60</y>
       <width>181</width>
       <height>21</height>
      </rect>
     </property>
     <property name="styleSheet">
      <string notr="true">background:none</string>
     </property>
     <property name="echoMode">
      <enum>QLineEdit::Password</enum>
     </property>
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
    </widget>
    <widget class="QPushButton" name="validar">
     <property name="geometry">
      <rect>
       <x>200</x>
       <y>120</y>
       <width>75</width>
       <height>23</height>
      </rect>
     </property>
     <property name="styleSheet">
      <string notr="true">QPushButton#validar{
  background:rgb(56, 213, 47);
  border:0px;
  color:white;
  }
  QPushButton#validar:pressed{
  border-bottom:2px solid green;
  }</string>
     </property>
     <property name="text">
      <string>Validar</string>
     </property>
     <property name="default">
      <bool>true</bool>
     </property>
    </widget>
    <widget class="QFrame" name="frame">
     <property name="geometry">
      <rect>
       <x>0</x>
       <y>0</y>
       <width>311</width>
       <height>16</height>
      </rect>
     </property>
     <property name="styleSheet">
      <string notr="true">background:#0F5D8D;</string>
     </property>
     <property name="frameShape">
      <enum>QFrame::StyledPanel</enum>
     </property>
     <property name="frameShadow">
      <enum>QFrame::Raised</enum>
     </property>
    </widget>
    <widget class="QPushButton" name="c1Close">
     <property name="geometry">
      <rect>
       <x>286</x>
       <y>0</y>
       <width>16</width>
       <height>16</height>
      </rect>
     </property>
     <property name="font">
      <font>
       <pointsize>9</pointsize>
       <weight>75</weight>
       <bold>true</bold>
      </font>
     </property>
     <property name="styleSheet">
      <string notr="true">color:white;
  background:none;
  border:0px;</string>
     </property>
     <property name="text">
      <string>X</string>
     </property>
    </widget>
   </widget>
   <resources/>
   <connections/>
  </ui>

FirebaseLogin.py

import firebase_admin
from firebase_admin import credentials, db

cred = credentials.Certificate('login.json')
firebase_admin.initialize_app(cred,{
    'databaseURL':'https://exe-tablas.firebaseio.com/'
})

【问题讨论】:

    标签: python python-3.x pyqt pyqt5 qlabel


    【解决方案1】:

    由于问题不是QMovie,问题是对Firebase的请求,这是一个阻塞任务,所以这个任务必须在另一个线程中执行,所以我们避免阻塞GUI,阻止gif显示它动画,创建一个信号,允许将数据从辅助线程发送到主线程,如下所示:

    import threading
    from PyQt5 import QtCore, QtGui, QtWidgets, uic
    from FirebaseLogin import db
    from contraInicio import ContraInicioC
    
    
    class LoginC(QtWidgets.QMainWindow):
        firebaseFinished = QtCore.pyqtSignal(object)
    
        def __init__(self, parent=None):
            super(LoginC, self).__init__(parent)
            uic.loadUi("Interfaz/Login.ui",self)
    
            self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
            self.l_permiso.setAttribute(QtCore.Qt.WA_NoMousePropagation)
            self.l_sucursal.setAttribute(QtCore.Qt.WA_NoMousePropagation)
            self.l_close.clicked.connect(self.close)
            self.contraseña = ContraInicioC()
            self.l_entrar.clicked.connect(self.setDataPrincipal)
            self.l_permiso.currentIndexChanged.connect(self.cambioPermiso)
            self.l_permiso.setCurrentText("Caja")
            self.l_sucursal.setCurrentText("Exxe 1")
            self.firebaseFinished.connect(self.on_firebaseFinished)
    
        @QtCore.pyqtSlot()
        def cambioPermiso(self):
            self.l_sucursal.setEnabled(self.l_permiso.currentText() == "Administrador")
    
        def setDataPrincipal(self):
            movi = QtGui.QMovie("giphy.gif")
            self.label.setMovie(movi)
            movi.start()
            fechaActual = QtCore.QDate.currentDate().toPyDate()
            path = "/{}".format(fechaActual)
            threading.Thread(target=self.get_data, args=(path,), daemon=True).start()
    
        def get_data(self, path):
            ref = db.reference(path)
            ret = ref.get()
            self.firebaseFinished.emit(ret)
    
        def on_firebaseFinished(self, ret):
            print("result", ret)
            self.contraseña.show()
            self.close()
    
        def mousePressEvent(self,event):
            if event.button() == QtCore.Qt.LeftButton:
                self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
                event.accept()
    
        def mouseMoveEvent(self,event):
            if event.buttons() == QtCore.Qt.LeftButton:
                self.move(event.globalPos() - self.dragPosition)
                event.accept()
    
    if __name__ == '__main__':
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        l = LoginC()
        l.show()
        sys.exit(app.exec_())
    

    【讨论】:

    • 非常感谢我已经尝试解决这个问题好几个小时了。我不认为这是火力基地:)
    • @Mystic_Force 在 SOes 中我发布了一个非常相似的答案,我很好奇:几个用户要求具有相同基础的代码:firebase with PyQt
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 2019-09-14
    • 2019-09-28
    • 2020-05-02
    • 2017-05-15
    • 2018-06-10
    相关资源
    最近更新 更多