1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| from PyQt5 import QtWidgets from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox import sys
class MyWindow(QMainWindow): def __init__(self): super(MyWindow, self).__init__() self.setGeometry(200, 250, 500, 250) self.setWindowTitle("一个窗口") self.initUI()
def initUI(self): self.label = QtWidgets.QLabel(self) self.label.setText("这是一个Label!") self.label.move(50, 50) self.b1 = QtWidgets.QPushButton(self) self.b1.setText("按钮1") self.b1.move(100, 100) self.b1.clicked.connect(self.clicked) self.b2 = QtWidgets.QPushButton(self) self.b2.setText("按钮2") self.b2.move(250, 100) self.b2.clicked.connect(self.showMsgBox)
def clicked(self): self.label.setText("点击按钮后我被改变了") self.update()
def update(self): self.label.adjustSize()
def showMsgBox(self): msg = QMessageBox() msg.setWindowTitle("弹窗提示") msg.setText("这是一个消息弹窗") msg.setIcon(QMessageBox.Information) x = msg.exec_()
def window(): app = QApplication(sys.argv) win = MyWindow() win.show() sys.exit(app.exec_())
window()
|