我该如何做到这一点,以便tkinter中画布的背景是透明的?

发布于 2025-02-03 03:16:36 字数 1263 浏览 2 评论 0原文

我正在尝试做到这一点,以便我在Tkinter上有一个画布。我希望画布的背景是透明的。我使用Mac。我该怎么做? 这是我的代码:

import tkinter as tk
import string as s
import turtle as tur

root = tk.Tk()
root.title('Test')
root.geometry('1010x1010+10+10')

turtleCa = tk.Canvas(root, width=375, height=900)
turtleCa.place(x=0, y=0)
t = tur.RawTurtle(turtleCa)
t.speed(0)
t.hideturtle()

prog = 0


def hang():
    global prog
    global t
    if prog == 0:
        t.penup()
        t.goto(125, -20)
        t.pendown()
        t.goto(125, 300)
        t.goto(0, 300)
        t.goto(0, 275)
    elif prog == 1:
        t.left(180)
        t.circle(35)
    elif prog == 2:
        t.penup()
        t.goto(0, 205)
        t.pendown()
        t.goto(0, 185)
    elif prog == 3:
        t.goto(-100, 110)
    elif prog == 4:
        t.penup()
        t.goto(0, 185)
        t.pendown()
        t.goto(100, 110)
    elif prog == 5:
        t.penup()
        t.goto(0, 185)
        t.pendown()
        t.goto(0, 70)
    elif prog == 6:
        t.goto(-100, -5)
    elif prog == 7:
        t.penup()
        t.goto(0, 70)
        t.pendown()
        t.goto(100, -5)
    prog = prog + 1

for i in range(8):
    hang()

root.mainloop()
tur.mainloop()

我试图使乌龟画布(Turtleca)的背景保持透明,以使其背后的根部仍然可以看到。谢谢!

I am trying to make it so that I have a canvas on tkinter for a turtle. I want the background of the canvas to be transparent. I use a mac. How would I do that?
Here is my code:

import tkinter as tk
import string as s
import turtle as tur

root = tk.Tk()
root.title('Test')
root.geometry('1010x1010+10+10')

turtleCa = tk.Canvas(root, width=375, height=900)
turtleCa.place(x=0, y=0)
t = tur.RawTurtle(turtleCa)
t.speed(0)
t.hideturtle()

prog = 0


def hang():
    global prog
    global t
    if prog == 0:
        t.penup()
        t.goto(125, -20)
        t.pendown()
        t.goto(125, 300)
        t.goto(0, 300)
        t.goto(0, 275)
    elif prog == 1:
        t.left(180)
        t.circle(35)
    elif prog == 2:
        t.penup()
        t.goto(0, 205)
        t.pendown()
        t.goto(0, 185)
    elif prog == 3:
        t.goto(-100, 110)
    elif prog == 4:
        t.penup()
        t.goto(0, 185)
        t.pendown()
        t.goto(100, 110)
    elif prog == 5:
        t.penup()
        t.goto(0, 185)
        t.pendown()
        t.goto(0, 70)
    elif prog == 6:
        t.goto(-100, -5)
    elif prog == 7:
        t.penup()
        t.goto(0, 70)
        t.pendown()
        t.goto(100, -5)
    prog = prog + 1

for i in range(8):
    hang()

root.mainloop()
tur.mainloop()

I am trying to make the turtle Canvas (turtleCa)'s background be transparent such that the root behind it will still be visible. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌辣 2025-02-10 03:16:36

而不是使用root.attributes(' - 透明',1)您可以使用root.wm_attributes(' - tryparentColor','red'')

是代码

from tkinter import *
 
root = Tk()
root.geometry("400x400")
root.wm_attributes('-transparentcolor', 'red') #use a color you wont use in your project i am using red as an example
 
def transparent():
    height = 300
    width = 300

    c = Canvas(root, width = width, height = height, bg = 'red') #bg is 'red' so it is invisible
    c.pack(side="top", anchor = 'c', expand=True)

transparent()
root.mainloop()

这 看起来像:

“在此处输入图像描述”

Instead of using root.attributes('-transparent', 1) you can use root.wm_attributes('-transparentcolor', 'red')

Here is the code

from tkinter import *
 
root = Tk()
root.geometry("400x400")
root.wm_attributes('-transparentcolor', 'red') #use a color you wont use in your project i am using red as an example
 
def transparent():
    height = 300
    width = 300

    c = Canvas(root, width = width, height = height, bg = 'red') #bg is 'red' so it is invisible
    c.pack(side="top", anchor = 'c', expand=True)

transparent()
root.mainloop()

How it looks like:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文