import akshare as ak
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
import pandas as pd
import tkinter.messagebox as messagebox
def get_data_and_save_to_excel(symbol_entry, period_combobox, start_date_entry, end_date_entry,adjust_entry):
symbol = symbol_entry.get()
period = period_combobox.get()
start_date = start_date_entry.get()
end_date = end_date_entry.get()
adjust=adjust_entry.get()
if period == "daily" or period == "weekly" or period == "monthly":
data = ak.stock_zh_a_hist(symbol=symbol, period=period, start_date=start_date, end_date=end_date,adjust=adjust)
else:
period = f"{period}"
data = ak.stock_zh_a_hist_min_em(symbol=symbol, period=period, start_date=start_date, end_date=end_date,adjust=adjust)
file_path = filedialog.asksaveasfilename(defaultextension=".xlsx")
if file_path: # 检查 file_path 是否为空
data.to_excel(file_path)
messagebox.showinfo("导入成功", "成功导入Excel")
def main():
root = tk.Tk()
root.title(“获取股票交易数据”)
symbol_label = tk.Label(root, text="股票代码:")
symbol_label.grid(row=0, column=0)
symbol_entry = tk.Entry(root)
symbol_entry.grid(row=0, column=1)
period_label = tk.Label(root, text="交易频率(每天,每周,每月,1分钟...):")
period_label.grid(row=1, column=0)
period_combobox = ttk.Combobox(root, values=["daily","weekly","monthly",'1','5','15','30','60'])
period_combobox.grid(row=1, column=1)
start_date_label = tk.Label(root, text="起始日期格式(20160601):")
start_date_label.grid(row=2, column=0)
start_date_entry = tk.Entry(root)
start_date_entry.grid(row=2, column=1)
end_date_label = tk.Label(root, text="结束日期格式(20230601):")
end_date_label.grid(row=3, column=0)
end_date_entry = tk.Entry(root)
end_date_entry.grid(row=3, column=1)
adjust_label = tk.Label(root, text="复权不复权(默认空,即不复权;qfq为复权;hfq为复权后的):")
adjust_label.grid(row=4, column=0)
adjust_combobox = ttk.Combobox(root, values=["","qfq", "hfq"])
adjust_combobox.grid(row=4, column=1)
save_button = tk.Button(root, text="保存到Excel",
command=lambda: get_data_and_save_to_excel(symbol_entry, period_combobox, start_date_entry,
end_date_entry,adjust_combobox))
save_button.grid(row=5, column=0, columnspan=2)
root.mainloop()
main()