【发布时间】:2019-09-15 17:32:24
【问题描述】:
我有这样的数据。
tempList2=[{'Date': '21-Aug-2019', 'Day': 'Sunday', 'Status': 'This is the message. It should be wrapped!!'}, {'Date': '22-Aug-2019', 'Day': 'Monday', 'Status': 'Message Delivered'}, {'Date': '23-Aug-2019', 'Day': 'Tuesday', 'Status': 'Invalid Data found!! Please retry'}]
接下来,我将为该数据创建一个dataframe。之后,我使用tkinter模块查看数据。
这是示例代码:
import pandas as pd
import tkinter as tk
a =[]
tempList2=[{'Date': '21-Aug-2019', 'Day': 'Sunday', 'Status': 'This is the message. It should be wrapped!!'}, {'Date': '22-Aug-2019', 'Day': 'Monday', 'Status': 'Message Delivered'}, {'Date': '23-Aug-2019', 'Day': 'Tuesday', 'Status': 'Invalid Data found!! Please retry'}]
for i in tempList2:
print(i)
print(type(i))
b = list(i.values())
a.append(b)
print(a)
tempList = a
df = pd.DataFrame(tempList)
# --- functions ---
def change(event, row, col):
# get value from Entry
value = event.widget.get()
# set value in dataframe
df.iloc[row,col] = value
print(df)
# --- main --
root = tk.Tk()
# create entry for every element in dataframe
rows, cols = df.shape
for r in range(rows):
for c in range(cols):
e = tk.Entry(root)
e.insert(0, df.iloc[r,c])
e.grid(row=r, column=c)
# ENTER
e.bind('<Return>', lambda event, y=r, x=c: change(event,y,x))
# ENTER on keypad
e.bind('<KP_Enter>', lambda event, y=r, x=c: change(event,y,x))
# start program
root.mainloop()
这是我得到的输出图像:
在输出中,数据不会被打包。数据被破坏。我需要将数据包含在特定行中。我有很多数据要查看。所以我需要一个滚动条来访问它。帮助我提供一些解决方案来包装数据和附加到整个窗口的滚动条。
【问题讨论】:
-
为什么不使用 ttk.treeview?
-
@1966bc,感谢您的回复。是的。我在 tkinter 列表框中使用了该树视图。但是没有边框的列表框白色背景使我的数据难以阅读。所以我转移到这个来让我的数据填充在框中,这让我很容易查看......因为我是 python 新手,我找不到更好的解决方案。
-
我(或其他人)也许可以帮助您,但没有安装 pandas — 所以我强烈建议您在不需要的问题中添加 minimal reproducible example。 .
-
@cheezeyes:“数据没有被包装”:
Entry不支持wrap,请改用Text。
标签: python python-3.x tkinter tkinter-canvas tkinter-layout