python--以窗口或者canvas为父容器的另一种情况
from tkinter import *
from tkinter import ttk
root = Tk()
canvas = Canvas(root)
tree = ttk.Treeview(canvas, show='headings') # 表格
canvas.grid(row=0, column=0)
tree["columns"] = ("姓名", "年龄", "身高")
tree.heading("姓名", text="姓名") # 显示表头
tree.heading("年龄", text="年龄")
tree.heading("身高", text="身高")
tree.column("姓名", width=100, anchor="center")
tree.column("年龄", width=100, anchor="center")
tree.column("身高", width=800, anchor="center")
MON_FONTSIZE = 15
style = ttk.Style()
style.configure('Treeview.Heading', font=(None, MON_FONTSIZE),
rowheight=int(MON_FONTSIZE * 5.2))
style.configure('Treeview', font=(None, MON_FONTSIZE),
rowheight=int(MON_FONTSIZE * 5.2))
tree.tag_configure("first", foreground="red")
tree.tag_configure("second", foreground="blue")
arr = [{'type': 'player', 'msg': '0.017s 玩家 攻击 1级小怪:6 点伤害,小怪血量51/86'},
{'type': 'enemy', 'msg': '0.017s 9级小怪 攻击 玩家:13 点伤害,玩家血量502/515'},
{'type': 'enemy', 'msg': '0.017s 7级小怪 攻击 玩家:9 点伤害,玩家血量493/515'},
{'type': 'enemy', 'msg': '0.017s 9级小怪 攻击 玩家:13 点伤害,玩家血量502/515'},
{'type': 'enemy', 'msg': '0.017s 7级小怪 攻击 玩家:9 点伤害,玩家血量493/515'}
, {'type': 'enemy', 'msg': '0.017s 9级小怪 攻击 玩家:13 点伤害,玩家血量502/515'},
{'type': 'enemy', 'msg': '0.017s 7级小怪 攻击 玩家:9 点伤害,玩家血量493/515'},
{'type': 'enemy', 'msg': '0.017s 9级小怪 攻击 玩家:13 点伤害,玩家血量502/515'},
{'type': 'enemy', 'msg': '0.017s 7级小怪 攻击 玩家:9 点伤害,玩家血量493/515'}
, {'type': 'enemy', 'msg': '0.017s 9级小怪 攻击 玩家:13 点伤害,玩家血量502/515'},
{'type': 'enemy', 'msg': '0.017s 7级小怪 攻击 玩家:9 点伤害,玩家血量493/515'},
{'type': 'enemy', 'msg': '0.017s 9级小怪 攻击 玩家:13 点伤害,玩家血量502/515'},
{'type': 'enemy', 'msg': '0.017s 7级小怪 攻击 玩家:9 点伤害,玩家血量493/515'}
,
{'type': 'enemy', 'msg': '0.017s 9级小怪 攻击 玩家:13 点伤害,玩家血量502/515'},
{'type': 'enemy', 'msg': '0.017s 7级小怪 攻击 玩家:9 点伤害,玩家血量493/515'}
, {'type': 'enemy', 'msg': '0.017s 9级小怪 攻击 玩家:13 点伤害,玩家血量502/515'},
{'type': 'enemy', 'msg': '0.017s 7级小怪 攻击 玩家:9 点伤害,玩家血量493/515'},
{'type': 'enemy', 'msg': '0.017s 9级小怪 攻击 玩家:13 点伤害,玩家血量502/515'},
{'type': 'enemy', 'msg': '0.017s 7级小怪 攻击 玩家:9 点伤害,玩家血量493/515'},
{'type': 'enemy', 'msg': '0.017s 9级小怪 攻击 玩家:13 点伤害,玩家血量502/515'},
{'type': 'enemy', 'msg': '0.017s 7级小怪 攻击 玩家:9 点伤害,玩家血量493/515'}]
index = 0
for item in arr: # 写入数据
if index == 0:
tree.insert('', index, values=(item.get("type"), item.get("player"), item.get("msg")), tags="first")
else:
tree.insert('', index, values=(item.get("type"), item.get("enemy"), item.get("msg")), tags="second")
index += 1
tree.pack()
vbar = ttk.Scrollbar(canvas, orient=VERTICAL, command=tree.yview)
tree.configure(yscrollcommand=vbar.set)
tree.grid(row=0, column=0, sticky=NSEW)
vbar.grid(row=0, column=1, sticky=NS)
root.mainloop()
红色字体部分很重要,否则滚动条无法生效
本文来自博客园,作者:小虾米吖~,转载请注明原文链接:https://www.cnblogs.com/LindaBlog/p/13931444.html
分类:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」