How to auto hide a dialogue box in PyQt5

I have created a dialogue box using pyqt5, which I want to display for 5 seconds an wish to autohide it after the given time.

Here’s the code:-

from PyQt5.QtWidgets import QMainWindow, QApplication, QLCDNumber,QWidget, QDesktopWidget, QLabel
from PyQt5 import uic
import sys
from PyQt5.QtCore import QTime, QTimer
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import time

class UI(QMainWindow):
    def __init__(self):
        super(UI, self).__init__()
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)

        # Load the ui file
        uic.loadUi("screenshot.ui", self)
        #QtCore.QTimer.singleShot(0, self.show)

        # self.label0 = self.findChild(QLabel, "label_2")
        # self.label1 = self.findChild(QLabel, "label")
        # self.label2 = self.findChild(QLabel, "label_3")

        self.show()

        # waiting for 2 second
        #time.sleep(2)

        # closing the window
        #self.close()


    def location_on_the_screen(self):
        ag = QDesktopWidget().availableGeometry()
        sg = QDesktopWidget().screenGeometry()
        widget = self.geometry()
        x = ag.width() - widget.width()
        y = 7 * ag.height() - sg.height() - widget.height()
        self.move(x, y)
    
# Initialize The App
app = QApplication(sys.argv)
UIWindow = UI()
#self.show()
#UIWindow.close()
UIWindow.location_on_the_screen()
app.exec_()

Here’s the screenshot.ui file code. Unable to add as an attachment.

<?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>230</width>
    <height>248</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>30</x>
      <y>20</y>
      <width>151</width>
      <height>171</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
    <property name="pixmap">
     <pixmap>../../../Roasfer/New folder/Trai_bg.png</pixmap>
    </property>
   </widget>
   <widget class="QLabel" name="label_2">
    <property name="geometry">
     <rect>
      <x>40</x>
      <y>70</y>
      <width>131</width>
      <height>101</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
   </widget>
   <widget class="QLabel" name="label_3">
    <property name="geometry">
     <rect>
      <x>130</x>
      <y>140</y>
      <width>41</width>
      <height>41</height>
     </rect>
    </property>
    <property name="text">
     <string/>
    </property>
    <property name="pixmap">
     <pixmap>../../../Roasfer/New folder/image18.png</pixmap>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>230</width>
     <height>20</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

Here are the images use in the above coe:-
Trai_bg.png
Image 18

I tried to run it using a for loop but it’s not working though it’s getting stuck. I am new to PyQt5, is there any tool that could resolve this or any way, by which this can be achieved.

>Solution :

Use QTimer:

class UI(QMainWindow):
    def __init__(self):
        super(UI, self).__init__()
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        uic.loadUi("screenshot.ui", self)
        self.show()
        QtCore.QTimer.singleShot(3000, self.close)

Leave a Reply