使用time.sleep或计时器PyQt5时无法获取桌面通知

时间:2018-06-26 13:54:29

标签: multithreading notifications pyqt5

是Pyqt5的新手, 我正在实施管理用户提交的任务的应用程序,我希望我的应用程序将提交的任务从数据库自动加载到Qtablewidget中,如果有新任务,该程序必须使用从互联网获得的以下代码来显示桌面通知

from PyQt5 import QtCore, QtGui, QtWidgets
class PopupWindowClass(QtWidgets.QWidget):
    popuphidden = QtCore.pyqtSignal()

def __init__(self, mssg):
    super(PopupWindowClass, self).__init__()
    self.setWindowFlags(QtCore.Qt.SplashScreen | QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)

    self.setMinimumSize(QtCore.QSize(300, 100))
    self.animation = QtCore.QPropertyAnimation(self, b"windowOpacity", self)
    self.animation.finished.connect(self.hide)
    self.timer = QtCore.QTimer()
    #self.timer.timeout.connect(self.hideAnimation)
    global MSSG
    MSSG = mssg
    self.setupUi()
    self.setPopupText(MSSG)

def setupUi(self):
    self.verticalLayout = QtWidgets.QVBoxLayout(self)
    self.label = QtWidgets.QLabel(self)
    font = QtGui.QFont()
    font.setPointSize(14)
    self.label.setFont(font)
    self.label.setAlignment(QtCore.Qt.AlignCenter)
    self.verticalLayout.addWidget(self.label)
    appearance = self.palette()
    appearance.setColor(QtGui.QPalette.Normal, QtGui.QPalette.Window,
                 QtGui.QColor("green"))
    self.setPalette(appearance)

def setPopupText(self, text):
    self.label.setText(text)
    self.label.adjustSize()

def mousePressEvent(self, event):
    if event.button() == QtCore.Qt.LeftButton:
        self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()            
        event.accept()
        QtWidgets.QWidget.hide(self)
        self.popuphidden.emit()
        self.timer.timeout.connect(self.hideAnimation)


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

def show(self):
    self.setWindowOpacity(0.0)
    self.animation.setDuration(1500)
    self.animation.setStartValue(0.0)
    self.animation.setEndValue(1.0)
    QtWidgets.QWidget.show(self)
    self.animation.start()
    self.timer.start(5000)

def hideAnimation(self):
    self.timer.stop()
    self.animation.setDuration(1500)
    self.animation.setStartValue(1.0)
    self.animation.setEndValue(0.0)
    self.animation.start()

def hide(self):
    if self.windowOpacity() == 0:
        QtWidgets.QWidget.hide(self)
        self.popuphidden.emit()

这是我的一些主要代码:

class MainApp(QMainWindow):
"""docstring for main"""
  def __init__(self, parent= None):
      super(MainApp, self).__init__(parent)
      uic.loadUi('Tasks.ui', self)
      self.handel_ui()

  def handel_ui(self):
      self.setWindowTitle('Tasks')
      self.btn_load.clicked.connect(self.load)

    def load(self):
    connection = pypyodbc.connect('Driver={SQL Server};'
                                  'Server=server;'
                                  'Database=TasksDB;'
                                  'uid=sa;pwd=pwd)
    cursor = connection.cursor()
    query = ("SELECT [To] , [Desc] , Priority , Status , ID , time FROM Tasks WHERE [To] = ? ORDER BY time DESC")
    values = ['Kariem    ']
    cursor.execute(query, values)
    results = cursor.fetchall()
    self.tableWidget.setRowCount(0)
    while results:
        for row_number , row_data in enumerate(results):
            self.tableWidget.insertRow(row_number)
            for column_number, data in enumerate(row_data):
                self.tableWidget.setItem(row_number, column_number,  QTableWidgetItem(str(data)))

        results = cursor.fetchone()
        connection.commit()
        connection.close()
    self.popup()

    def popup(self):
      count = self.tableWidget.rowCount()
      item1 = self.tableWidget.item(0, 0)
      i_d1 = item1.text()
      db_no = self.get_no_tasks("SELECT nooftasks FROM count WHERE [user] = ?", [i_d1])
     print('db no = ', db_no, 'rowcount = ', count)   
     if db_no < count:
          self. desktopNotify ('You Have New Task')
          print('message should appear')
    self.update_db("UPDATE count SET nooftasks = ? WHERE [user] = ?", [count, i_d1])
   time.sleep(5)
   self.load()

def desktopNotify(self, msg):
    def move2RightBottomCorner(win):
        try:
            screen_geometry = QApplication.desktop().availableGeometry()
            screen_size = (screen_geometry.width(), screen_geometry.height())
            win_size = (win.frameSize().width(), win.frameSize().height())
            x = screen_size[0] - win_size[0] - 10
            y = screen_size[1] - win_size[1] - 10
            win.move(x, y)
        except Exception as e:
            print(e)

    main_window = PopupWindowClass(msg)
    main_window.show()
    move2RightBottomCorner(main_window)

问题是应用程序继续打印db no =',db_no,'rowcount =',计数并挂起

请帮忙 我想让我的应用程序等待一段时间,然后自动执行加载功能并在条件为真时显示通知

预先感谢

0 个答案:

没有答案