You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Tkinter中实现文本框的圆角效果?

如何在Tkinter中实现文本框的圆角效果?

嘿,我来帮你解决Tkinter文本框加圆角的问题!

首先得说清楚,原生的tk.Text组件本身不支持直接设置圆角,而且ttk.Style也帮不上忙——因为ttk模块里根本没有对应的Text样式类,所以你之前试的ttk.Style()肯定没效果啦。

不过我们可以用Canvas来模拟圆角效果,思路是用Canvas绘制一个圆角背景,然后把Text组件放在这个Canvas上面,让Text的背景和窗口/Canvas背景一致,看起来就像是Text自带圆角一样。下面是修改后的完整代码:

import tkinter as tk
from tkinter import ttk

win = tk.Tk()
WIDTH, HEIGHT = 500, 500
win.geometry(f"{WIDTH}x{HEIGHT}")
win.title("Chat Bot")

# 自定义函数创建带圆角的文本框
def create_rounded_text(parent, corner_radius=10, **kwargs):
    # 创建Canvas用于绘制圆角边框
    canvas = tk.Canvas(parent, bg=parent.cget('bg'), highlightthickness=0)
    canvas.pack(side="bottom", fill="both", expand=True)
    
    # 创建Text组件,去掉边框、背景和窗口一致
    text = tk.Text(canvas, relief="flat", bg=parent.cget('bg'), **kwargs)
    
    # 定义绘制圆角矩形的方法
    def create_rounded_rectangle(x1, y1, x2, y2, radius=25, **kwargs):
        # 生成圆角矩形的多边形顶点
        points = [x1+radius, y1,
                  x1+radius, y1,
                  x2-radius, y1,
                  x2-radius, y1,
                  x2, y1,
                  x2, y1+radius,
                  x2, y1+radius,
                  x2, y2-radius,
                  x2, y2-radius,
                  x2, y2,
                  x2-radius, y2,
                  x2-radius, y2,
                  x1+radius, y2,
                  x1+radius, y2,
                  x1, y2,
                  x1, y2-radius,
                  x1, y2-radius,
                  x1, y1+radius,
                  x1, y1+radius,
                  x1, y1]
        return canvas.create_polygon(points, **kwargs, smooth=True)
    
    canvas.create_rounded_rectangle = create_rounded_rectangle
    
    # 当Canvas大小变化时,重新绘制圆角边框
    def update_rounded_border(event):
        canvas.delete("rounded_border")
        pad = 2
        canvas_width = canvas.winfo_width()
        canvas_height = canvas.winfo_height()
        # 绘制圆角边框,保存为"rounded_border"以便后续清除
        canvas.create_rounded_rectangle(pad, pad, canvas_width-pad, canvas_height-pad,
                                        radius=corner_radius, outline="#ccc", fill=parent.cget('bg'),
                                        tags="rounded_border")
    
    # 把Text放在Canvas内,留出圆角的边距
    text.pack(side="bottom", fill="both", expand=True, padx=corner_radius, pady=corner_radius)
    
    # 初始化绘制,并绑定窗口大小变化事件
    win.update_idletasks()
    update_rounded_border(None)
    canvas.bind("<Configure>", update_rounded_border)
    
    return text

# 创建带圆角的文本框,可调整corner_radius改变圆角大小
text = create_rounded_text(win, height=5, width=10)

# 保留你原来的标签组件
user = ttk.Label(text=" :You")
user.pack(anchor="e")
bot = ttk.Label(text="AI: ")
bot.pack(anchor="nw")

win.mainloop()

我来给你拆解下这段代码的关键点:

  • create_rounded_text函数封装了创建圆角文本框的逻辑,你可以直接调用它来生成带圆角的Text组件
  • 用Canvas的create_polygon配合smooth=True绘制平滑的圆角矩形,作为文本框的边框
  • 将Text组件的relief设为flat(去掉默认边框),背景色和父容器一致,这样就不会遮挡Canvas的圆角
  • 绑定<Configure>事件,当窗口或Canvas大小变化时自动更新圆角边框,保证适配各种尺寸

你可以通过调整corner_radius参数来控制圆角的弧度大小,比如设成15会得到更明显的圆角效果。

内容来源于stack exchange

火山引擎 最新活动