本文介绍了如何调整带有形状的 tkinter overrideredirect 窗口的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个圆形的 tkinter 窗口.我正在尝试调整窗口大小.一切正常,但当我尝试移动它时,它又变成方形了.

I have made a tkinter window which is round in shape.I am trying to resize the window.Everything works fine, but when I try to move it, it becomes square again.

我已经添加了再次绘制形状的代码,但它仍然变成方形.

I have added the code to draw the shape again, but still it becomes squared.

代码如下:

from tkinter import Label, Tk, Canvas, BOTH, PhotoImage, Toplevel
from tkinter.constants import BOTTOM, E, NW, RAISED
import pyautogui as pg

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

# Creating a canvas for placing the squircle shape.
canvas = Canvas(root, height=500, width=500, highlightthickness=0, bg='white')
canvas.pack(fill=BOTH, expand=1)

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 move(event):
        global rect
        fx = root.winfo_pointerx() - 250
        fy = root.winfo_pointery() - 10
        root.geometry(f"500x500+{fx}+{fy}")
        # if fx > 1 and fy > 1:
        #         canvas.delete(rect)
        #         rect = round_rectangle(0, 0, fx, fy, radius=50, fill="#1fa5fe")


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)
def cl(event):
        root.quit()

def resize(event):
        def end(event):
                global rect
                root.bind("<B1-Motion>", move)
        global rect
        global x, y
        root.unbind("<B1-Motion>")
        x = root.winfo_pointerx() - root.winfo_rootx()
        y = root.winfo_pointery() - root.winfo_rooty()
        if x > 0:
                fx = root.winfo_rootx()
                fy = root.winfo_rooty() + y
                ht = root.winfo_height() - y
                if ht > 0:
                        root.geometry(f"{x}x{ht}+{fx}+{fy}")
                        canvas.delete(rect)
                        rect = round_rectangle(0, 0, x, ht, radius=50, fill="#1fa5fe")
        root.bind("<ButtonRelease-1>", end)

place_center()

# Creating the squircle
rect = round_rectangle(0, 0, 500, 500, radius=50, fill="#1fa5fe")

root.bind("<B1-Motion>", move)
root.bind("<Button-3>", cl)

rx = root.winfo_rootx()
ry = root.winfo_rooty()
side = Label(canvas, text='  \n', background="blue")
side.place(x=500-10, y=500-10)
side.bind("<B1-Motion>", resize)
root.unbind("<B1-Motion>")

root.mainloop()

这是一些图片.

调整大小前:

调整大小和移动后:

如果您需要,我使用的是 Windows 10.

If you need, I am using Windows 10.

PS:对不起,如果代码写得不好!我正在创建它作为示例,完成后我将应用到我的其他应用中.

PS: Sorry if the code isn't written in good manner! I am creating this as a sample, which I will apply in my other apps when done.

谢谢.

推荐答案

NVM,我解决了问题.

NVM, I solved the problem.

当我移动窗口时,它被设置为默认几何图形,这使它看起来很圆.

When I was moving the window, it was set to default geometry, which made it look round.

我已经对其进行了更改,这是 move() 函数的更新代码:

I have changed it and here's the updated code for the move() function:

def move(event):
        global rect
        fx = root.winfo_pointerx() - 250
        fy = root.winfo_pointery() - 10
        try:
                root.geometry(f"{x}x{ht}+{fx}+{fy}")
        except Exception:
                root.geometry(f"500x500+{fx}+{fy}")

最终代码(有一些变化):

The final code (with a few changes):

from tkinter import Label, Tk, Canvas, BOTH, PhotoImage, Toplevel
from tkinter.constants import BOTTOM, E, NW, RAISED, TOP

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

# Creating a canvas for placing the squircle shape.
canvas = Canvas(root, height=500, width=500, highlightthickness=0, bg='white')
canvas.pack(fill=BOTH, expand=1)

def place_center(): # Placing the window in the center of the screen
    global x, y
    rx = root.winfo_screenwidth()
    ry = root.winfo_screenheight()
    x = int((rx/2) - (500/2))
    y = int((ry/2) - (500/2))
    root.geometry(f"500x500+{x}+{y}")

def move(event):
        global rect
        fx = root.winfo_pointerx() - 250
        fy = root.winfo_pointery() - 10
        try:
                root.geometry(f"{x}x{ht}+{fx}+{fy}")
        except Exception:
                root.geometry(f"500x500+{fx}+{fy}")
        # if fx > 1 and fy > 1:
        #         canvas.delete(rect)
        #         rect = round_rectangle(0, 0, fx, fy, radius=50, fill="#1fa5fe")


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)
def cl(event):
        root.quit()

def resize(event):
        global rect
        global x, y, ht
        x = root.winfo_pointerx() - root.winfo_rootx()
        y = root.winfo_pointery() - root.winfo_rooty()
        if x > 0:
                fx = root.winfo_rootx()
                fy = root.winfo_rooty() + y
                ht = root.winfo_height() - y
                if ht > 0:
                        root.geometry(f"{x}x{ht}+{fx}+{fy}")
                        canvas.delete(rect)
                        rect = round_rectangle(0, 0, x, ht, radius=50, fill="#1fa5fe")

place_center()

top = Canvas(canvas, height=50, bg="#1fa5fe", highlightthickness=0)
top.pack(side=TOP, pady=2)

# Creating the squircle
rect = round_rectangle(0, 0, 500, 500, radius=50, fill="#1fa5fe")

top.bind("<B1-Motion>", move)
root.bind("<Button-3>", cl)

rx = root.winfo_rootx()
ry = root.winfo_rooty()
side = Label(canvas, text='  \n', background="blue")
side.place(x=500-10, y=500-10)
side.bind("<B1-Motion>", resize)
root.unbind("<B1-Motion>")

root.mainloop()

这篇关于如何调整带有形状的 tkinter overrideredirect 窗口的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:31