简体   繁体   English

如何在新的Tkinter窗口上显示字典

[英]How do i display dictionaries on new tkinter window

I'm working on excel file to show the data on new tkinter window. 我正在处理excel文件,以在新的tkinter窗口上显示数据。

Here is the excel data i converted to dict: 这是我转换为dict的Excel数据:

{'Country': {0: 'Japan', 1: 'China', 2: 'USA', 3: 'Russia', 4: 'Japan', 
5: 'Japan', 6: 'China'}, 'Port': {0: 'Yokohama', 1: 'Ningbo', 2: 
'Baltimore', 3: 'Moscow', 4: 'Tokyo', 5: 'Tokyo', 6: 'Shanghai'}, 
'incoterm': {0: 'FOB', 1: 'DAT', 2: 'FOB', 3: 'EXW', 4: 'FOB', 5: 'FOB', 
6: 'EXW'}, 'Capacity': {0: '40ton', 1: '40ton', 2: 'Other', 3: '20ton', 
4: '20ton', 5: 'Other', 6: '40ton'}, 'Date': {0: nan, 1: nan, 2: nan, 3: 
nan, 4: nan, 5: nan, 6: nan}, 'Oct': {0: 400, 1: 500, 2: 600, 3: 100, 4: 
400, 5: 500, 6: 120}, 'Nov': {0: 500, 1: 200, 2: 200, 3: 300, 4: 500, 5: 
600, 6: 985}, 'Dec': {0: 100, 1: 200, 2: 800, 3: 400, 4: 200, 5: 100, 6: 
146}, '$ value': {0: 2650.6, 1: 2650.6, 2: 2650.6, 3: 2650.6, 4: 2650.6, 
5: 2650.6, 6: 2500.6}, 'Total': {0: 2650600.0, 1: 2385540.0, 2: 
4240960.0, 3: 2120480.0, 4: 2915660.0, 5: 3180720.0, 6: 3128250.6}}

What i got so far: 我到目前为止所得到的:

import pandas as pd
from tkinter import *
from tkinter import ttk
df = pd.read_excel("some excel data")

df = df.to_dict()
a = []
a.append(dict(df))
print(a)

root = Tk()

for data in a:
    temp_text = '{0} {1} - ({2})'.format(data['Country'], 
    data['incoterm'], data['Total'])
    ttk.Label(root, text=temp_text).pack()

mainloop()

Output: 输出:

{0: 'Japan', 1: 'China', 2: 'USA', 3: 'Russia', 4: 'Japan', 5: 'Japan', 
6: 'China'}{0: 'FOB', 1: 'DAT', 2: 'FOB', 3: 'EXW', 4: 'FOB', 5: 'FOB', 
6: 'EXW'}-({0: 2650600.0, 1: 2385540.0, 2: 4240960.0, 3: 2120480.0, 4: 
2915660.0, 5: 3180720.0, 6: 3128250.6}})

Expected output: 预期产量:

Japan FOB -(2650600.0)
China EXW -(2385540.0)
....etc

Here convert to list with dictioanry is not necessary, use: 这里不需要用dictioanry转换为列表,使用:

df = pd.DataFrame(a)
print (df)
  Country       Port incoterm Capacity  Date  Oct  Nov  Dec  $ value  \
0   Japan   Yokohama      FOB    40ton   NaN  400  500  100   2650.6   
1   China     Ningbo      DAT    40ton   NaN  500  200  200   2650.6   
2     USA  Baltimore      FOB    Other   NaN  600  200  800   2650.6   
3  Russia     Moscow      EXW    20ton   NaN  100  300  400   2650.6   
4   Japan      Tokyo      FOB    20ton   NaN  400  500  200   2650.6   
5   Japan      Tokyo      FOB    Other   NaN  500  600  100   2650.6   
6   China   Shanghai      EXW    40ton   NaN  120  985  146   2500.6   

       Total  
0  2650600.0  
1  2385540.0  
2  4240960.0  
3  2120480.0  
4  2915660.0  
5  3180720.0  
6  3128250.6 

You can join all columns with converting numeric columns to strings: 您可以通过将数字列转换为字符串来连接所有列:

s = df['Country'] + ' ' + df['incoterm'] + ' - (' +  df['Total'].astype(str) + ')'
for temp_text in s: 
    print (temp_text)

Or use DataFrame.itertuples : 或使用DataFrame.itertuples

for data in df.itertuples():
    temp_text = '{0} {1} - ({2})'.format(data.Country, data.incoterm, data.Total)
    print (temp_text)

If performance is not important use DataFrame.iterrows , but is is slowiest: 如果性能不重要,请使用DataFrame.iterrows ,但最慢:

for i, data in df.iterrows():
    temp_text = data
    temp_text = '{0} {1} - ({2})'.format(data['Country'], data['incoterm'], data['Total'])
    print (temp_text)
Japan FOB - (2650600.0)
China DAT - (2385540.0)
USA FOB - (4240960.0)
Russia EXW - (2120480.0)
Japan FOB - (2915660.0)
Japan FOB - (3180720.0)
China EXW - (3128250.6)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM