PyQt5 QInputDialog对话框
在计算机应用程序中,对话框是用户与应用程序交互的窗口。对话框用于输入数据,修改数据,修改应用程序设置等。
import sys from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, QPushButton, QLabel, QInputDialog class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.resize(250, 155) self.setWindowTitle('title') self.name = QLabel('Name', self) self.sex = QLabel('Sex', self) self.age = QLabel('Age', self) self.cname = QLabel() self.csex = QLabel() self.cage = QLabel() self.button = QPushButton('Update', self) self.button.clicked.connect(self.update) self.gird= QGridLayout() self.gird.addWidget(self.name, 0, 0) self.gird.addWidget(self.cname, 0, 1) self.gird.addWidget(self.sex, 1, 0) self.gird.addWidget(self.csex, 1, 1) self.gird.addWidget(self.age, 2, 0) self.gird.addWidget(self.cage, 2, 1) self.gird.addWidget(self.button) self.setLayout(self.gird) self.show() def update(self): sexx = ['男','女'] name, n = QInputDialog.getText(self, 'Update name', 'New Name') if n: self.cname.setText(name) sex, s = QInputDialog.getItem(self, 'Update sex', 'New Sex', sexx) if s: self.csex.setText(sex) age, a = QInputDialog.getInt(self, 'Update age', 'New Age') if a: self.cage.setText(str(age)) if __name__ == "__main__": app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())