在写第二个analyse类是总是写到了它的父类上就是主界面上,直接把home类中的菜单覆盖了,可能是继承出现了问题,我把all_datas类的内容放到analyse下却是正常的,
另外,time.sleep(4) win_about.destroy()到时无法自动退出。
class First_Page(object):
def __init__(self, master):
self.root = master
self.root.geometry("%dx%d" % (700, 600))
self.root.title('复盘') # 背景颜色
self.createPage()
def createPage(self, cur_dirt=None):
self.page = tkinter.Frame(self.root, width=600, height=480, )
img1 = Image.open("股票图片.png")
img1 = img1.resize((700, 600))
photo1 = ImageTk.PhotoImage(img1)
label1 = tk.Label(self.page, image=photo1, borderwidth=0)
label1.img = photo1
label1.grid(row=0, column=1, sticky="n")
tkinter.Label(self.page, text='股票数据分析', font=('宋体', 28)).place(x=240, y=60)
tkinter.Label(self.page, text='请先爬取数据,等待5秒后再进入软件', font=('宋体', 15)).place(x=200, y=200)
tkinter.Button(self.page, text='爬取今日市场数据', command=scrapy,width=15, height=3,).place(x=200, y=300)
tkinter.Button(self.page, text='进入软件', width=15, height=3, command=self.success_tip).place(x=400, y=300)
self.page.pack()
def success_tip(self):
if os.path.isfile("今日股票趋势.csv"):
self.page.destroy()
Home(self.root)
else:
win_about = tk.Tk()
win_about.geometry("400x200")
win_about.title("提示")
tk.Label(win_about, text='请先爬取数据,等待5秒后再进入软件', font=('宋体', 15)).place()
win_about.mainloop()
time.sleep(4)
win_about.destroy()
class Home(object):
def __init__(self, master: tkinter.Tk):
self.root = master
self.root.geometry("%dx%d" % (1000,800))
self.root.title('主界面')
self.creat_page()
def creat_page(self):
# 市场数据页面
self.all_datas_frame = all_datas(self.root)
#分析页面
self.analyse_frame = analyse(self.root)
# 关于页面
self.about_frame = tkinter.Frame(self.root)
mesg = "xxx"
tkinter.Label(self.about_frame, text=mesg, font=('宋体', 10)).pack()
menubar = tkinter.Menu(self.root)
menubar.add_command(label='市场数据', command=self.show_all_datas)
menubar.add_command(label='个股数据',command=self.show_analyse)
menubar.add_command(label='修改')
menubar.add_command(label='关于', command=self.show_about)
self.root['menu'] = menubar
def show_about(self):
self.about_frame.pack()
self.all_datas_frame.pack_forget()
self.analyse_frame.pack_forget()
def show_analyse(self):
self.analyse_frame.pack()
self.all_datas_frame.pack_forget()
self.about_frame.pack_forget()
def show_all_datas(self):
self.all_datas_frame.pack()
self.about_frame.pack_forget()
self.analyse_frame.pack_forget()
class all_datas(tkinter.Frame):
def __init__(self, root):
super().__init__(root)
self.table_vawe = tkinter.Frame()
class analyse(tkinter.Frame):
def __init__(self,root):
super().__init__(root)
self.plot = tkinter.Frame()
self.name = tkinter.Frame()
self.name.pack()
self.plot.pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
self.code = tkinter.StringVar()
self.create_matplotlib()
self.createWidget(self.figure)
self.master.mainloop()
def createWidget(self, figure):
tkinter.Label(self.name, text='请输入股票代码(请带后缀)', width=30).grid(row=0, column=0)
tkinter.Entry(self.name, textvariable=self.code, width=30, bd=5).grid(row=0, column=1)
self.canvas = FigureCanvasTkAgg(figure, self.plot)
self.canvas.draw()
self.canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
toolbar = NavigationToolbar2Tk(self.canvas, self.plot)
toolbar.update()
self.canvas._tkcanvas.pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
self.button = tkinter.Entry(self, )
self.button.pack(side=tkinter.BOTTOM)
def create_matplotlib(self):
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
self.figure = plt.figure(num=2, figsize=(7, 4), dpi=80, facecolor="gold", edgecolor='green', frameon=True)
fig1 = plt.subplot(1, 1, 1)
x = np.arange(-2 * np.pi, 2 * np.pi, 0.1)
y1 = np.sin(x)
y2 = np.cos(x)
line1 = fig1.plot(x, y1, color='red', linewidth=2, label='y=sin(x)', linestyle='--')
line2 = fig1.plot(x, y2, color='green', label='y=cos(x)')
plt.setp(line2, linewidth=1, linestyle='-', alpha=0.7)
fig1.set_title("数学曲线图", loc='center', pad=20, fontsize='xx-large', color='red')
fig1.legend(['正弦', '余弦'], loc='lower right', facecolor='orange', frameon=True, shadow=True, framealpha=0.7)
# ,fontsize='xx-large'
fig1.set_xlabel('(x)横坐标')
fig1.set_ylabel("(y)纵坐标")
fig1.set_yticks([-1, -1 / 2, 0, 1 / 2, 1])
fig1.grid(which='major', axis='x', color='gray', linestyle='-', linewidth=0.5, alpha=0.2)
if __name__ == '__main__':
root = tkinter.Tk()
First_Page(root)
root.mainloop()