2017-09-24 103 views
0

我希望自己的通知來自PC上的手機,例如notify-senf。我彈出小部件,但是我不能刪除。當我刪除它時,仍然有空的地方。我怎樣才能做到這一點? 這段代碼就是例子。來自QWidget的PyQt5通知

class Notification(QWidget): 
    signNotifyClose = QtCore.pyqtSignal(str) 

    def __init__(self, parent = None): 
     time = datetime.now() 
     currentTime = str(time.hour) + ":" + str(time.minute) + "_" 
     self.LOG_TAG = currentTime + self.__class__.__name__ + ": " 
     super(QWidget, self).__init__(parent) 

     self.setWindowFlags(QtCore.Qt.FramelessWindowHint) #убирает заголовок, поверх всех окон (| QtCore.Qt.WindowStaysOnTopHint) 
     resolution = QDesktopWidget().screenGeometry(-1) 
     screenWidth = resolution.width() 
     screenHeight = resolution.height() 
     print(self.LOG_TAG + "width: " + str(resolution.width()) + " height: " + str(resolution.height())) 
     self.count = 0 # Счетчик уведомлений 
     self.timer = 3 

     self.vboxMainLayout = QVBoxLayout() # layout contain notifications 
     self.move(screenWidth, 0) 
     self.setLayout(self.vboxMainLayout) 

    def setNotify(self, title, notify): 
     count = self.count 
     title = QLabel() 
     title.setStyleSheet("border: 1px solid #000") 
     title.setText(title) 
     title.setStyleSheet("font-family: 'Roboto', sans-serif; font-size: 14px; font-weight: bold; padding: 0;") 

     text = QLabel() 
     text.setText(notify) 
     text.setStyleSheet("font-family: 'Roboto', sans-serif; font-size: 12px; font-weight: normal; padding: 0;") 

     gridNotify = QGridLayout() 
     gridNotify.addWidget(title, 0, 0) 
     gridNotify.addWidget(text, 1, 0) 

     buttonClose = QPushButton() 
     buttonClose.clicked.connect(self.deleteWidgets) 
     buttonClose.setIcon(QIcon("res/close1.png")) 
     buttonClose.setFlat(False) 
     buttonClose.setMaximumWidth(14) 
     buttonClose.setMaximumHeight(14) 

     gridClose = QGridLayout() 
     gridClose.addWidget(buttonClose, 0, 0) 

     gridLayoutMain = QGridLayout() 
     gridLayoutMain.setColumnStretch(0,1) 
     gridLayoutMain.setColumnStretch(0,2) 
     gridLayoutMain.setColumnStretch(0,3) 
     gridLayoutMain.addLayout(gridClose, 0, 4) 
     gridLayoutMain.addLayout(gridNotify, 0, 0) 

     self.count += 1 

     self.vboxMainLayout.addLayout(gridLayoutMain) 
     self.show() 
     threading.Timer(2, self.delete, args=(gridLayoutMain,)).start() 

    def delete(self, layout): 
     for i in reversed(range(layout.count())): 
      item = layout.takeAt(i) 
      widget = item.widget() 
      if widget is not None: 
       # widget.deleteLater() 
      elif item.layout() is not None: 
       print("") 
       self.delete(item.layout()) 

通知

發生之後刪除

+0

你爲什麼不使用接近(? – eyllanesc

+0

我添加了更多的代碼。我如何關閉一個通知? –

+0

據我所知,你想在2秒後關閉應用程序,所有的消息應該被刪除。我是對的? – eyllanesc

回答

0

爲了簡化,最好是建立一個Widget的消息的任務,因爲我在下面:

class Message(QWidget): 
    def __init__(self, title, message, parent=None): 
     QWidget.__init__(self, parent) 
     self.setLayout(QGridLayout()) 
     self.titleLabel = QLabel(title, self) 
     self.titleLabel.setStyleSheet(
      "font-family: 'Roboto', sans-serif; font-size: 14px; font-weight: bold; padding: 0;") 
     self.messageLabel = QLabel(message, self) 
     self.messageLabel.setStyleSheet(
      "font-family: 'Roboto', sans-serif; font-size: 12px; font-weight: normal; padding: 0;") 
     self.buttonClose = QPushButton(self) 
     self.buttonClose.setIcon(QIcon("res/close1.png")) 
     self.buttonClose.setFixedSize(14, 14) 
     self.layout().addWidget(self.titleLabel, 0, 0) 
     self.layout().addWidget(self.messageLabel, 1, 0) 
     self.layout().addWidget(self.buttonClose, 0, 1, 2, 1) 

如果您想先刪除郵件,則必須使用佈局的removeWidget()功能將其從佈局中刪除,然後您必須使用deleteLater()刪除自己及其子窗口小部件。當我們發送一個信號時,我們可以得到通過sender()發射它的對象,在我們的例子中它將是按鈕,並且通過這個我們獲得作爲我們的消息控件的父對象。然後更新我們使用adjustSize()的佈局的正確大小。即使刪除整個小部件也會獲得最小值,所以如果沒有消息,最好關閉它。

class Notification(QWidget): 
    signNotifyClose = pyqtSignal(str) 
    def __init__(self, parent = None): 
     time = datetime.now() 
     currentTime = str(time.hour) + ":" + str(time.minute) + "_" 
     self.LOG_TAG = currentTime + self.__class__.__name__ + ": " 
     super(QWidget, self).__init__(parent) 

     self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint) 
     resolution = QDesktopWidget().screenGeometry(-1) 
     screenWidth = resolution.width() 
     screenHeight = resolution.height() 
     print(self.LOG_TAG + "width: " + str(resolution.width()) + " height: " + str(resolution.height())) 
     self.nMessages = 0 
     self.mainLayout = QVBoxLayout(self) 
     self.move(screenWidth, 0) 

    def setNotify(self, title, message): 
     m = Message(title, message, self) 
     self.mainLayout.addWidget(m) 
     m.buttonClose.clicked.connect(self.onClicked) 
     self.nMessages += 1 
     self.show() 

    def onClicked(self): 
     self.mainLayout.removeWidget(self.sender().parent()) 
     self.sender().parent().deleteLater() 
     self.nMessages -= 1 
     self.adjustSize() 
     if self.nMessages == 0: 
      self.close() 

下面是如何用下面的代碼使用它的一個例子:)

class Example(QWidget): 
    counter = 0 

    def __init__(self, parent=None): 
     QWidget.__init__(self, parent) 
     self.setLayout(QVBoxLayout()) 
     btn = QPushButton("Send Notify", self) 
     self.layout().addWidget(btn) 

     self.notification = Notification() 
     btn.clicked.connect(self.notify) 


    def notify(self): 
     self.counter += 1 
     self.notification.setNotify("Title{}".format(self.counter), 
            "message{}".format(self.counter)) 


if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    w = Example() 
    w.show() 
    sys.exit(app.exec_()) 
+0

謝謝!告訴我,如何使它們在不活動的主窗口中可見? –

+0

你想如何生成消息? – eyllanesc

+0

從QMainWindow的 self.notify =通知() self.notify.setNotify(標題,消息) 和接收該代碼,你給我寫信 –