如何让消息框上的数据有序?蟒蛇tkinter [英] How do I make data orderly on the message box? python-tkinter

查看:51
本文介绍了如何让消息框上的数据有序?蟒蛇tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的目标是将数据显示到消息框.如图所示,数据到处都是,但是如果我把它发送到Excel 它看起来非常好,而消息框只是不均匀.

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.

如何将消息框上显示的数据均匀化,使其看起来像 excel?

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

这是我的代码:

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\nselling','spot\nbuying','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() 

推荐答案

我在 中使用 Pandas 有很好的经验制表,然后使用 tkinterhtml 插入到 Tkinter 窗口中.

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

您可以在表格中使用stralign"和numalign",甚至在创建表格时针对各种格式和对齐方式使用floatfmt".

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

用法示例:

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)

这篇关于如何让消息框上的数据有序?蟒蛇tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