软件示意图:

# -*- coding:utf-8 -*-

import threading

import requests

import time

from tkinter import *

from tkinter.ttk import *

from tkinter.messagebox import *

class Application_ui(Frame):

# 这个类仅实现界面生成功能,具体事件处理代码在子类Application中。

def __init__(self, master=None):

Frame.__init__(self, master)

self.master.title('12306查票系统')

self.master.geometry('795x351')

self.createWidgets()

def createWidgets(self):

self.top = self.winfo_toplevel()

self.bum = self.winfo_toplevel()

frame = Frame()

frame.place(x=20, y=60, width=750, height=259)

self.style = Style()

self.style.configure('Label1.TLabel', anchor='w', font=('宋体', 9, 'bold'))

self.Label1 = Label(self.top, text='出发地:', style='Label1.TLabel')

self.Label1.place(relx=0.03, rely=0.068, relwidth=0.072, relheight=0.048)

self.Text1Var = StringVar(value='')

self.Text1 = Entry(self.top, textvariable=self.Text1Var, font=('宋体', 9))

self.Text1.place(relx=0.091, rely=0.046, relwidth=0.142, relheight=0.094)

self.style.configure('Label1.TLabel', anchor='w', font=('宋体', 9, 'bold'))

self.Label1 = Label(self.top, text='目的地:', style='Label1.TLabel')

self.Label1.place(relx=0.262, rely=0.068, relwidth=0.072, relheight=0.048)

self.Text2Var = StringVar(value='')

self.Text2 = Entry(self.top, textvariable=self.Text2Var, font=('宋体', 9))

self.Text2.place(relx=0.322, rely=0.046, relwidth=0.142, relheight=0.094)

self.style.configure('Command1.TButton', font=('宋体', 9, 'bold'))

self.Command1 = Button(self.top, text='查 询', command=self.Command2, style='Command1.TButton')

self.Command1.place(relx=0.785, rely=0.046, relwidth=0.172, relheight=0.094)

self.style.configure('Label1.TLabel', anchor='w', font=('宋体', 9, 'bold'))

self.Label2 = Label(self.top, text='出发日期:', style='Label2.TLabel')

self.Label2.place(relx=0.493, rely=0.058, relwidth=0.092, relheight=0.048)

self.number = StringVar()

numberChosen = Combobox(self.top, textvariable=self.number)

numberChosen.place(relx=0.574, rely=0.049, relwidth=0.142, relheight=0.074)

numberChosen['values'] = (1, 2, 4, 42, 100) # 设置下拉列表的值

values = []

y = int(time.strftime("%Y", time.localtime()))

m = int(time.strftime("%m", time.localtime()))

d = int(time.strftime("%d", time.localtime()))

i = 0

yy = y

mm = m

dd = d

while i < 30: # 30天数据

if m in (1, 3, 5, 7, 8, 10, 12):

if d + i > 31:

dd = d + i - 31

mm = m + 1

if mm > 12:

yy = y + 1

mm = mm - 12

else:

dd = d + i

elif m in (4, 6, 9, 11):

if d + i > 30:

dd = d + i - 30

mm = m + 1

if mm > 12:

yy = y + 1

mm = mm - 12

else:

dd = d + i

else:

if (m % 400 == 0) or ((m % 4 == 0) and (m % 100 != 0)):

if d + i > 29:

dd = d + i - 29

mm = m + 1

if mm > 12:

yy = y + 1

mm = mm - 12

else:

dd = d + i

else:

if d + i > 28:

dd = d + i - 28

mm = m + 1

if mm > 12:

yy = y + 1

mm = mm - 12

else:

dd = d + i

s = '%d-%02d-%02d' % (yy, mm, dd)

values.append(s)

i += 1

numberChosen['values'] = tuple(values)

numberChosen.current(0) # 设置下拉列表默认显示的值,0为 numberChosen['values'] 的下标值

scrollBar = Scrollbar(frame)

scrollBar.pack(side=RIGHT, fill=Y)

self.tree = Treeview(frame, height=259,

columns=("车次", "出发站名", "到达站名", "出发时间", "到达时间", "一等座", "二等座", "硬卧", "软卧", "硬座", "无座"),

show="headings", yscrollcommand=scrollBar.set)

scrollBar.configure(command=self.tree.yview)

self.tree.column('车次', width=50, anchor='center')

self.tree.column('出发站名', width=80, anchor='center')

self.tree.column('到达站名', width=80, anchor='center')

self.tree.column('出发时间', width=80, anchor='center')

self.tree.column('到达时间', width=80, anchor='center')

self.tree.column('一等座', width=60, anchor='center')

self.tree.column('二等座', width=60, anchor='center')

self.tree.column('硬卧', width=60, anchor='center')

self.tree.column('软卧', width=60, anchor='center')

self.tree.column('硬座', width=60, anchor='center')

self.tree.column('无座', width=60, anchor='center')

self.tree.heading('车次', text='车次')

self.tree.heading('出发站名', text='出发站名')

