码迷,mamicode.com
首页 > 其他好文 > 详细

看盘工具

时间:2019-12-04 13:25:10      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:read   dict   hda   update   type   gbk   data   daemon   pen   

from tkinter import *
import urllib.request
from PIL import Image, ImageTk
import os,io,threading,time
from tkinter import messagebox
win=Tk()
win.title(‘疯陈编程-看盘‘)
win.geometry(‘1270x800+200+100‘)
#win.attributes("-toolwindow", 1)
win.resizable(width=False,height=False)

f1 = LabelFrame(win,text=‘上证日线图‘)
f1.grid(row=0,column=0,padx=10,pady=10)

f2 = LabelFrame(win,text=‘上证分时图‘)
f2.grid(row=0,column=1,padx=10,pady=10)

f3 = LabelFrame(win,text=‘个股日线图‘)
f3.grid(row=1,column=0,padx=10,pady=10)

f4 = LabelFrame(win,text=‘个股分时图‘)
f4.grid(row=1,column=1,padx=10,pady=10)

f5 = LabelFrame(win,text=‘股票查询操作‘)
f5.grid(row=2,column=0,columnspan=2,padx=10,pady=10)

f6 = LabelFrame(win,text=‘盘口数据‘)
f6.grid(row=1,column=1,rowspan=3,padx=10,pady=10)

netDateUrl = ‘http://hq.sinajs.cn/list=sz000078‘

#获取股票数据当日数据
def getStockDate(netDateUrl):
    html = urllib.request.urlopen(netDateUrl).read()
    html = html.decode(‘gbk‘)
    stock_dict={}
    stock_dict[‘code‘] = html[13:19]
    temp = html[21:246].split(‘,‘)
    stock_dict[‘name‘] = temp[0]
    stock_dict[‘open‘] = temp[1]
    stock_dict[‘close‘] = temp[2]
    stock_dict[‘trade‘] = temp[3]
    stock_dict[‘high‘] = temp[4]
    stock_dict[‘low‘] = temp[5]
    stock_dict[‘buy‘] = temp[6]
    stock_dict[‘sell‘] = temp[7]
    stock_dict[‘volume‘] = str(int(temp[8])//100)+‘手‘
    stock_dict[‘money‘] = str(float(temp[9])//10000)+‘万‘
    stock_dict[‘buy1vol‘] = temp[10]
    stock_dict[‘buy1pri‘] = temp[11]
    stock_dict[‘buy2vol‘] = temp[12]
    stock_dict[‘buy2pri‘] = temp[13]
    stock_dict[‘buy3vol‘] = temp[14]
    stock_dict[‘buy3pri‘] = temp[15]
    stock_dict[‘buy4vol‘] = temp[16]
    stock_dict[‘buy4pri‘] = temp[17]
    stock_dict[‘buy5vol‘] = temp[18]
    stock_dict[‘buy5pri‘] = temp[19]
    stock_dict[‘sell1vol‘] = temp[20]
    stock_dict[‘sell1pri‘] = temp[21]
    stock_dict[‘sell2vol‘] = temp[22]
    stock_dict[‘sell2pri‘] = temp[23]
    stock_dict[‘sell3vol‘] = temp[24]
    stock_dict[‘sell3pri‘] = temp[25]
    stock_dict[‘sell4vol‘] = temp[26]
    stock_dict[‘sell4pri‘] = temp[27]
    stock_dict[‘sell5vol‘] = temp[28]
    stock_dict[‘sell5pri‘] = temp[29]
    stock_dict[‘data‘] = temp[30]
    stock_dict[‘time‘] = temp[31]
    
    return stock_dict
#获取图片对象
def getImage(code,imtype):
    if imtype==‘d‘:
        if code[0]==‘6‘ or code==‘000001‘:
            url = ‘http://image.sinajs.cn/newchart/daily/n/sh‘+code+‘.gif‘
        else:
            url = ‘http://image.sinajs.cn/newchart/daily/n/sz‘+code+‘.gif‘
        image_bytes = urllib.request.urlopen(url).read()
        data_stream = io.BytesIO(image_bytes)
        pil_image = Image.open(data_stream)
        tk_image = ImageTk.PhotoImage(pil_image)
        return tk_image
    else:
        if code[0]==‘6‘ or code==‘000001‘:
            url = ‘http://image.sinajs.cn/newchart/min/n/sh‘+code+‘.gif‘
        else:
            url = ‘http://image.sinajs.cn/newchart/min/n/sz‘+code+‘.gif‘
        image_bytes = urllib.request.urlopen(url).read()
        data_stream = io.BytesIO(image_bytes)
        pil_image = Image.open(data_stream)
        tk_image = ImageTk.PhotoImage(pil_image)
        return tk_image
    
#获取上证日线图
tk_image_shday = getImage(‘000001‘,imtype=‘d‘)
L1 = Label(f1, image=tk_image_shday) 
L1.grid(padx=5, pady=5) 
#获取上证分时图
tk_image_min_shshi = getImage(‘000001‘,imtype=‘m‘)
L2 = Label(f2, image=tk_image_min_shshi) 
L2.grid(padx=5, pady=5) 

#获取股票日线图
tk_image_day = getImage(‘000078‘,imtype=‘d‘)
L3 = Label(f3, image=tk_image_day) 
L3.grid(padx=5, pady=5) 
#获取股票分时图
tk_image_min = getImage(‘000078‘,imtype=‘d‘)
L4 = Label(f4, image=tk_image_min) 
L4.grid(padx=5, pady=5) 

var=StringVar()
var.set(‘‘)
Entry(f5,textvariable=var,width=20).grid(row=0,column=5,padx=10,pady=10)


def fun():
    code = var.get()
    if len(code)!=6:
        messagebox.showinfo(‘错误‘,‘请输入正确的股票代码!‘)
        return
    global L4,L3
    global im3,im4
    im3 = getImage(code,imtype=‘d‘)
    im4 = getImage(code,imtype=‘m‘)
    L3.config(image=im3)
    L4.config(image=im4)
    L3.update()
        
def fun2():
    p = threading.Thread(target = fun)
    p.setDaemon(True)#守护进程
    p.start()

Button(f5,text=‘上证指数‘).grid(row=0,column=0,padx=10,pady=10)
Button(f5,text=‘深证指数‘).grid(row=0,column=1,padx=10,pady=10)
Button(f5,text=‘中小板指数‘).grid(row=0,column=2,padx=10,pady=10)
Button(f5,text=‘创业板指数‘).grid(row=0,column=3,padx=10,pady=10)
Button(f5,text=‘数据同步‘).grid(row=0,column=4,padx=10,pady=10)

Button(f5,text=‘查询股票‘,command=fun).grid(row=0,column=6,padx=10,pady=10)
Button(f5,text=‘停止刷新‘).grid(row=0,column=7,padx=10,pady=10)
Button(f5,text=‘退出程序‘).grid(row=0,column=8,padx=10,pady=10)
win.mainloop()

  

看盘工具

标签:read   dict   hda   update   type   gbk   data   daemon   pen   

原文地址:https://www.cnblogs.com/wumac/p/11982162.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!