随笔 - 315  文章 - 1  评论 - 12  阅读 - 24万

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()
复制代码

红色字体部分很重要,否则滚动条无法生效

posted on   小虾米吖~  阅读(202)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」

点击右上角即可分享
微信分享提示