代码
'''
4个按钮
'''
import sys
import random
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget
from PyQt5.QtGui import QIcon
btns = []
def when_btn_onclick():
print("按钮(点我试试) 被点击了")
btn = random.choice(btns)
position = (random.randint(1, 500), random.randint(1, 400))
btn.move(*position)
print("某个按钮移动到了 {}".format(position))
print("x: {}, y: {}, width: {}, height: {}".format(widget.x(), widget.y(), widget.width(), widget.height()))
def when_btn2_onclick():
print("按钮(退出) 被点击了")
app = QApplication.instance()
app.quit()
def when_btn3_onclick():
print("按钮(显示窗口信息) 被点击了")
print("窗口信息:", end="")
print("x: {}, y: {}, width: {}, height: {}".format(widget.x(), widget.y(), widget.width(), widget.height()))
def when_btn4_onclick():
print("按钮(归位) 被点击了")
btn.move(100, 200)
btn2.move(300, 200)
btn3.move(200, 300)
btn4.move(200, 100)
app = QApplication(sys.argv)
widget = QWidget()
icon_path = "./ico/Porsche.ico"
widget.setWindowIcon(QIcon(icon_path))
btn = QPushButton(widget)
btns.append(btn)
btn.setText("点我试试")
btn.clicked.connect(when_btn_onclick)
btn.move(100, 200)
btn2 = QPushButton(widget)
btns.append(btn2)
btn2.setText("退出")
btn2.clicked.connect(when_btn2_onclick)
btn2.move(300, 200)
btn3 = QPushButton(widget)
btns.append(btn3)
btn3.setText("显示窗口信息")
btn3.clicked.connect(when_btn3_onclick)
btn3.move(200, 300)
btn4 = QPushButton(widget)
btns.append(btn4)
btn4.setText("归位")
btn4.clicked.connect(when_btn4_onclick)
btn4.move(200, 100)
widget.resize(500, 400)
widget.move(200, 100)
widget.show()
sys.exit(app.exec_())