我正在尝试使一个使用tkinter的overrideredirect窗口呈圆形。
目前为止,我已经完成了以下步骤:
from tkinter import Tk, Canvas, BOTH, PhotoImage
from tkinter.constants import NW, RAISED
import pyautogui as pg
root = Tk()
root.overrideredirect(1)
root.attributes("-topmost", 1)
root.geometry("500x500")
# Creating a canvas for placing the squircle shape.
canvas = Canvas(root, height=500, width=500, highlightthickness=0)
canvas.pack()
def place_center(): # Placing the window in the center of the screen
global x, y
reso = pg.size()
rx = reso[0]
ry = reso[1]
x = int((rx/2) - (500/2))
y = int((ry/2) - (500/2))
root.geometry(f"500x500+{x}+{y}")
def round_rectangle(x1, y1, x2, y2, radius=25, **kwargs): # Creating a rounded rectangle
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)
place_center()
# Taking a screenshot and adding it to the canvas to create a transparent effect
root.withdraw()
s = pg.screenshot(region=(x, y, 500, 500))
tp = "C:\\Users\\username\\AppData\\Local\\Temp\\bg.png"
s.save(tp)
bg = PhotoImage(file=tp)
canvas.create_image(0, 0, image=bg, anchor=NW)
root.deiconify()
os.remove(tp)
# Creating the squircle
round_rectangle(0, 0, 500, 500, radius=70, fill="#1fa5fe")
root.mainloop()
我使用这个函数来移动窗口:
def move(event):
fx = root.winfo_pointerx() - 250
fy = root.winfo_pointery() - 10
root.geometry(f"500x500{fx}+{fy}")
我没有在代码中添加它,因为当我移动窗口时,截图仅针对特定区域,所以它会再次变成方形。
移动前:
![移动前的圆形窗口](https://istack.dev59.com/d5Jya.webp)
![移动后的方形窗口](https://istack.dev59.com/DVZu4.webp)
我尝试在
while True:
循环中使用 root.withdraw()
和 root.deiconify()
,但这会导致闪烁。谢谢帮助。
root = Tk()
之后,加入root.config(bg='grey15')
和root.attributes('-transparentcolor', 'grey15')
,同时将画布的背景颜色也设置为相同的颜色。 - Matiissroot.config(bg="grey15"); canvas.config(bg="grey15"); root.attributes("-transparentcolor", "grey15")
代替它。 - TheLizzard