tkinter-treeview组件

import tkinter as tk
from tkinter import ttk

class Ui:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title('gui')
        self.root.geometry("600x500")
        self.interface()

    def interface(self):
        style = ttk.Style()
        style.configure("Treeview",rowheight=20)
        style.configure("Treeview.Heading",font=("Arial",18,'bold')) #设置标题字体

        self.tree = ttk.Treeview(self.root,height=10,style='Treeview')
        self.tree.grid(row=0,column=0,rowspan=10,columnspan=10,padx=60)

        #列名
        self.tree['columns'] = ("Name","Age","City")

        #设置标题名
        self.tree.heading("#0",text='序号',anchor='w')# #0是默认索引号,标题文本为序号,可以# 
           不填,就隐藏起来了
        self.tree.heading("Name",text='姓名',anchor='w')
        self.tree.heading("Age",text='年龄',anchor='w')
        self.tree.heading("City",text='城市',anchor='w')

        #5设置列宽度(像素),无anchor参数时,列表数据清楚时,默认左对齐#0除外。
        self.tree.column("#0",width=100) 
        self.tree.column("Name",width=100)
        self.tree.column("Age",width=90)
        self.tree.column("City",width=150)

        #插入默认数据
        self.tree.insert("",tk.END,text='1',values=('轩萱',9,"河南郑州"))


if __name__ == '__main__':
    app=Ui()
    app.root.mainloop()
import tkinter as tk
from tkinter import ttk

class Ui:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title('gui')
        self.root.geometry("600x500")
        self.interface()

    def interface(self):
        style = ttk.Style()
        style.configure("Treeview",rowheight=20)
        style.configure("Treeview.Heading",font=("Arial",11,'bold')) #设置标题字体

        self.tree = ttk.Treeview(self.root,height=10,style='Treeview')
        self.tree.grid(row=0,column=0,rowspan=10,columnspan=10,padx=60)

        #列名
        self.tree['columns'] = ("Name","Age","City")

        #设置标题名
        self.tree.heading("#0",text='序号',anchor='w')# #0是默认索引号,标题文本为序号
        self.tree.heading("Name",text='姓名',anchor='w')
        self.tree.heading("Age",text='年龄',anchor='w')
        self.tree.heading("City",text='城市',anchor='w')

        #5设置列宽度(像素),无anchor参数时,列表数据清楚时,默认左对齐#0除外。
        self.tree.column("#0",width=100)
        self.tree.column("Name",width=100)
        self.tree.column("Age",width=90)
        self.tree.column("City",width=150)

        #插入默认数据
        self.tree.insert("",tk.END,text='1',values=('轩萱',9,"河南郑州"))
        self.tree.insert("",tk.END,text='1',values=('宝水',12,"河南郑州"))

        #创建按钮
        self.btn0 = tk.Button(self.root,text='获取数据',command=self.get_data)
        self.btn0.grid(row=11,column=0,ipadx=10,pady=20)

        self.btn1 = tk.Button(self.root,text="获取全部数据",command=self.get_all_data)
        self.btn1.grid(row=11,column=1,ipadx=10,pady=20)

        #添加文本框
        self.label = tk.Label(self.root,text='打印日志',font=("宋体",10),bg='green')
        self.label.grid(row=12,column=0,ipadx=10,pady=10)

        self.w1 = tk.Text(self.root,width=50,height=5)
        self.w1.grid(row=13,column=0,columnspan=5,padx=60)

    def get_data(self):
        '''获取选中数据'''
        selected_items = self.tree.selection()
        if selected_items:
            for item in selected_items:
                row_data = self.tree.item(item)['values']
                self.w1.insert("1.0",f'selected Row Data:{row_data}\n')
        else:
            self.w1.insert("1.0","未选中任何数据\n")

    def get_all_data(self):
        rows = self.tree.get_children()#获取所有行的Id
        #遍历每一行
        for row in rows:
            #获取该行字典
            data = self.tree.item(row)
            self.w1.insert('1.0',f"{data['values']}\n")


if __name__ == '__main__':
    app=Ui()
    app.root.mainloop()
import tkinter as tk
from tkinter import ttk, messagebox


