Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

How do I make data orderly on the message box? python-tkinter

My current goal is to display data onto a message box. As this picture shows, the data is all over the place, but if I send it to Excel it looks perfectly fine whereas the message box is just not even.

How can I even out the displayed data on messagebox just to look like the excel?

Here is my code:

import tkinter as tk
import tkinter.messagebox as tm
import pandas
import datetime


def currency_rate():
    dfs=pandas.read_html('https://rate.bot.com.tw/xrt?Lang=en-US')
    currency=dfs[1] 
    currency= currency.iloc[:,0:5] 
    currency.columns=['currency','cash-buying','cash-selling','spot-buying','spot-selliing'] 
    currency['currency'] =currency['currency'].str.extract('((w+))')  
    date=datetime.datetime.now() 
    result_date="Update: Date {:4}.{:02}.{:02}-Time {:02}:{:02}:{:02}".format(date.year,date.month,date.day,date.hour,date.minute,date.second)
    tm.showinfo(title =str(result_date), message =str(currency))  

def currency_rate_import():    
    dfs=pandas.read_html('https://rate.bot.com.tw/xrt?Lang=en-US')
    currency=dfs[1] 
    currency= currency.iloc[:,0:5] 
    currency.columns=['currency','cash-buying','cash
selling','spot
buying','spot-selliing'] 
    currency['currency'] =currency['currency'].str.extract('((w+))')  
    date=datetime.datetime.now() 
    currency.to_excel('Currency Rate{:4}{:02}{:02}-Daily.xlsx'.format(date.year,date.month,date.day))

my_window=tk.Tk()


btn2=tk.Button(my_window,text="Export (Excel)",font="Calibri 14",width=25,height=1,command=currency_rate_import)
btn2.pack(side=tk.BOTTOM) 
btn1=tk.Button(my_window,text="Today's exchange rate",font="Calibri 14",background="yellow",width=25,height=1,command=currency_rate)
btn1.pack(side=tk.BOTTOM) 

my_window.mainloop() 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have good experience using pandas with tabulate and then inserting in Tkinter window with tkinterhtml.

You have "stralign" and "numalign" in tabulate and even "floatfmt" for all sorts of formats and alignment while creating tables.

example for usage:

from tabulate import tabulate
from tkinterhtml import HtmlFrame

html = tabulate(df, 
                headers='keys',
                floatfmt=",.1f", tablefmt='html',
                numalign='center', stralign='center')
top = tk.Toplevel()
hf = HtmlFrame(top)
hf.pack()
hf.set_content(html)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...