我希望自己的通知來自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())
通知
發生之後刪除
你爲什麼不使用接近(? – eyllanesc
我添加了更多的代碼。我如何關閉一個通知? –
據我所知,你想在2秒後關閉應用程序,所有的消息應該被刪除。我是對的? – eyllanesc