from tkinter import *
import os,time
from tkinter import ttk
from openpyxl import load_workbook
from openpyxl import Workbook
#主界面函数MainPage.py:
class Main_page():
def __init__(self, master=None):
self.root = master #定义内部变量root
self.root.geometry('%dx%d' % (610, 605))
#self.create_page()
def create_page(self):
self.forward_rate = Forward_frame(self.root)# 创建不同Frame
self.accumulate_up = Ccumulate_frame(self.root)
self.add_data = Add_frame(self.root)
self.set_sys = Set_frame(self.root)
self.a_bout = About_frame(self.root)
menubar = Menu(self.root)
colculate_menu = Menu(menubar,tearoff=False)
colculate_menu.add_command(label='递进换手率',command=self.forward)
colculate_menu.add_command(label='累计换手率',command=self.accumulate)
colculate_menu.add_separator()
colculate_menu.add_command(label='退出', command=_quit)
menubar.add_cascade(label='计算',menu=colculate_menu)
menubar.add_command(label='补数据',command=self.add)
menubar.add_command(label='设置', command=self._set)
menubar.add_command(label='关于', command=self._about)
self.root.config(menu=menubar)
self.root['menu'] = menubar # 设置菜单栏
def forward(self):
self.accumulate_up.grid_forget()
self.add_data.grid_forget()
self.a_bout.grid_forget()
self.set_sys.grid_forget()
self.forward_rate.grid()
def accumulate(self):
self.forward_rate.grid_forget()
self.add_data.grid_forget()
self.a_bout.grid_forget()
self.set_sys.grid_forget()
self.accumulate_up.grid()
def add(self):
self.forward_rate.grid_forget()
self.accumulate_up.grid_forget()
self.a_bout.grid_forget()
self.set_sys.grid_forget()
self.add_data.grid()
def _set(self):
self.add_data.grid_forget()
self.forward_rate.grid_forget()
self.accumulate_up.grid_forget()
self.a_bout.grid_forget()
self.set_sys.grid()
def _about(self):
self.add_data.grid_forget()
self.forward_rate.grid_forget()
self.accumulate_up.grid_forget()
self.set_sys.grid_forget()
self.a_bout.grid()
#递进换手率
class Forward_frame(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.mylist = ['换手率','起始日期','递进周期']
self.need_file = mypath+'base\\name_code.xlsx'
self.entry_var = []
self.create_page()
def create_page(self):
i = 1
Label(self, text='递进换手率计算').grid(
row=0, column=1, stick=W, padx=20, pady=10)
for each in self.mylist:
self.entry_var.append(IntVar())
Label(self, text = each).grid(row=i, stick=W, padx=60, pady=10)
Entry(self, textvariable=self.entry_var[i-1],
width=50).grid(row=i, column=1, stick=E)
i += 1
i = 1
for each in [100,int(today_date),15]:
self.entry_var[i-1].set(each)
i += 1
Button(self, text='选择文件',command=self.get_file).grid(
row=11, column=1, stick=E, padx=20,pady=60)
def calc_forward(self):
Button(self, text='确定',state='disable').grid(
row=12, column=1, stick=W, pady=10)
need_list = tr.get_code(self.need_file)#提取所有股票代码
tr.forward_day(mypath,desk_path,need_list,\
self.entry_var[0].get(),str(self.entry_var[1].get()),\
self.entry_var[-1].get())
Button(self, text='确定',\
command=self.calc_forward).grid(
row=12, column=1, stick=W, pady=10)
def get_file(self):
self.need_file = tr.get_file()
Button(self, text='确定',\
command=self.calc_forward).grid(
row=12, column=1, stick=W, pady=10)
#累计换手率
class Ccumulate_frame(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.mylist = ['换手率','起始日期','计算周期']
self.need_file = mypath+'base\\name_code.xlsx'
self.entry_var = []
self.create_page()
return
def create_page(self):
i = 1
Label(self, text='累计换手率计算').grid(row=0, column=1, stick=W, padx=20, pady=10)
for each in self.mylist:
self.entry_var.append(IntVar())
Label(self, text = each).grid(row=i, stick=W, padx=60, pady=10)
Entry(self, textvariable=self.entry_var[i-1], width=50).grid(row=i, column=1, stick=E)
i += 1
i = 1
for each in [100,int(today_date),15]:
self.entry_var[i-1].set(each)
i += 1
Button(self, text='选择文件',command=self.get_file).grid(
row=11, column=1, stick=E, padx=20,pady=60)
return
def calc_rate(self):
Button(self, text='确定',state='disable').grid(row=12, column=1, stick=W, pady=10)
need_list = tr.get_code(self.need_file)#提取股票代码
tr.calculate_rate(mypath,desk_path,need_list,self.entry_var[0].get(),str(self.entry_var[1].get()),\
self.entry_var[-1].get())
Button(self, text='确定',\
command=self.calc_rate).grid(\
row=12, column=1,stick=W, pady=10)
def get_file(self):
self.need_file = tr.get_file()
Button(self, text='确定',
command=self.calc_rate).grid(
row=12, column=1, stick=W, pady=10)
#补数据
class Add_frame(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.add_list = ['新股','日线','除权','流通盘']
self.add_var = []
self.need_file = mypath+'base\\name_code.xlsx'#默认所有A股
self.create_page()
def create_page(self):
Label(self, text='补日线').grid(row=0,column=3,padx=00,pady=60,stick=W)
j = 1
for each in self.add_list:
self.add_var.append(IntVar())
Checkbutton(self,text=each,variable=self.add_var[-1]).grid(
row= 1,column=j,padx=20,pady=10,stick=W)
self.add_var[-1].set('1')
j += 1
Button(self, text='选择文件',command=self.get_file).grid(
row=2, column=4, stick=W, padx=20,pady=60)
Button(self, text='更新',command=self.add_data).grid(
row=12, column=3, stick=W, padx=00,pady=00)
def get_file(self):
self.need_file = tr.get_file()
def add_data(self):
Button(self, text='选择文件',state='disable').grid(
row=2, column=4, stick=W, padx=20,pady=60)
Button(self, text='更新',state='disable').grid(
row=12, column=3, stick=W, padx=00,pady=00)
if self.add_var[-1].get():
tr.get_circulate(mypath,zq_path,today_date) #提取流通盘
if self.add_var[2].get():
gbbq_list = tr.get_gbbq(mypath,today_date)#更新股本变迁资料
if self.add_var[0].get() or self.add_var[1].get():
need_list = tr.get_code(self.need_file)#提取股票代码
need_list = list(set(need_list))#剔除相同的代码
need_list,exist_list = tr.split_list(mypath,need_list)#分别提取转日线和补日线列表表
file_list = tr.day2xlsx(mypath,zq_path,need_list)#转日线
tr.add_name_code(mypath,file_list)#添加名称
tr.add_circulate(file_list)#添加流通盘
tr.add_gbbq(mypath,file_list,first=True)#加股本变迁资料
file_list = tr.calculate_mediary(mypath,file_list,end_row=3)#计算中间数据
if self.add_var[1].get():
tr.add_data(mypath,zq_path,exist_list)#更新日线
tr.add_gbbq(mypath,gbbq_list,first=False)#完善股本变更资料
Button(self, text='更新',command=self.add_data).grid(
row=12, column=3, stick=W, padx=60,pady=160)
#设置
class Set_frame(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.stock_path = []
self.desktop_path = []
self.create_page()
def create_page(self):
global mypath
self.wb = load_workbook(mypath + 'sys\\path.xlsx')
self.ws = self.wb.active
for i in range(2,self.ws.max_row+1):
if self.ws.cell(i,1).value != None:
self.desktop_path.append(self.ws.cell(i,1).value)
if self.ws.cell(i,2).value != None:
self.stock_path.append(self.ws.cell(i,2).value)
Label(self, text='系统设置').grid(row=0,column=3,padx=0,pady=0,stick=W)
Label(self,text='桌面目录: ').grid(row=1,stick=W,padx=60,pady=10)
self.desktop = ttk.Combobox(
self,width=12,textvariable=self.desktop_path)
self.desktop.grid(row=1,column=1,stick=W,padx=60,pady=10)
self.desktop['value'] = self.desktop_path
self.desktop.current(0)
#self.desktop.bind("<>",self.save_path)
Label(self,text='证券软件目录: ').grid(row=2,stick=W,padx=60,pady=10)
self.stockpath = ttk.Combobox(
self,width=12,textvariable=self.stock_path)
self.stockpath.grid(row=2,column=1,stick=W,padx=60,pady=10)
self.stockpath['value'] = self.stock_path
self.stockpath.current(0)
#self.stockpath.bind("<>",self.save_path)
Button(self, text='保存',command=self.save_path).grid(
row=12, column=2, stick=W, padx=10,pady=60)
def save_path(self,*args):
global desk_path,zq_path
desk_path = self.desktop.get()
zq_path = self.stockpath.get()
x = 0
y = 0
for i in range(2,self.ws.max_row+1):
if (self.ws.cell(i,1).value == self.desktop.get()):
x = 1
if (i != 2):
a = self.ws.cell(2,1).value
self.ws.cell(2,1).value = self.desktop.get()
self.ws.cell(i,1).value = a
if (self.ws.cell(i,2).value == self.stockpath.get()):
y = 1
if (i != 2):
a = self.ws.cell(2,2).value
self.ws.cell(2,2).value = self.stockpath.get()
self.ws.cell(i,2).value = a
if x != 1:
self.ws.cell(self.ws.max_row+1,1).value = self.desktop.get()
if y != 1:
self.ws.cell(self.ws.max_row+1,2).value = self.stockpath.get()
self.wb.save(mypath + 'sys\\path.xlsx')
self.wb.close()
return
#版本号
class About_frame(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.create_page()
def create_page(self):
Label(self, text='版本').grid()
Label(self,text='版本号:001 ').grid(
row=1,stick=W,padx=60,pady=30)
Label(self,text='版权所有,未经许可不得使用!!! ').grid(
row=2,stick=W,padx=60,pady=30)
def list_box(self):
'结果显示窗'
sb = Scrollbar(master)
sb.grid(side=RIGHT,fill=y)
lb = Listbox(master,yscrollcommand=sb.set)
for i in range(100):
lb.insert(END,i)
lb.grid(side=LEFT,fill=BOTH)
sb.config(command=lb.yview)
def mk_dir(mypath = 'd:\\turnover_rate\\'):
'检查目录,没有则创建'
os.chdir('d:\\')
if not os.path.exists(mypath):
os.mkdir(mypath)
os.chdir(mypath)
if not os.path.exists('data'):
os.mkdir('data')
if not os.path.exists('base'):
os.mkdir('base')
if not os.path.exists('sys'):
os.mkdir('sys')
sys.path.append(mypath+'sys')
return mypath
def _quit():
'退出'
root.quit()
root.destroy()
global mypath,desk_path,zq_path,need_list,exist_list
mypath = mk_dir()
import turnover as tr
wb = load_workbook(mypath+'sys\\path.xlsx')
ws = wb.active
desk_path = ws.cell(2,1).value
zq_path = ws.cell(2,2).value
wb.close()
today_date = time.strftime('%Y%m%d',time.localtime(time.time()))
root = Tk()
root.title('换手率(天数)')
Main_page(root).create_page()
root.mainloop()