import sys from PyQt5.Qt import * class Window(QWidget): def __init__(self): super().__init__() self.setup_ui() def setup_ui(self): self.setWindowTitle("Qt桌面应用程序") self.resize(600, 300) self.label = QLabel(self) self.label.resize(self.width(), 50) self.label.setStyleSheet("background-color:cyan") self.push_button_int_input = QPushButton(self) self.push_button_int_input.setText("IntInput") self.push_button_int_input.move(50, 100) self.push_button_text_input = QPushButton(self) self.push_button_text_input.setText("TextInput") self.push_button_text_input.move(150, 100) self.push_button_item_input = QPushButton(self) self.push_button_item_input.setText("ItemInput") self.push_button_item_input.move(250, 100) self.push_button_get_double = QPushButton(self) self.push_button_get_double.setText("getDouble") self.push_button_get_double.move(50, 150) self.push_button_get_multi_line_text = QPushButton(self) self.push_button_get_multi_line_text.setText("getMLText") self.push_button_get_multi_line_text.move(150, 150) self.push_button_get_item = QPushButton(self) self.push_button_get_item.setText("getItem") self.push_button_get_item.move(250, 150) self.push_button_activities() def push_button_activities(self): def push_button_int_input_clicked(): self.dialogue_func() self.input_dialogue.show() self.input_dialogue.setInputMode(QInputDialog.IntInput) self.dialogue_int_input() self.push_button_int_input.clicked.connect(push_button_int_input_clicked) def push_button_text_input_clicked(): self.dialogue_func() self.input_dialogue.show() self.input_dialogue.setInputMode(QInputDialog.TextInput) self.dialogue_text_input() self.push_button_text_input.clicked.connect(push_button_text_input_clicked) def push_button_item_input_clicked(): self.dialogue_func() self.input_dialogue.show() self.input_dialogue.setInputMode(QInputDialog.TextInput) self.input_dialogue.setComboBoxItems(["abc", "123", "甲乙丙"]) # self.input_dialogue.setOptions(QInputDialog.UseListViewForComboBoxItems) self.dialogue_text_input() self.push_button_item_input.clicked.connect(push_button_item_input_clicked) def push_button_get_double_clicked(): self.file_result = QInputDialog.getDouble(self, "输入窗口", "请选择/输入:", decimals=2) self.label.setText(f"最终输入:{self.file_result[0]} 确定:{self.file_result[1]}") self.push_button_get_double.clicked.connect(push_button_get_double_clicked) def push_button_get_multi_line_text_clicked(): self.file_result = QInputDialog.getMultiLineText(self, "输入窗口", "请选择/输入:", "多行输入...") self.label.setText(f"最终输入:{self.file_result[0]} 确定:{self.file_result[1]}") self.push_button_get_multi_line_text.clicked.connect(push_button_get_multi_line_text_clicked) def push_button_get_item_clicked(): self.file_result = QInputDialog.getItem(self, "输入窗口", "请选择/输入:", ["abc", "123", "甲乙丙"]) self.label.setText(f"最终输入:{self.file_result[0]} 确定:{self.file_result[1]}") self.push_button_get_item.clicked.connect(push_button_get_item_clicked) def dialogue_func(self): self.input_dialogue = QInputDialog(self) self.input_dialogue.setWindowTitle("输入窗口") self.input_dialogue.setLabelText("请选择/输入:") self.input_dialogue.setWindowModality(Qt.WindowModal) def dialogue_int_input(self): self.input_dialogue.accepted.connect(lambda: self.label.setText( f"接受 结果:{self.input_dialogue.result()} 最终输入:{self.input_dialogue.intValue()}")) self.input_dialogue.rejected.connect(lambda: self.label.setText( f"拒绝 结果:{self.input_dialogue.result()} 最终输入:{self.input_dialogue.intValue()}")) def int_value_changed(value): self.label.setText(f"当前输入:{value}") self.input_dialogue.intValueChanged.connect(int_value_changed) def dialogue_text_input(self): self.input_dialogue.accepted.connect(lambda: self.label.setText( f"接受 结果:{self.input_dialogue.result()} 最终输入:{self.input_dialogue.textValue()}")) self.input_dialogue.rejected.connect(lambda: self.label.setText( f"拒绝 结果:{self.input_dialogue.result()} 最终输入:{self.input_dialogue.textValue()}")) def text_value_changed(value): self.label.setText(f"当前输入:{value}") self.input_dialogue.textValueChanged.connect(text_value_changed) if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())