gpt4 book ai didi

python - 如何使 tkinter 窗口变圆?

转载 作者:行者123 更新时间:2023-12-05 01:28:15 25 4
gpt4 key购买 nike

我正在尝试制作一个圆形的 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}")

我没有将它添加到代码中,因为当我移动窗口时,它再次变成正方形,因为屏幕截图只截取了特定区域。

搬家前: Round window before moving

移动后: Squared window after moving

如何在移动的同时使它变圆?

我尝试在 while True: 循环中使用 root.withdraw()root.deiconify(),但它会导致闪烁。

如有任何帮助,我们将不胜感激。

谢谢。

最佳答案

如何在 tkinter 中更改窗口的形状(虽然这应该在 Windows 计算机上最有效,但其他一些操作系统(macOS、Linux)可能有问题意味着这个确切的代码可能无法工作并且不会给出所需的输出)。代码注释中的解释:

from tkinter import Tk, Canvas
from PIL import Image, ImageTk
import requests


# load image from web but you can of course load it from file, this is
# just so you can run and immediately see the results
url = 'http://media-s3-us-east-1.ceros.com/g3-communications/images/2019/01/15/05eea4b9b9ce010d2dd6b0c063d2f5ca/p1-blob.png?imageOpt=1&fit=bounds&width=893'
data = requests.get(url, stream=True).raw
# just when loading your own image instead of `data` use the path, and resize
# if you need, can obvs change these numbers
image = Image.open(data).resize((600, 600), Image.ANTIALIAS)


root = Tk()
# set root bg color to some rare color because
# the `-transparentcolor` affects all colors on the window, all of them
root.config(bg='grey15')
# just simply set window to the middle, not necessary
root.geometry(f'+{root.winfo_screenwidth() // 2 - 300}+{root.winfo_screenheight() // 2 - 300}')
# the important part, set the transparentcolor to some rare color in this case `grey15`,
# can obvs be sth else depending on your theme
root.attributes('-transparentcolor', 'grey15')
# remove window from window manager
root.overrideredirect(True)

# create canvas and set the bg to the same "rare" color (width and height set
# to image width and height, can be sth different too)
canvas = Canvas(
root, bg='grey15', width=image.width, height=image.height, highlightthickness=0
)
canvas.pack()
# convert image to PhotoImage for `tkinter` to understand
photo = ImageTk.PhotoImage(image)
# put the image on canvas because canvas supports transparent bg
canvas.create_image(0, 0, image=photo, anchor='nw')

root.mainloop()

关键部分是 .attributes('-transparentcolor', ...)(这很可能在某些操作系统上不起作用)并且窗口的形状由该形状的图像(图像应具有透明背景(.png 图像))。也可以使用 Canvas 方法 (.create_) 简单地绘制形状。同样重要的是要记住该属性会影响窗口上的所有颜色,这意味着例如如果您将透明颜色设置为白色并且背景图像包含白色,它很可能也是透明的(看起来可能不像好)所以我建议使用一些最不可能出现在 GUI 上的颜色

关于python - 如何使 tkinter 窗口变圆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68845235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com