self.tree.heading('到达站名', text='到达站名')

self.tree.heading('出发时间', text='出发时间')

self.tree.heading('到达时间', text='到达时间')

self.tree.heading('一等座', text='一等座')

self.tree.heading('二等座', text='二等座')

self.tree.heading('硬卧', text='硬卧')

self.tree.heading('软卧', text='软卧')

self.tree.heading('硬座', text='硬座')

self.tree.heading('无座', text='无座')

self.tree.pack()

class Application(Application_ui):

# 这个类实现具体的事件处理回调函数。界面生成代码在Application_ui中。

def __init__(self, master=None):

Application_ui.__init__(self, master)

header = {

"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",

}

# 获取各个城市的编号

city_url = "https://kyfw.12306.cn/otn/resources/js/framework/station_name.js?station_version=1.9077"

rep = requests.get(city_url, headers=header)

self.areatocode = {}

# 把内容以{城市名:对应的编号}存入字典

for i in rep.content.decode().split("@")[1:]:

if i:

tmp = i.split("|")

self.areatocode[tmp[1]] = tmp[2]

def Command2(self):

"""点击查询按钮触发的逻辑"""

start1 = self.Text1.get()

to1 = self.Text2.get()

date = self.number.get()

# 这里日期判断不够严格 自己可以附加

now_data = time.strftime('%Y.%m.%d', time.localtime(time.time()))

if not start1:

showerror(title='警告', message='出发地不能为空')

elif start1 not in self.areatocode:

showerror(title='警告', message='输入错误,没有找到该城市')

elif not to1:

showerror(title='警告', message='目的地不能为空')

elif to1 not in self.areatocode:

showerror(title='警告', message='输入错误,没有找到该城市')

elif not date:

showerror(title='警告', message='输入错误,没有找到该日期')

elif int(date.replace('-', '')) < int(now_data.replace('.', '')):

showerror(title='警告', message='日期不能小于当期日期')

elif start1 and to1 and date:

if self.Command1['text'] == '查 询':

self.Command1['text'] = '正 在 查 询'

# 每次点击查询按钮后将按钮设置为不可用 防止多次发送请求

self.Command1.config(state=DISABLED)

# 启动一个线程防止程序在查询期间被卡主 出现未响应的情况

t = threading.Thread(target=self.Command1_Cmd, args=(start1, to1, date))

t.start()

def Command1_Cmd(self, start1, to1, date):

try:

start = self.areatocode[start1]

to = self.areatocode[to1]

# 这里有学生和成人之分 默认直接写成成人了

student = "ADULT"

url = "https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=" + date + "&leftTicketDTO.from_station=" + start + "&leftTicketDTO.to_station=" + to + "&purpose_codes=" + student

rep = requests.get(url, headers={

"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"})

patrst01 = '"result":\[(.*?)\]'

# 正则提取查票的结果

rst0 = re.compile(patrst01).findall(rep.content.decode())

checimap_pat = '"map":({.*?})'

checimap = eval(re.compile(checimap_pat).findall(rep.content.decode())[0])

if rst0[0] == '':

showinfo(title='警告', message='没有找到该车次的信息')

rst01 = rst0[0]

allcheci = rst01.split(",")

# 点击查询按钮 先把内容清空

x = self.tree.get_children()

for item in x:

self.tree.delete(item)

for i in range(0, len(allcheci)):

thischeci = allcheci[i].split("|")

code = thischeci[3]

fromname = thischeci[6]

fromname = checimap[fromname]

# [7]---toname

toname = thischeci[7]

toname = checimap[toname]

# [8]---stime

stime = thischeci[8]

# [9]---atime

atime = thischeci[9]

# [28]---一等座

yz = thischeci[31] if str(thischeci[31]) != '' else "-"

# [29]---二等座

wz = thischeci[30] if str(thischeci[30]) != '' else "-"

# [30]---硬座

ze = thischeci[29] if str(thischeci[29]) != '' else "-"

# [31]---无座

zy = thischeci[26] if str(thischeci[26]) != '' else "-"

# 硬卧

xx = thischeci[28] if str(thischeci[28]) != '' else "-"

# 软卧

yy = thischeci[23] if str(thischeci[23]) != '' else "-"

# 将数据回显到软件中

self.tree.insert('', i, values=(

code, fromname, toname, stime, atime, str(yz), str(wz), str(xx), str(yy), str(ze), str(zy)))

# 查询完毕将按钮变为正常

self.Command1.config(state=NORMAL)

self.Command1['text'] = '查 询'

except:

self.Command1.config(state=NORMAL)

self.Command1['text'] = '查 询'

if __name__ == "__main__":

top = Tk()

top.resizable(width=False, height=False)

Application(top).mainloop()

try:

top.destroy()

except:

pass

