1 from PyQt5.QtWidgets import * 2 import sys 3 4 class QlabelDemo(QDialog): 5 def __init__(self): 6 super().__init__() 7 8 self.setWindowTitle('QLabel 例子') 9 #快捷键 Alt+首字母 10 nameLb1 = QLabel('&Name', self) 11 nameEd1 = QLineEdit(self) 12 nameLb1.setBuddy(nameEd1) 13 14 nameLb2 = QLabel('&Password', self) 15 nameEd2 = QLineEdit(self) 16 nameLb2.setBuddy(nameEd2) 17 18 btnOK = QPushButton('&OK') 19 btnCancel = QPushButton('&Cancel') 20 mainLayout = QGridLayout(self) 21 mainLayout.addWidget(nameLb1, 0, 0) 22 mainLayout.addWidget(nameEd1, 0, 1, 1, 2) 23 24 mainLayout.addWidget(nameLb2, 1, 0) 25 mainLayout.addWidget(nameEd2, 1, 1, 1, 2) 26 27 mainLayout.addWidget(btnOK, 2, 1) 28 mainLayout.addWidget(btnCancel, 2, 2) 29 30 if __name__ == '__main__': 31 app = QApplication(sys.argv) 32 labelDemo = QlabelDemo() 33 labelDemo.show() 34 sys.exit(app.exec_())