"""
1, 控件内创建若干标签
2,标签点击变色
"""
from PyQt5.Qt import *
import sys
# class Label(QLabel):
# def mousePressEvent(self, QMouseEvent):
# self.setStyleSheet('background-color:red;')
class Window(QWidget):
def mousePressEvent(self, evt):
# QMouseEvent
local_x = evt.x()
local_y = evt.y()
sub_widget = self.childAt(local_x, local_y)
if sub_widget is not None:
sub_widget.setStyleSheet('background-color:red;')
print("被点击了", local_x, local_y)
app = QApplication(sys.argv)
# 创建控件
# window = QWidget()
window = Window()
# 设置控件
window.setWindowTitle('QWidget父子关系')
# 设置大小
window.resize(500, 500)
for i in range(1, 11):
label = QLabel(window)
label.setText('标签' + str(i))
label.move(40 * i, 40 * i)
# 展示控件
window.show()
# 退出
sys.exit(app.exec_())
网友评论