from PyQt5.Qt import *
class MyWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('230_QComboBox_案例,全国省市级联表')
self.resize(500,500)
self.iniUI()
def iniUI(self):
cb1 = QComboBox(self)
cb2 = QComboBox(self)
self.cb1 = cb1
self.cb2 = cb2
cb1.resize(self.width() * 4 / 12, self.height() / 10)
cb2.resize(self.width() * 4 / 12, self.height() / 10)
cb1.move((self.width() - cb1.width()*2) / 2, (self.height() - cb1.height()) *13/ 32)
cb2.move(self.width()-((self.width() - cb2.width() * 2) / 2)-cb2.width(), (self.height() - cb2.height()) * 13 / 32)
label = QLabel(self)
self.label = label
label.setText('')
label.setAlignment(Qt.AlignCenter)
label.setStyleSheet('font-size:35px;background-color:cyan;')
label.resize(self.cb1.width(),self.cb1.height()*2)
label.move((self.width()-label.width())/2,self.cb1.y()+self.cb1.height()*2)
def comboBoxOperation(self):
AnHui = {'合肥':'霸都','芜湖':'米市','安庆':'宜城','蚌埠':'铁路','淮南':'煤矿'}
JiangSu = {'南京':'金陵','苏州':'姑苏','泰州':'汉唐','扬州':'广陵','徐州':'陶谦'}
ZheJiang ={'杭州':'临安','绍兴':'会稽','义乌':'批发','奉化':'老蒋','温州':'永嘉'}
self.dictionary = {'安徽':AnHui,'江苏':JiangSu,'浙江':ZheJiang}
self.cb1.currentIndexChanged[str].connect(self.cb1_changed)
self.cb2.currentIndexChanged.connect(self.cb2_changed)
self.cb1.addItems(self.dictionary.keys())#首先创建好信号连接,然后再去操作数据
def cb1_changed(self,province):
cities = self.dictionary[province]
self.cb2.blockSignals(True)
self.cb2.clear()
self.cb2.blockSignals(False)
for key,value in cities.items():
self.cb2.addItem(key,value)
def cb2_changed(self,index):
nickname = self.cb2.itemData(index)
print(nickname)
self.label.setText(nickname)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win = MyWindow()
win.comboBoxOperation()
win.show()
sys.exit(app.exec_())
PyQt5_QComboBox_实现一个江苏 浙江 安徽 三省市的级联表
最新推荐文章于 2024-04-17 20:26:14 发布