You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Tkinter创建的窗口设置圆角效果?

Tkinter无边框窗口设置圆角解决方案

基于你现有的无边框时钟代码,可直接调用Windows原生API实现无锯齿圆角效果,无需额外安装第三方依赖,修改后的完整可运行代码如下:

import tkinter as tk
from tkinter import *
import tkinter.ttk as ttk
import datetime as dt
import calendar
from ctypes import windll
from time import strftime
from tkinter.ttk import *
from time import strftime
import time
import tempfile
import tkinter

# 修正拖动窗口跳位问题
def move_window(event):
    root.geometry(f'+{event.x_root - event.x}+{event.y_root - event.y}')

root = tk.Tk()
root.overrideredirect(1)
root.bind("<B1-Motion>", move_window)
root.eval('tk::PlaceWindow . center')
root.title("Simple Clock App")
root.geometry('300x200')
root.configure(background='gray')

# 新增圆角设置代码开始
# 圆角半径,可自行调整数值
corner_radius = 20
hwnd = windll.user32.GetParent(root.winfo_id())
# 创建圆角矩形裁剪区域,宽高对应窗口尺寸300x200
rgn = windll.gdi32.CreateRoundRectRgn(0, 0, 300, 200, corner_radius, corner_radius)
windll.user32.SetWindowRgn(hwnd, rgn, True)
# 新增圆角设置代码结束

def clock():
    hour = time.strftime("%H")
    minute = time.strftime("%M")
    day = time.strftime("%A")

    my_label.config(text=hour + ":" + minute)
    my_label.after(1000, clock)

    my_label2.config(text=day)

def update():
    my_label.config(text="New Text")

my_label = Label(root, text="", font=("Segoe UI Variable", 48), foreground='white', background='gray')
my_label.pack(pady=20)

my_label2 = Label(root, text="", font=("Segoe UI Variable", 14), foreground='white', background='gray')
my_label2.pack(pady=10)

clock()
root.mainloop()

参数调整说明

  • 修改corner_radius的数值可以调整圆角大小,数值越大圆角越明显
  • 如果你后续修改了窗口的尺寸,需要同步修改CreateRoundRectRgn方法中传入的宽、高参数,和你设置的窗口尺寸保持一致
  • 该方案适配Windows系统,效果无边缘毛边,表现最优

内容的提问来源于stack exchange,提问作者waterstyle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 17:57:03