我需要在tkinter中使用框架或网格在表格中显示数据。 我已经在tkinter窗口中显示了数据,但我想放在一个表中,所以任何人都可以帮我处理代码(还有滚动条)..
这是代码:
def allClub():
data=cursor.execute("SELECT * from CLUBS order by club_name")
master = Tk()
master.geometry('500x500')
master.title('CLUBS')
Label1 = Label(master, text="CLUB ID", width=10)
Label1.grid(row=0, column=0)
Label2 = Label(master, text="CLUB NAME", width=10)
Label2.grid(row=0, column=1)
Label3 = Label(master, text="RATING", width=10)
Label3.grid(row=0, column=2)
Label1 = Label(master, text="MANAGER", width=10)
Label1.grid(row=0, column=3)
Label1 = Label(master, text="CHAIRMAN", width=10)
Label1.grid(row=0, column=4)
Label1 = Label(master, text="LEAGUE", width=15)
Label1.grid(row=0, column=5)
Label1 = Label(master, text="TITLES", width=10)
Label1.grid(row=0, column=6)
Label1 = Label(master, text="YEAR FOUNDED", width=10)
Label1.grid(row=0, column=7)
for index, dat in enumerate(data):
Label(master, text=dat[0]).grid(row=index+1, column=0)
Label(master, text=dat[1]).grid(row=index+1, column=1)
Label(master, text=dat[2]).grid(row=index+1, column=2)
Label(master, text=dat[3]).grid(row=index+1, column=3)
Label(master, text=dat[4]).grid(row=index+1, column=4)
Label(master, text=dat[5]).grid(row=index+1, column=5)
Label(master, text=dat[6]).grid(row=index+1, column=6)
Label(master, text=dat[7]).grid(row=index+1, column=7)
此处输出窗口的屏幕截图链接: https://i.stack.imgur.com/zFymD.jpg
答案 0 :(得分:1)
Tkinter没有任何“表格”小部件,如果您计划有很多行和列,那么您可以使用的最好的事情是Treeview
或Listbox
。
另一方面,您无法为Frame
创建滚动条,因为该小部件的文档并未说明它支持滚动。这个问题的解决方案涉及创建画布,您可以查看here。
以下是Treeview
小部件的示例:
from tkinter import *
from tkinter import ttk
root = Tk()
root.geometry("500x200")
data = [ ["val1", "val2", "val3"],
["asd1", "asd2", "asd3"],
["bbb1", "bbb3", "bbb4"],
["ccc1", "ccc3", "ccc4"],
["ddd1", "ddd3", "ddd4"],
["eee1", "eee3", "eee4"] ]
frame = Frame(root)
frame.pack()
tree = ttk.Treeview(frame, columns = (1,2,3), height = 5, show = "headings")
tree.pack(side = 'left')
tree.heading(1, text="Column 1")
tree.heading(2, text="Column 2")
tree.heading(3, text="Column 3")
tree.column(1, width = 100)
tree.column(2, width = 100)
tree.column(3, width = 100)
scroll = ttk.Scrollbar(frame, orient="vertical", command=tree.yview)
scroll.pack(side = 'right', fill = 'y')
tree.configure(yscrollcommand=scroll.set)
for val in data:
tree.insert('', 'end', values = (val[0], val[1], val[2]) )
root.mainloop()
答案 1 :(得分:0)
# One way to make a table is to use a loop for the Entry class.
import tkinter as tk
win=tk.Tk()
win.title('Tk GUI')
cols=['Col1','Col2','Col3']
data = [ ["val1", "val2", "val3"],
["asd1", "asd2", "asd3"],
["bbb1", "bbb3", "bbb4"],
["ccc1", "ccc3", "ccc4"],
["ddd1", "ddd3", "ddd4"],
["eee1", "eee3", "eee4"] ]
for y in range(len(data)+1):
for x in range(len(cols)):
if y==0:
e=tk.Entry(font=('Consolas 8 bold'),bg='light blue',justify='center')
e.grid(column=x, row=y)
e.insert(0,cols[x])
else:
e=tk.Entry()
e.grid(column=x, row=y)
e.insert(0,data[y-1][x])
win.mainloop()