import sys from PyQt5.Qt import * class Window(QWidget): def __init__(self): super().__init__() self.city_dic = { "北京": { "东城": "001", "西城": "002", "朝阳": "003", "丰台": "004" }, "上海": { "黄埔": "005", "徐汇": "006", "长宁": "007", "静安": "008", "松江": "009" }, "广东": { "广州": "010", "深圳": "011", "湛江": "012", "佛山": "013" } } self.setup_ui() def setup_ui(self): self.setWindowTitle("Qt桌面应用程序") self.resize(300, 300) self.label = QLabel(self) self.label.move(50, 20) self.combo_box_1 = QComboBox(self) self.combo_box_1.resize(100, 30) self.combo_box_1.move(0, 50) self.combo_box_2 = QComboBox(self) self.combo_box_2.resize(100, 30) self.combo_box_2.move(100, 50) self.font_combo_box = QFontComboBox(self) self.font_combo_box.resize(100, 30) self.font_combo_box.move(200, 50) def current_font_changed(font): for i in self.findChildren(QComboBox): i.setFont(font) self.font_combo_box.currentFontChanged.connect(current_font_changed) def current_index_changed_2(index): self.label.setText(self.combo_box_2.itemData(index)) self.label.adjustSize() self.combo_box_2.currentIndexChanged.connect(current_index_changed_2) def current_index_changed_1(index): self.combo_box_2.blockSignals(True) self.combo_box_2.clear() self.combo_box_2.blockSignals(False) for x, y in self.city_dic[self.combo_box_1.itemText(index)].items(): self.combo_box_2.addItem(x, y) self.combo_box_1.currentIndexChanged.connect(current_index_changed_1) # for i in self.city_dic.keys(): # self.combo_box_1.addItem(i) self.combo_box_1.addItems(self.city_dic.keys()) if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())