更改日期格式并在tkinter上显示

时间:2017-05-25 16:10:52

标签: python-2.7 matplotlib tkinter

有没有办法在tkinter图上更改轴的格式?如您所见,我使用绘图小部件将我的图形放在我的框架上。有没有办法让我以num / num格式在底部制作日期?喜欢4/18而不是2017年4月18日?继承我的代码:(也可以改变我的图形背景的颜色吗?它们默认为灰色)

import numpy as np
import datetime as dt
import yahoo_finance as yf
import matplotlib.pyplot as plt
from Tkinter import *
import quandl

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg


root=Tk()
root.geometry('1400x875')

root.title("Stock Information")


fmain=Frame(root, width=1400, height=900)
fmain.place(x=100, y=0)

today=dt.date.today()

thirty_day_graph_frame=Frame(fmain, width=1290, height=300)
thirty_day_graph_frame.place(x=0, y=240)

thirty_days=dt.timedelta(days=43)   
thirty_days_ago=today-thirty_days


five_yrs_graph_frame=Frame(fmain, width=1290, height=300)
five_yrs_graph_frame.place(x=0, y=540)


five_years=dt.timedelta(days=1825)
five_years_ago=today-five_years


def stock_info(stock_name):
    global five_yrs_graph_frame
    five_yrs_graph_frame.destroy()

    global thirty_day_graph_frame
    thirty_day_graph_frame.destroy()





    thirty_day_graph_frame=Frame(fmain, width=1290, height=300)
    thirty_day_graph_frame.place(x=0, y=240)

    five_yrs_graph_frame=Frame(fmain, width=1290, height=300)
    five_yrs_graph_frame.place(x=0, y=540)

    stock=yf.Share(stock_name)
    stock_price=stock.get_price()

    name_price_label=Label(fmain, text=(stock_name,':', stock_price),font=("Times New Roman",23))
    name_price_label.place(x=400, y=10)

    day_change=stock.get_change()

    if float(day_change) > 0:
        font_color="green"
    elif float(day_change) < 0:
        font_color="red"
    else:
        font_color="yellow"

    day_change_label=Label(fmain, text=(day_change),font=("Times New Roman",20),fg=str(font_color))
    day_change_label.place(x=400, y=40)

    thirty_day_data = quandl.get("WIKI/"+str(stock_name), start_date=str(thirty_days_ago), end_date=str(today),column_index=4) #So quandl.get gives a lot of info, so the column_index=4 is just getting closing prices
    five_year_data = quandl.get("WIKI/"+str(stock_name),start_date=str(five_years_ago), end_date=str(today), column_index=4)


    thirty_day_fig = plt.figure(figsize=(10,3.75)) 
    plt.plot(thirty_day_data)
    canvas = FigureCanvasTkAgg(thirty_day_fig, master=thirty_day_graph_frame)
    plot_widget = canvas.get_tk_widget()
    plot_widget.place(x=0,y=0)


    five_year_fig=plt.figure(figsize=(10,3.75))
    plt.plot(five_year_data)
    canvas1=FigureCanvasTkAgg(five_year_fig, master=five_yrs_graph_frame)
    plot_widget1=canvas1.get_tk_widget()
    plot_widget1.place(x=1,y=0)



apple_button=Button(root,text='AAPL', command=lambda:stock_info('AAPL'))
tesla_button=Button(root,text='TSLA', command=lambda:stock_info('TSLA'))
google_button=Button(root,text='GOOG', command=lambda:stock_info('GOOG'))


apple_button.place(x=10, y=15)
tesla_button.place(x=10, y=45)
google_button.place(x=10,y=75)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

您可以使用函数strftime

来使用日期时间库
from datetime import datetime

today = datetime.today()
print ('ISO :', today) # ISO: 2017-05-25 16:23:35.850570   
format = "%d/%m"
s = today.strftime(format)

print (s) # 25/05 

您可以在here

中详细了解此功能并格式化选项