如何从 tkinter 中的画布中删除灰色背景

How to remove the grey background from canvases in tkinter

我制作我的第一个 GUI 只是为了好玩,想知道这是否可行,如果可行,如何从我的画布周围移除灰色背景。我也知道代码很乱;这是我第一次使用 Tkinter。

另外澄清一下,项目本身是 100% 假设的,我无意将其用于任何恶意目的。

  import tkinter as tk

    canvas_width = 400
    canvas_height = 20

    colours = ("#476042", "yellow")
    box=[]

    #---- Main Window ----
    window = tk.Tk()
    window.title("Krypto Locker V1.0")
    window.geometry("400x400")
    window.resizable(width=True, height=True)



    #--- Background  -----

    C = tk.Canvas(window, bg="blue", height=250, width=300)
    filename = tk.PhotoImage(file = "C:\Users\lauchlan\Desktop\Programing\project\resources\image.png")
    background_label = tk.Label(window, image=filename)
    background_label.place(x=0, y=0, relwidth=1, relheight=1)

    C.pack()

    #---------------------




    #--- Functions  -----

    def countdown(count):
        # change text in label
        label['text'] = count

        if count > 0:
            # call countdown again after 1000ms (1s)
            window.after(1000, countdown, count-1)



    #--- Main Interface ----


    #-- Text Widget 1 --

    w = tk.Canvas(window,
               width=canvas_width,
               height=canvas_height)
    w.pack()

    w.create_text(canvas_width / 2,
                  canvas_height / 2,
                  text="Welcome to Krytpto Locker V1.0 ")
    w.place(x=10, y=20,)

    #-- Text Widget 2 --

    e = tk.Canvas(window,
               width=500,
               height=20)
    e.pack()

    e.create_text(canvas_width / 2,
                  canvas_height / 2,
                  text="All your personal files will soon be encrypted! ")
    e.place(x=10, y=40,)

    #-- Text Widget 3 --

    h = tk.Canvas(window,
               width=500,
               height=500)
    h.pack()

    h.create_text(   195 / 1,
                   40 / 1, text="‌‌ ‌‌ Hello, you're computer has been infected by Krypto Locker all your \npersonal files will soon be encrypted and deleted in order to prevent this \n‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ ‌‌ you will be required to send 0 to a secure bitcoin wallet\n ‌‌ ‌‌ ‌‌ ‌‌ you will then recieve an email containing a recovery password")
    h.place(x=10, y=70,)





    #--- Countdown Timer -------------------------------------

    #-- Text  --

    m = tk.Canvas(window,
               width=500,
               height=500)
    m.pack()

    m.create_text(80 / 1,
                  50 / 1,
                  text="Time Until Files Encrypted : ")
    m.place(x=1, y=339,)


    #-- Timer  --

    label = tk.Label(window)
    label.place(x=155, y=380,)
    countdown(1000)

    #---------------------------------------------------------



    #--- Recovery Password Box -------------------------------------

    #---- Text ---
    l = tk.Canvas(window,
               width=500,
               height=100)
    l.pack()

    l.create_text(80 / 1,
                  50 / 1,
                  text="Enter Recovery Password : ")
    l.place(x=1, y=250,)

    #---- Entry Box ----


    password = tk.Entry(window)

    password.place(x=165, y=292)


    #---- Submit Button ----

    submit = tk.Button(window, text="Submit",)
    submit.place(x=300, y=289)



    #----------------------------------------------------------------




















    tk.mainloop()

在 tk.mainloop 之前添加以下内容以更改颜色:

h.config(background="snow")
l.config(background="snow")
m.config(background="snow")
e.config(background="snow")
w.config(background="snow")
C.config(background="snow")

您不能使 canvas 透明。此外,更好的做法是将文本和其他小部件添加到 tkinter window(如果您希望图像显示最大)。

为此,将所有文本制作成一个 canvas。并在添加这些之前将图像添加到 canva 。工作还是我。