本帖最后由 阿奇_o 于 2022-9-16 02:02 编辑
这样,可否?from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(100, 100, 400, 500)
# checkbox
self.cbox = QCheckBox('选项一', self)
self.cbox.move(100, 150)
self.cbox.clicked.connect(self.clicked_cbox)
self.show() # show all the widgets
def clicked_cbox(self):
checkbox = self.sender()
if checkbox.isChecked():
print(checkbox.text() + ' is checked')
self.lb = PopupLabel()
else:
print(checkbox.text() + ' is unchecked')
self.lb = PopupLabel()
class PopupLabel(QWidget):
def __init__(self, msg="设置已保存!"):
super().__init__()
# this will hide the title bar
self.setWindowFlag(Qt.FramelessWindowHint)
# self.setWindowTitle("no title")
self.setGeometry(100, 100, 400, 300)
self.label_1 = QLabel(msg, self)
# moving position (by default label will display at top left corner)
self.label_1.move(100, 100)
# setting up border and background color
self.label_1.setStyleSheet("background-color: lightgreen; border: 3px solid green")
self.timer = QTimer()
self.timer.timeout.connect(self.move_label)
self.cnt = 0 # 计时器的周期数
self.timer.start(1000) # 间隔1000ms(周期1s)
# self.setWindowOpacity(0.5) # 这是整体透明设置
# 如何只设置窗体部分的背景透明,而其他控件不透明?
self.setAttribute(Qt.WA_NoSystemBackground, True)
self.setAttribute(Qt.WA_TranslucentBackground, True)
self.show()
def move_label(self):
self.cnt += 1
self.label_1.move(self.cnt * 20 + 100, 100)
self.label_1.setText(f"。。我移动了!--> {self.cnt}")
# self.label_1.adjustSize() #
self.label_1.resize(200, 30)
if self.cnt == 3: # 移动3次后(3秒),移除标签,移除窗口(关闭)
self.label_1.deleteLater() # 移除标签
self.timer.stop()
self.deleteLater() # 移除窗口(因为是“主窗口”程序随即退出)
App = QApplication(sys.argv)
window = MainWindow()
sys.exit(App.exec())
ps: 现学的,之前没用过PyQt,查了N个网站,花费了两三个小时。。
|