import sys
from PyQt5.Qt import *
# class Window(QWidget):
# def __init__(self):
# super().__init__()
# self.setWindowTitle("")
# self.resize(500,500)
# self.setup_ui()
# #添加副控件
# def setup_ui(self):
#
# if __name__ == '__main__':
app = QApplication(sys.argv)
window = QWidget()
window.move(100,100)
window.resize(500,500)
widget_count = 20
column_count =3
#九功格算法
widget_width = window.width()/column_count
row_count = (widget_count-1)//column_count+1
widget_height = window.height()/row_count
for i in range(0,widget_count):
w = QWidget(window)
w.resize(widget_width,widget_height)
widget_x = i % column_count * widget_width
widget_y = i//column_count*widget_height
w.move(widget_x,widget_y)
w.setStyleSheet("background-color:red;border:1px solid yellow ")
w.show()
#固定大小窗口
# window.setFixedSize(500,500)
# window.setGeometry(100,100,150,150)
def changeCao():
new_content = label.text()+"社会"
label.setText(new_content)
# label.resize(label.width()+100,label.height())
#自适应宽度
label.adjustSize()
label = QLabel(window)
label.setText("标签")
label.move(100,100)
label.setStyleSheet("background-color:cyan;")
btn = QPushButton(window)
btn.setText("增加内容")
btn.move(100,300)
btn.clicked.connect(changeCao)
#子控件变成了,方块
red = QWidget(window)
red.resize(100,100)
red.setStyleSheet("background-color:red;")
red.move(300,0)
gerrn = QWidget(window)
#大小
gerrn.resize(100,100)
#颜色
gerrn.setStyleSheet("background-color:gerrn;")
#位置
gerrn.move(200,0)
window.show()
sys.exit(app.exec_())
pyqt5:九宫格算法
于 2023-09-12 15:50:55 首次发布