【发布时间】:2017-09-24 11:20:03
【问题描述】:
我想通过 PC 上的手机发送我自己的通知,例如 notify-senf。我做弹出小部件,但我不能删除。当我删除它时,仍然有空的地方。我怎样才能做到这一点? 此代码是示例。
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())
通知
删除后放置
【问题讨论】:
-
为什么不用close()?
-
我添加了更多代码。如何关闭一条通知?
-
据我了解,您想在 2 秒后关闭应用程序并删除所有消息。我是对的?
-
没有。它的例子。通知应该一一关闭。我可以通过 self.close() 关闭所有通知。
-
试试我的答案:P
标签: python python-3.x pyqt pyqt5