【问题标题】:Changing date format and show it on tkinter更改日期格式并在 tkinter 上显示
【发布时间】:2017-10-26 09:00:43
【问题描述】:

有没有办法改变 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()

【问题讨论】:

    标签: python-2.7 matplotlib tkinter


    【解决方案1】:

    你可以通过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 中阅读有关此功能和格式选项的更多信息

    【讨论】:

    • 谢谢,太棒了!你知道我怎样才能让我的图表有不同的背景颜色吗?您会注意到,当您运行它时,它会出现在一个灰色框中。我可以改变吗?
    猜你喜欢
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-23
    • 2019-03-31
    相关资源
    最近更新 更多