python查询12306余票_「python」12306余票查询GUI相关推荐

  1. python程序如何执行死刑_「Python基础知识」Python生成器函数

    原标题:「Python基础知识」Python生成器函数 对于程序而言,内存也是很重要的,因为程序中很多数据都是保存在内存中的,如果内存中存储的数据过多,那么系统就会崩溃,这是人们不希望发生的. 可以采 ...

  2. python通过什么对象连接数据库_「Python」连接数据库的三种方式

    连接SQLite 要操作关系数据库,首先需要连接到数据库,一个数据库连接称为Connection: 连接到数据库后,需要打开游标,称之为Cursor,通过Cursor执行SQL语句,然后,获得执行结果 ...

  3. python读文件去除空行_「34」Python文件操作经典案例:CSV文件的读与写

    [1]认识CSV文件 CSV是Comma Separated Values的缩写,它是逗号分隔符文本格式,常用于数据交换.Excel文件和数据库数据的导入和导出. 鉴于CSV的应用场景,编程人员与它打 ...

  4. python画图表的库_「Python」python绘制图表

    介绍一种简单而又功能强大的绘制图形或报表的包-pyecharts,一个基于Echarts(基于JS的数据可视化库)的图标类库,除了绘制常见的折线图.柱状图.饼图.箱型图和散点图外,还可以绘制3D柱状图 ...

  5. python语言中的冒号_「冒号」编程语言中的冒号(:) - seo实验室

    冒号 1.lisp语言中用括弧()分割语句和定义函数参数,用空格分割原子,用分号表示注释,逗号,数组中用,用冒号(:)来定义函数命令,即可以在cad命令行里输入. 2.Python中,用()定义函数参 ...

  6. python设计一个动物类_「Python」每日一练:设计圆类计算周长和面积、设计动物类...

    编程题 1.设计一个 Circle(圆)类,包括半径和颜色属性,编写构造方法和其他方法,计算圆的周长和面积.请编写程序验证类的功能. 2.设计一个 Animal(动物)类,包括颜色属性和叫方法.再设计 ...

  7. python学生数据完善程序_「Python」每日一练:学生数据完善程序

    编程题 1.现有一程序已创建了4个列表,这4个列表中有添加元素,但列表中的数据不完善,有一定的错误,内容如下所示: list1=["学号","姓名"," ...

  8. python删除空白没有显示_「whitespace」python学习笔记:whitespace(空白符)及其判断、删除方法 - seo实验室...

    whitespace 一.string模块里的常量whitespace(string.whitespac)包含6个字符: ASCII码 字符表示 显示 描述 9 \t 无 HT,horizontal ...

  9. python自动接收邮件验证码_「 python 」 python 实现发送邮件验证码

    当我们在注册一个网页时,有的网页会让必须要短信验证.邮箱验证,才可以进行账号的注册.其实,我们在项目中会遇到很多问题,比如:当我们进行爬虫的时候,很容易由于网页的改版,造成我们事先写好的爬虫突然无效, ...

  10. python 最小二乘回归 高斯核_「机器学习」一文读懂线性回归、岭回归和Lasso回归...

    点击上方蓝色字体,关注AI小白入门哟 作者 | 文杰 编辑 | yuquanle 本文介绍线性回归模型,从梯度下降和最小二乘的角度来求解线性回归问题,以概率的方式解释了线性回归为什么采用平方损失,然后 ...

最新文章

  1. 18道经典MySQL面试题,祝您升职加薪
  2. Kafka 孕育开源 KarelDB
  3. linux64位ioremap函数,linux操作系统中的ioremap函数详解
  4. xp电脑自动锁定计算机,WindowsXP系统组策略的锁死的解决方法
  5. 掌握神经网络模型的快捷方式
  6. Windows Phone开发(11):常用控件(下)
  7. sklearn 2.回归树
  8. [Go] 函数/方法 的 变参
  9. Tomcat中文乱码解决办法
  10. HDU2022 海选女主角【最值】
  11. 异步Udp监听关闭 出现异常,访问已释放的资源或者其他错误的解决方法
  12. 【原】创建 WPF 不规则窗口
  13. 惯性导航系统--百科笔记
  14. 基于cpolar内网穿透工具ssh远程访问linux服务器
  15. 用 .NET Core开发BT下载的Tracker服务器
  16. bluecms 手把手教你0day挖掘-基础入门篇
  17. 无线信号不好,如何进行扩展?(中继、桥接、WDS、PLC)
  18. 企业微信授权登录实例代码(仅仅获取过程)
  19. Unity 读取DICOM文件,并支持移动端
  20. 前端实现电子签名(vue组件)

热门文章

  1. 学计算机投影仪定义,一种计算机信息技术教学用投影仪的制作方法
  2. 负载均衡添加ssl证书
  3. RedHat7.5最小化安装之后一些配置
  4. 金耀初教授:进化计算在人工智能领域的发展
  5. 阿里数据中台与OneData
  6. linux 主机名称修改
  7. 数字化名词解释—数字化转型
  8. 推荐一款专家级网站流量分析软件-逆火(Nihuo)
  9. 解决word、typora等打不开超链接问题
  10. .NET Core 分析程序集更优方法,超越ReflectionOnlyLoad