在 Tkinter 中如何最好地在表格中顯示資料?
一般來說,我們會以表格的形式來呈現資料。一個表格包含一組行和列。表格中的資料將以行和列的形式順序地儲存。
假設我們正在構建一個 Tkinter 應用程式,其中我們必須將學生資料儲存在表格中的某個位置。該表格結構包含 3 列,用於儲存學生的姓氏、名字和學號。為了顯示此類資訊,Tkinter 提供了一個 **Notebook** 小部件,我們可以在其中以表格的形式儲存資料。
例項
# 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") # Create an object of Style widget style = ttk.Style() style.theme_use('clam') # Add a Treeview widget tree = ttk.Treeview(win, column=("FName", "LName", "Roll No"), show='headings', height=5) tree.column("# 1", anchor=CENTER) tree.heading("# 1", text="FName") tree.column("# 2", anchor=CENTER) tree.heading("# 2", text="LName") tree.column("# 3", anchor=CENTER) tree.heading("# 3", text="Roll No") # Insert the data in Treeview widget tree.insert('', 'end', text="1", values=('Amit', 'Kumar', '17701')) tree.insert('', 'end', text="1", values=('Ankush', 'Mathur', '17702')) tree.insert('', 'end', text="1", values=('Manisha', 'Joshi', '17703')) tree.insert('', 'end', text="1", values=('Shivam', 'Mehrotra', '17704')) tree.pack() win.mainloop()
輸出
如果我們執行上述程式碼,它將顯示一個包含表格的視窗,該表格中有一些學生資料。
廣告