Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading