用python写一个能查询各地天气的程序,界面用的是pyqt库。
1 from PyQt5.Qt import * 2 import sys 3 import json 4 from requests_html import HTMLSession 5 filename=‘城市代码.json‘ 6 with open(filename) as f: 7 zidian=json.load(f) 8 print(zidian) 9 app=QApplication(sys.argv) 10 window=QWidget() 11 window.resize(800,600) 12 window.setWindowTitle(‘天气预报‘) 13 chengshi=QLabel(window) 14 chengshi.move(100,40) 15 chengshi.setText(‘城市:‘) 16 chengshi.setStyleSheet(‘font-size:40px‘) 17 shuru=QLineEdit(‘北京‘,window) 18 shuru.move(220,40) 19 shuru.resize(200,45) 20 shuru.setStyleSheet(‘font-size:40px‘) 21 btn=QPushButton(window) 22 btn.move(440,38) 23 btn.setText(‘查询‘) 24 btn.setStyleSheet(‘font-size:35px‘) 25 xianshi=QLabel(window) 26 xianshi.setStyleSheet(‘font-size:35px;background-color:cyan‘) 27 xianshi.setText(‘‘) 28 xianshi.move(50,150) 29 xianshi.resize(700,80) 30 xianshi2=QLabel(window) 31 xianshi2.setStyleSheet(‘font-size:35px;background-color:pink‘) 32 xianshi2.setText(‘‘) 33 xianshi2.move(50,250) 34 xianshi2.resize(700,80) 35 xianshi3=QLabel(window) 36 xianshi3.setStyleSheet(‘font-size:35px;background-color:silver‘) 37 xianshi3.setText(‘‘) 38 xianshi3.move(50,350) 39 xianshi3.resize(700,80) 40 xianshi4=QLabel(window) 41 xianshi4.setStyleSheet(‘font-size:35px;background-color:orange‘) 42 xianshi4.setText(‘‘) 43 xianshi4.move(50,450) 44 xianshi4.resize(700,80) 45 def cao(): 46 a=shuru.text() 47 b=zidian[a] 48 url=‘http://www.weather.com.cn/weather/‘+b+‘.shtml‘ 49 session=HTMLSession() 50 r=session.get(url) 51 list1=r.html.find(‘ul.t.clearfix‘) 52 list2=list1[0].text 53 list3=list2.split(‘\n‘) 54 tq=[0] 55 tqq=[0] 56 for i in range(0, 7): 57 for j in range(0, 4): 58 k = i * 4 59 if j == 0: 60 tq = list3[k + j] 61 else: 62 tq = tq + list3[k + j] 63 tqq.append(tq) 64 print(tqq) 65 xianshi.setText(tqq[1]) 66 xianshi2.setText(tqq[2]) 67 xianshi3.setText(tqq[3]) 68 xianshi4.setText(tqq[4]) 69 btn.clicked.connect(cao) 70 window.show() 71 sys.exit(app.exec_())