class Ui:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title('gui')
        self.root.geometry("800x500")
        self.interface()

    def interface(self):
        style = ttk.Style()
        style.configure("Treeview",rowheight=20)
        style.configure("Treeview.Heading",font=("Arial",11,'bold')) #设置标题字体

        self.tree = ttk.Treeview(self.root,height=10,style='Treeview')
        self.tree.grid(row=0,column=0,rowspan=10,columnspan=10,padx=60)

        #列名
        self.tree['columns'] = ("Name","Age","City")

        #设置标题名
        self.tree.heading("#0",text='序号',anchor='w')# #0是默认索引号,标题文本为序号
        self.tree.heading("Name",text='姓名',anchor='w')
        self.tree.heading("Age",text='年龄',anchor='w')
        self.tree.heading("City",text='城市',anchor='w')

        #5设置列宽度(像素),无anchor参数时,列表数据清楚时,默认左对齐#0除外。
        self.tree.column("#0",width=100)
        self.tree.column("Name",width=100)
        self.tree.column("Age",width=90)
        self.tree.column("City",width=150)

        #插入默认数据
        self.tree.insert("",tk.END,text='1',values=('轩萱',9,"河南郑州"))
        self.tree.insert("",tk.END,text='1',values=('宝水',12,"河南郑州"))

        #创建按钮
        self.btn0 = tk.Button(self.root,text='添加',command=self.add_data)
        self.btn0.grid(row=11,column=1,ipadx=10,pady=20)

        self.btn1 = tk.Button(self.root,text="编辑",command=self.edit_data)
        self.btn1.grid(row=11,column=3,ipadx=10,pady=20)

        self.btn2 = tk.Button(self.root,text='删除',command=self.delete_data)
        self.btn2.grid(row=11,column=5,ipadx=10,pady=20)

    def add_data(self):
        self.label0 = tk.Label(self.root,text='姓名',bg='pink')
        self.label0.grid(row=0,column=11,ipadx=10)

        self.var0 = tk.StringVar()
        self.enter0 = tk.Entry(self.root,textvariable=self.var0,width=15)
        self.enter0.grid(row=1,column=11)

        self.label1 = tk.Label(self.root,text='年龄',bg='pink')
        self.label1.grid(row=2,column=11,ipadx=10)

        self.var1=tk.StringVar()
        self.enter1 = tk.Entry(self.root,textvariable=self.var1,width=15)
        self.enter1.grid(row=3,column=11)

        self.label2 = tk.Label(self.root, text='城市', bg='pink')
        self.label2.grid(row=4, column=11, ipadx=10)

        self.var2 = tk.StringVar()
        self.enter2 = tk.Entry(self.root, textvariable=self.var2, width=15)
        self.enter2.grid(row=5, column=11)

        self.btn3 = tk.Button(self.root,text='确定',command=self._add_item)
        self.btn3.grid(row=6,column=11,pady=10,ipadx=10)

    def _add_item(self):
        '''添加数据'''
        rows = self.tree.get_children()
        name = self.var0.get()
        age = self.var1.get()
        city = self.var2.get()


        if name and age and city:
            self.tree.insert('',tk.END,text=str(len(rows)+1),values=(name,age,city))
        else:
            self.btn3.bind('<Button-1>',self.window("输入框不可以为空"))


    def edit_data(self):
        selected_items = self.tree.selection()
        if selected_items:
            for item in selected_items:
                row_data = self.tree.item(item)['values']

                self.label0 = tk.Label(self.root, text='姓名', bg='pink')
                self.label0.grid(row=0, column=11, ipadx=10)


                self.var0 = tk.StringVar()
                self.var0.set(row_data[0])
                self.enter0 = tk.Entry(self.root, textvariable=self.var0, width=15)
                self.enter0.grid(row=1, column=11)

                self.label1 = tk.Label(self.root, text='年龄', bg='pink')
                self.label1.grid(row=2, column=11, ipadx=10)

                self.var1 = tk.StringVar()
                self.var1.set(row_data[1])
                self.enter1 = tk.Entry(self.root, textvariable=self.var1, width=15)
                self.enter1.grid(row=3, column=11)

                self.label2 = tk.Label(self.root, text='城市', bg='pink')
                self.label2.grid(row=4, column=11, ipadx=10)

                self.var2 = tk.StringVar()
                self.var2.set(row_data[2])
                self.enter2 = tk.Entry(self.root, textvariable=self.var2, width=15)
                self.enter2.grid(row=5, column=11)

                self.btn3 = tk.Button(self.root, text='确定', command=self._edit_item)
                self.btn3.grid(row=6, column=11, pady=10, ipadx=10)
        else:
            self.btn1.bind('<Button-1>',self.window("未选中数据"))

    def _edit_item(self):
        selected_item = self.tree.selection()
        name=self.var0.get()
        age=self.var1.get()
        city = self.var2.get()
        if name and age and city:
            self.tree.item(selected_item,values=(name,age,city))
        else:
            self.btn3.bind("<Button-1>",self.window("输入框不能为空"))


    def delete_data(self):
        selected_item = self.tree.selection()
        if selected_item:
            self.tree.delete(selected_item)
        else:
            self.btn2.bind('<Button-1>',self.window("未选中数据"))

    def window(e,text):
        messagebox.showinfo('提示',text)






if __name__ == '__main__':
    app=Ui()
    app.root.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值