使用 Tkinter 建立一個表格外觀
表格以行和列的形式包含資料項。考慮在應用程式中有一個表格 GUI 的情況,我們可以在其中使用其他 Python 庫(如 Numpy、Pandas、Matplotlib 等)操作資料。Tkinter 提供了 TreeView 小部件,使使用者能夠繪製表格並向其中插入資料。TreeView 小部件可以透過定義 Treeview(parent, column, **options) 建構函式來構建。
示例
# Import the required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame
win = Tk()
# Set the size of the tkinter window
win.geometry("700x350")
s = ttk.Style()
s.theme_use('clam')
# Add a Treeview widget
tree = ttk.Treeview(win, column=("c1", "c2", "c3"), show='headings', height=5)
tree.column("# 1", anchor=CENTER)
tree.heading("# 1", text="ID")
tree.column("# 2", anchor=CENTER)
tree.heading("# 2", text="FName")
tree.column("# 3", anchor=CENTER)
tree.heading("# 3", text="LName")
# Insert the data in Treeview widget
tree.insert('', 'end', text="1", values=('1', 'Joe', 'Nash'))
tree.insert('', 'end', text="2", values=('2', 'Emily', 'Mackmohan'))
tree.insert('', 'end', text="3", values=('3', 'Estilla', 'Roffe'))
tree.insert('', 'end', text="4", values=('4', 'Percy', 'Andrews'))
tree.insert('', 'end', text="5", values=('5', 'Stephan', 'Heyward'))
tree.pack()
win.mainloop()輸出
執行上述程式碼將顯示一個視窗,其中包含一個表格,其中包含一些行和列。
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP