用python写一个class Ui_MainWindow(object):里放入一个ttk.Treeview得代码
时间: 2024-05-02 15:22:59 浏览: 101
好的,以下是您要求的Python代码:
```python
import tkinter as tk
from tkinter import ttk
class Ui_MainWindow(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.title("My Treeview")
self.tree = ttk.Treeview(self)
self.tree.pack(fill=tk.BOTH, expand=True)
# Inserting Columns
self.tree["columns"] = ("Name", "Gender", "Age")
self.tree.column("#0", width=0, stretch=tk.NO)
self.tree.column("Name", width=100)
self.tree.column("Gender", width=100)
self.tree.column("Age", width=100)
# Adding Headers
self.tree.heading("#0", text="", anchor=tk.CENTER)
self.tree.heading("Name", text="Name", anchor=tk.CENTER)
self.tree.heading("Gender", text="Gender", anchor=tk.CENTER)
self.tree.heading("Age", text="Age", anchor=tk.CENTER)
# Adding Data to Treeview
self.tree.insert(parent="", index=0, iid=0, text="", values=("John Doe", "Male", "28"))
self.tree.insert(parent="", index=1, iid=1, text="", values=("Jane Doe", "Female", "26"))
self.tree.insert(parent="", index=2, iid=2, text="", values=("Bob Smith", "Male", "32"))
if __name__ == '__main__':
app = Ui_MainWindow()
app.mainloop()
```
以上代码实现了一个带有ttk.Treeview的UI界面,其中包括3列数据,分别是:Name、Gender和Age。同时,使用了3条样例数据进行演示。
请注意:以上代码仅供参考,具体实现可能因项目不同而有所变化。
阅读全文