Abismuth
Abismuth

Reputation: 11

Removing background from a label in Tkinter

from tkinter import *
from tkinter import messagebox
import tkinter
import hashlib
from PIL import Image, ImageTk
from win32api import GetSystemMetrics

#===========================================================================================
#functions to center windows
def center_window_x(width):
    x_coordinate = (GetSystemMetrics(0)/2) - (width/2)
    return x_coordinate
def center_window_y(height):
    y_coordinate = (GetSystemMetrics(1)/2) - (height/2)
    return y_coordinate


#===========================================================================================
#function to create setup page
def first_time_setup(width, height):
    setup_window = Tk()

    #===========================================================================================
    #remove window border and position in center
    setup_window.overrideredirect(1)
    setup_frame = Frame(setup_window)
    setup_frame.pack_propagate(False)
    setup_window.geometry('%dx%d+%d+%d' % (width, height, center_window_x(width), center_window_y(height)))

    #===========================================================================================
    #background image for setup window
    canvas = Canvas(setup_window, width=width, height=height)
    canvas.grid(columnspan=2)
    image = Image.open("setup_background.jpg")
    canvas.image = ImageTk.PhotoImage(image)
    canvas.create_image(0, 0, image=canvas.image, anchor="nw")

    #===================================================================================================
    #add username label
    start_title = Label(setup_window, text="Username")
    start_title.place(x=430,y=225)

    #===================================================================================================
    #add admin user entry box 
    admin_user_ent = Entry(setup_window)
    admin_user_ent.place(x=500,y=225)

first_time_setup(650, 300)

Is there a way to remove the white background behind the username label? I know how to change the colour of it, but how do I remove it all together.

Is there a way to remove the white background behind the username label? I know how to change the colour of it, but how do I remove it all together.

sorry for posting twice, apparently there wasn't enough text and too much code.

Upvotes: 0

Views: 19949

Answers (1)

Mike - SMT
Mike - SMT

Reputation: 15226

It sounds like you are asking how to make your Label have a transparent background. From my understanding at the moment tkinter does not have this feature for widgets like labels and buttons. However it is still possible to create your own see-through label with Canvas

Here is an example of using Canvas to achieve something similar to what you are looking to do.

import tkinter as tk

root = tk.Tk()

mycanvas = tk.Canvas(root, width = 200, height = 25)
mycanvas.create_rectangle(0, 0, 100, 40, fill = "green")
mycanvas.pack(side = "top", fill = "both", expand = True)

text_canvas = mycanvas.create_text(10, 10, anchor = "nw")
mycanvas.itemconfig(text_canvas, text="Look no background! Thats new!")

root.mainloop()

enter image description here

Upvotes: 3

Related Questions