首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使tkinter窗口变圆?

如何使tkinter窗口变圆?
EN

Stack Overflow用户
提问于 2021-08-19 09:24:10
回答 2查看 318关注 0票数 2

我正在试着做一个圆型的直接窗子。到目前为止,我已经这样做了:

代码语言:javascript
复制
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()

我使用此函数来移动窗口:

代码语言:javascript
复制
def move(event):
    fx = root.winfo_pointerx() - 250
    fy = root.winfo_pointery() - 10
    root.geometry(f"500x500{fx}+{fy}")

我没有把它添加到代码中,因为当我移动窗口时,它又变成了方形,因为屏幕截图只拍摄了一个特定的区域。

在移动之前:

移动后:

即使我移动它,我怎么能使它转过来?

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

任何帮助都将不胜感激。

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-08-20 10:03:56

如何在tkinter中改变窗口的形状(虽然这在Windows机上应该是可行的,但是其他一些操作系统(macOS,Linux)可能会有问题,这意味着这个精确的代码可能不能工作,也不能给出想要的输出)。代码注释中的说明:

代码语言:javascript
复制
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上最不可能出现的颜色

票数 3
EN

Stack Overflow用户

发布于 2021-08-20 13:40:17

我相信这会解决你的问题。

代码语言:javascript
复制
from tkinter import *
from PIL import ImageTk, Image
import pyautogui as pg


root = Tk()
root.overrideredirect(1)
root.configure(bg="white")
root.attributes("-topmost", 1)
root.attributes("-transparentcolor", "white")
root.geometry("500x500")

label = Label(root, height=900, width=900, bg="white", border=0)
label.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}")


place_center()

img = Image.open("bg.png")
img = img.resize((300, 300), Image.ANTIALIAS)

imagem = ImageTk.PhotoImage(img)
label.configure(image=imagem)

root.mainloop()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68845235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档