python - 应该使用什么 tkinter 小部件来显示和存储数据?可以用标签吗?我正在使用 sqlite3 作为数据库
问题描述
def query():
global record
conn = sqlite3.connect('billing.db')
c = conn.cursor()
c.execute("SELECT *, oid FROM billing")
records = c.fetchall()
# print(records)
print_records = ''
for record in records:
print_records += str(record[0]) + " " + str(record[1]) + " " + str(record[2]) \
+ " " + str(record[3]) + " " + str(record[4]) + " " + str(record[5]) \
+ " " + str(record[6]) + " " + str(record[7]) + "\n"
show = Label(root, width=120, text=print_records, anchor=NW)
show.grid(row=2, column=3, columnspan=2)
show.place(x=402, y=50, height=510)
show.config(font=("TimesNewRoman", 10))
conn.commit()
conn.close()
解决方案
这是一个非常简单的示例,ttk.Treeview
用于显示数据库中的数据:
def query():
# Same code above...
records = [('Hello','World'),('This','is'),('Treeview','widget')] # This will c.fetchall()
cols = ('First column','Second columns') # Change this with whatever you want your column name to be
tree = ttk.Treeview(root,columns=cols,show='headings',height=100)
tree.pack(padx=10,pady=10)
for col in cols: # Set treeview attributes
tree.heading(col,text=col)
tree.column(col,width=100,anchor='center')
for items in records: # Insert values inside the treeview
tree.insert('','end',values=items)
请记住,由于这是一个ttk
小部件,因此您必须先导入它:
from tkinter import ttk
如果您希望列具有单独的宽度,请检查此答案
推荐阅读
- javascript - 使用 AJAX 从 XML 显示图像
- css - 将外部 div 缩小到缩放(转换)内部 div 的大小
- vb.net - 如何停靠面板以填充整个表单
- wordpress - wordpress 不稳定的类似缓存的行为
- pdfsharp - 使用 MigraDoc / PdfSharp 自动打印
- java - 如果字符串包含 get getContents (JXL)
- python - Python:使用 Request POST 获取网站数据
- java - Java - 如何检查来自同一个抽象类的两个对象是否相等
- javascript - 如何更改加速度计的值
- matlab - 如何将 tsne() 应用于 MATLAB 表格数据?