【问题标题】:How to show csv file in a grid?如何在网格中显示 csv 文件?
【发布时间】:2017-08-02 13:35:57
【问题描述】:

我试图弄清楚如何在 tkinter 的网格中显示 .csv 文件,但在网上找不到太多。

这是我走了多远。

import tkinter


root = tkinter.Tk()

for r in range(3):
    for c in range(4):
          tkinter.Label(root, text='R%s/C%s'%(r,c),borderwidth=1 ).grid(row=r,column=c)

root.mainloop()   

如何使用相同的方法读取.csv 文件?

【问题讨论】:

  • 你做过研究吗?对“python csv”的简单搜索应该足以为您提供有关如何读取 csv 文件的大量信息。
  • 是的,我对 python csv 没问题,但 tkinter 网格让我有点困惑。

标签: python python-2.7 csv tkinter grid


【解决方案1】:

像这样使用树形视图来代替Label,效率会更高。

from tkinter import *
import tkinter.ttk as ttk
import csv

root = Tk()
root.title("Python - Import CSV File To Tkinter Table")
width = 500
height = 400
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width / 2) - (width / 2)
y = (screen_height / 2) - (height / 2)
root.geometry("%dx%d+%d+%d" % (width, height, x, y))
root.resizable(0, 0)

TableMargin = Frame(root, width=500)
TableMargin.pack(side=TOP)

scrollbarx = Scrollbar(TableMargin, orient=HORIZONTAL)
scrollbary = Scrollbar(TableMargin, orient=VERTICAL)
tree = ttk.Treeview(TableMargin, columns=("Employye ID", "Name", "Date",'Registration Time'), height=400, selectmode="extended",
                    yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
scrollbary.config(command=tree.yview)
scrollbary.pack(side=RIGHT, fill=Y)
scrollbarx.config(command=tree.xview)
scrollbarx.pack(side=BOTTOM, fill=X)
tree.heading('Employye ID', text="Employye ID", anchor=W)
tree.heading('Name', text="Name", anchor=W)
tree.heading('Date', text="Date", anchor=W)
tree.heading('Registration Time', text="Time", anchor=W)
tree.column('#0', stretch=NO, minwidth=0, width=0)
tree.column('#1', stretch=NO, minwidth=0, width=120)
tree.column('#2', stretch=NO, minwidth=0, width=120)
tree.column('#3', stretch=NO, minwidth=0, width=120)
tree.column('#3', stretch=NO, minwidth=0, width=120)
tree.pack()
with open('./RegisteredEmployees/RegisteredEmployees.csv') as f:
  reader = csv.DictReader(f, delimiter=',')
  for row in reader:
    emp_id = row['Employye ID']
    name = row['Name']
    dt = row['Date']
    ti = row['Registration Time']
    tree.insert("", 0, values=(emp_id, name, dt,ti))
root.mainloop()

我只是初始化 Treeview,我使用 csv 模块读取 csv 并在 treeview 中插入列和行数据。

结果 See the result here

【讨论】:

    【解决方案2】:

    您可以使用 python csv 模块中的 reader 来读取文件。 Reader 将 .csv 文件作为输入,然后可以像表格一样进行迭代。我已包含代码、示例 .csv 文件和我的结果。

    代码:

    import tkinter
    import csv
    
    root = tkinter.Tk()
    
    # open file
    with open("test.csv", newline = "") as file:
       reader = csv.reader(file)
    
       # r and c tell us where to grid the labels
       r = 0
       for col in reader:
          c = 0
          for row in col:
             # i've added some styling
             label = tkinter.Label(root, width = 10, height = 2, \
                                   text = row, relief = tkinter.RIDGE)
             label.grid(row = r, column = c)
             c += 1
          r += 1
    
    root.mainloop()
    

    CSV 文件:

    col1,col2,col3
    thing1,thing2,thing3
    hi,hey,hello
    

    结果:

    【讨论】:

    • 谢谢我正在寻找的东西。
    猜你喜欢
    • 2010-11-05
    • 2011-03-29
    • 2019-06-11
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    相关资源
    最近更新 更多