# -*- coding: utf-8 -*-
# python:2.x
__author__ = ‘Administrator‘
"""
这是一个简单的查询工具,是由pyqt+python+qt设计器制作而已的小工具
版本:
‘‘‘
python2.7.8
pyqt4.10
‘‘‘
操作系统;‘windows7 64bit‘,中文
作者:空白
博客地址:http://www.cnblogs.com/mhxy13867806343/ and https://github.com/mhxy13867806343(未有内容)
目的:巩固python基础学习
其他:暂无
"""
#导入指定的内置模块
import sys,urllib,urllib2,json,urlparse,\
datetime,time,os,re
from bs4 import *
#导入指定的第三方模块
from PyQt4.Qt import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from dateutil.rrule import *
from dateutil.parser import parse
#导入指定自定义模块
from urls import Ui_Form
import apphelp
#QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))
#类名
class Inquiry(QWidget,Ui_Form):
def __init__(self,parent=None):
super(Inquiry,self).__init__(parent)
try:
self.setupUi(self)
self.setWindowTitle(u‘查询工具v0.1‘)
self.setFixedSize(400,500)
self.ClickedSelect.setEnabled(False)
timers=QTimer(self)
timers.timeout.connect(self.bs02)
timers.start(1)
except IOError as s:
assert s,u‘文件不存在‘
self.OnMOuser()
self.UrlS.textChanged.connect(self.__)
self.ClickedSelect.clicked.connect(self.onSelect)
self.bs01()
self.RexExps()
self.tabs()
self.tablsdata.setRowCount(3)
self.tablsdata.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.UrlS.setText(‘http://pv.sohu.com/cityjson‘)
self.tablsdata.setVerticalHeaderLabels([u‘当前年份‘u‘温度范围‘,u‘风向情况‘,u‘当天时间段‘])
def __(self,txt):
self.ClickedSelect.setEnabled(txt.isEmpty()==False)
def OnMOuser(self):
now = datetime.datetime.now()
now1= now.strftime(‘%Y-%m-%d %H:%M:%S‘)
self.ClickedSelect.setEnabled(True)
self.timedatas.setToolTip(u‘最近更新:‘+now1)
def bs01(self):
path1=urllib2.urlopen(‘http://www.ip.cn/‘).read()
soup=BeautifulSoup(path1)
find_div=soup.find(‘div‘,{‘class‘:‘well‘})
self.addres.setText(find_div.contents[0].text[23:-2])
self.addres.setToolTip(find_div.contents[0].text[2:])
def bs02(self):
now = datetime.datetime.now()
t=now.strftime(‘%Y-%m-%d‘)
self.timedatas.setText(t)
#查询内容/url是否存在
def onSelect(self,text1):
text=self.UrlS.text()
urla=‘^(https?://)?([\da-z\.-]+)\.([a-z\.]{2,6})([/\w \.-]*)*/?$‘
urlb=‘[a-zA-z]+://[^s]*‘
pattern=re.compile(urla)
match = pattern.search(text)
if text.isEmpty():#self.UrlS.text()是否为空
QMessageBox.information(self,u‘提示‘,u‘error:不能为空‘)
else:
if match:#是否url地址类型相同
print u‘合法的天气网址‘,text
self.tableShow()
self.localt()
else:QMessageBox.information(self,u‘提示‘,u‘网址不合法:{0}‘.format(text))
#正则表达式控制self.UrlS.text()类格式
def RexExps(self):
url=‘^(https?://)?([\da-z\.-]+)\.([a-z\.]{2,6})([/\w \.-]*)*/?$‘
url=QRegExp(url)
self.UrlS.setValidator(QRegExpValidator(url,self))
def tabs(self):
stringlist=QStringList()
times=datetime.date.today()
now = datetime.datetime.now()
now1= now.strftime(‘%w‘)
stringlist.append(unicode(times+datetime.timedelta(days=0)))
# stringlist.append(unicode(times+datetime.timedelta(days=1)))
# stringlist.append(unicode(times+datetime.timedelta(days=2)))
# stringlist.append(unicode(times+datetime.timedelta(days=3)))
# stringlist.append(unicode(times+datetime.timedelta(days=4)))
# stringlist.append(unicode(times+datetime.timedelta(days=5)))
# stringlist.append(unicode(times+datetime.timedelta(days=6)))
self.tablsdata.setHorizontalHeaderLabels(stringlist)
self.tablsdata.setToolTip(u‘今天是星期:{0}‘.format(now1))
def tableShow(self):#用于显示来自URL地址的信息
now = datetime.datetime.now()
now0=now.strftime(‘%m‘)
now1= now.strftime(‘%d‘)
# tab1=QTableWidgetItem(u‘今天是{0}月{1}日‘.format(now0,now1))
# self.tablsdata.setItem(0,0,tab1)
# tab2=QTableWidgetItem(u‘今天是{0}月{1}日‘.format(now0,now1))
# self.tablsdata.setItem(0,1,tab2)
#----------
times=datetime.date.today()
now = datetime.datetime.now()
Custom0=times+datetime.timedelta(days=0)
Custom1=times+datetime.timedelta(days=6)
Custom2=(rrule(DAILY,dtstart=parse(str(Custom0)),until=parse(str(Custom1))))
tab1=QTableWidgetItem(unicode(list(Custom2)[0]).split(‘00:00:00‘)[0])
self.tablsdata.setItem(0,0,tab1)
# tab2=QTableWidgetItem(unicode(list(Custom2)[1]).split(‘00:00:00‘)[0])
# self.tablsdata.setItem(0,1,tab2)
# tab3=QTableWidgetItem(unicode(list(Custom2)[2]).split(‘00:00:00‘)[0])
# self.tablsdata.setItem(0,2,tab3)
# tab4=QTableWidgetItem(unicode(list(Custom2)[3]).split(‘00:00:00‘)[0])
# self.tablsdata.setItem(0,3,tab4)
# tab5=QTableWidgetItem(unicode(list(Custom2)[4]).split(‘00:00:00‘)[0])
# self.tablsdata.setItem(0,4,tab5)
# tab6=QTableWidgetItem(unicode(list(Custom2)[5]).split(‘00:00:00‘)[0])
# self.tablsdata.setItem(0,5,tab6)
# tab7=QTableWidgetItem(unicode(list(Custom2)[6]).split(‘00:00:00‘)[0])
# self.tablsdata.setItem(0,6,tab7)
def localt(self):
ctit=urllib2.urlopen(unicode(self.UrlS.text())).read()
ctit2=ctit.split(‘=‘)[1].split(‘,‘)[2].split(‘"‘)[3]
ctitName=apphelp.convertName(ctit2)
ctitID=apphelp.getCityCodeFromName(ctitName)
msg=apphelp.getCityWeather_RealTime(ctitID)
msg1=QTableWidgetItem(unicode(msg[‘temp‘])+u‘度‘)
self.tablsdata.setItem(0,0,msg1)
msg2=QTableWidgetItem(unicode(msg[‘wind‘]))
self.tablsdata.setItem(1,0,msg2)
msg3=QTableWidgetItem(unicode(msg[‘time‘]))
self.tablsdata.setItem(2,0,msg3)
def main():
app=QApplication([])
Inquirys=Inquiry()
Inquirys.show()
sys.exit(app.exec_())
main()
如图:
----------------------------
源文件代码如下:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file ‘F:\gui\urls.ui‘
#
# Created: Sat Sep 13 18:05:39 2014
# by: PyQt4 UI code generator 4.10.4
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(587, 498)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(_fromUtf8("i/a.jpg")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Form.setWindowIcon(icon)
Form.setStyleSheet(_fromUtf8(""))
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(20, 70, 51, 16))
self.label.setObjectName(_fromUtf8("label"))
self.label_2 = QtGui.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(200, 10, 121, 16))
self.label_2.setObjectName(_fromUtf8("label_2"))
self.UrlS = QtGui.QLineEdit(Form)
self.UrlS.setGeometry(QtCore.QRect(70, 70, 250, 20))
self.UrlS.setObjectName(_fromUtf8("UrlS"))
self.ClickedSelect = QtGui.QPushButton(Form)
self.ClickedSelect.setGeometry(QtCore.QRect(250, 100, 51, 23))
self.ClickedSelect.setObjectName(_fromUtf8("ClickedSelect"))
self.label_3 = QtGui.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(20, 158, 61, 16))
self.label_3.setObjectName(_fromUtf8("label_3"))
self.addres = QtGui.QLineEdit(Form)
self.addres.setGeometry(QtCore.QRect(80, 158, 141, 20))
self.addres.setReadOnly(True)
self.addres.setObjectName(_fromUtf8("addres"))
# self.addres2 = QtGui.QLineEdit(Form)
# self.addres2.setGeometry(QtCore.QRect(360, 158, 201, 20))
# self.addres2.setObjectName(_fromUtf8("addres2"))
self.label_4 = QtGui.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(280, 158, 81, 16))
self.label_4.setObjectName(_fromUtf8("label_4"))
# self.selects = QtGui.QToolButton(Form)
# self.selects.setGeometry(QtCore.QRect(470, 110, 81, 31))
# self.selects.setStyleSheet(_fromUtf8("QToolButton{\n"
# " border:2px solid gray;\n"
# " border-radius: 10px;\n"
# "}\n"
# "QToolButton:hover{\n"
# " color:white;\n"
# " background:white;\n"
# "}"))
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap(_fromUtf8("i/b.jpg")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
# self.selects.setIcon(icon1)
# self.selects.setIconSize(QtCore.QSize(100, 100))
# self.selects.setObjectName(_fromUtf8("selects"))
self.label_5 = QtGui.QLabel(Form)
self.label_5.setGeometry(QtCore.QRect(20, 120, 81, 16))
self.label_5.setObjectName(_fromUtf8("label_5"))
self.tablsdata = QtGui.QTableWidget(Form)
self.tablsdata.setGeometry(QtCore.QRect(00, 220, 200, 120))
self.tablsdata.setObjectName(_fromUtf8("tablsdata"))
self.tablsdata.setColumnCount(1)
self.tablsdata.setRowCount(0)
item = QtGui.QTableWidgetItem()
self.tablsdata.setHorizontalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
# self.tablsdata.setHorizontalHeaderItem(1, item)
# item = QtGui.QTableWidgetItem()
# self.tablsdata.setHorizontalHeaderItem(2, item)
# item = QtGui.QTableWidgetItem()
# self.tablsdata.setHorizontalHeaderItem(3, item)
# item = QtGui.QTableWidgetItem()
# self.tablsdata.setHorizontalHeaderItem(4, item)
# item = QtGui.QTableWidgetItem()
# self.tablsdata.setHorizontalHeaderItem(5, item)
# item = QtGui.QTableWidgetItem()
# self.tablsdata.setHorizontalHeaderItem(6, item)
self.label_6 = QtGui.QLabel(Form)
self.label_6.setGeometry(QtCore.QRect(0, 200, 131, 16))
self.label_6.setObjectName(_fromUtf8("label_6"))
self.month = QtGui.QComboBox(Form)
self.month.setGeometry(QtCore.QRect(0, 190, 69, 22))
self.month.setStyleSheet(_fromUtf8("QComboBox{\n"
" border:2px solid gray;\n"
" border-radius: 10px;\n"
"}\n"
"QComboBox:hover{\n"
" color:white;\n"
" background:white;\n"
"}"))
self.month.setObjectName(_fromUtf8("month"))
self.month.addItem(_fromUtf8(""))
self.month.addItem(_fromUtf8(""))
self.month.addItem(_fromUtf8(""))
self.month.addItem(_fromUtf8(""))
self.month.addItem(_fromUtf8(""))
self.month.addItem(_fromUtf8(""))
self.month.addItem(_fromUtf8(""))
self.month.addItem(_fromUtf8(""))
self.month.addItem(_fromUtf8(""))
self.month.addItem(_fromUtf8(""))
self.month.addItem(_fromUtf8(""))
self.month.addItem(_fromUtf8(""))
self.label_7 = QtGui.QLabel(Form)
self.label_7.setGeometry(QtCore.QRect(450, 190, 51, 20))
self.label_7.setObjectName(_fromUtf8("label_7"))
self.label_8 = QtGui.QLabel(Form)
self.label_8.setGeometry(QtCore.QRect(10, 470, 51, 16))
self.label_8.setObjectName(_fromUtf8("label_8"))
self.timedatas = QtGui.QLineEdit(Form)
self.timedatas.setGeometry(QtCore.QRect(70, 470, 191, 20))
self.timedatas.setReadOnly(True)
self.timedatas.setObjectName(_fromUtf8("timedatas"))
self.label_9 = QtGui.QLabel(Form)
self.label_9.setGeometry(QtCore.QRect(280, 470, 61, 16))
self.label_9.setObjectName(_fromUtf8("label_9"))
self.retranslateUi(Form)
self.month.setCurrentIndex(8)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.label.setText(_translate("Form", "URL地址:", None))
self.label_2.setText(_translate("Form", "一个简单的查询工具", None))
self.ClickedSelect.setText(_translate("Form", "查询", None))
self.label_3.setText(_translate("Form", "所在地区:", None))
# self.label_4.setText(_translate("Form", "自定义地区:", None))
# self.selects.setText(_translate("Form", "...", None))
#self.label_5.setText(_translate("Form", "请选择一个:", None))
item = self.tablsdata.horizontalHeaderItem(0)
item.setText(_translate("Form", "今日", None))
# item = self.tablsdata.horizontalHeaderItem(1)
# item.setText(_translate("Form", "明天()日", None))
# item = self.tablsdata.horizontalHeaderItem(2)
# item.setText(_translate("Form", "后天()日", None))
# item = self.tablsdata.horizontalHeaderItem(3)
# item.setText(_translate("Form", "1()日", None))
# item = self.tablsdata.horizontalHeaderItem(4)
# item.setText(_translate("Form", "2()日", None))
# item = self.tablsdata.horizontalHeaderItem(5)
# item.setText(_translate("Form", "3()日", None))
# item = self.tablsdata.horizontalHeaderItem(6)
# item.setText(_translate("Form", "4()日", None))
# self.label_6.setText(_translate("Form", "未来天气情况如下:", None))
self.month.setItemText(0, _translate("Form", "1月", None))
self.month.setItemText(1, _translate("Form", "2月", None))
self.month.setItemText(2, _translate("Form", "3月", None))
self.month.setItemText(3, _translate("Form", "4月", None))
self.month.setItemText(4, _translate("Form", "5月", None))
self.month.setItemText(5, _translate("Form", "6月", None))
self.month.setItemText(6, _translate("Form", "7月", None))
self.month.setItemText(7, _translate("Form", "8月", None))
self.month.setItemText(8, _translate("Form", "9月", None))
self.month.setItemText(9, _translate("Form", "10月", None))
self.month.setItemText(10, _translate("Form", "11月", None))
self.month.setItemText(11, _translate("Form", "12月", None))
#self.label_7.setText(_translate("Form", "2014年", None))
self.label_8.setText(_translate("Form", "更新时间", None))
self.label_9.setText(_translate("Form", "作者:空白", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
如图: