How to Make a Tkinter Label Background Transparent

How to make a tkinter Label background transparent?

It is not supported with transparent backgrounds in Tk.

How do I make the background of a tkinter label transparent so only the text is seen?

I don't think you can make a Label transparent.

You can create text on a Canvas though, this text by default doesn't have a background:

import datetime
import tkinter as tk


def round_time(dt, round_to):
seconds = (dt - dt.min).seconds
rounding = (seconds + round_to / 2) // round_to * round_to
return dt + datetime.timedelta(0, rounding - seconds, -dt.microsecond)


def ct():
def count():
now = round_time(datetime.datetime.now(), round_to=1)
eh = datetime.datetime(2019, 3, 31, 20, 30)
tte = eh - now
canvas.itemconfig(label_cd, text=str(tte))
root.after(50, count)

count()


root = tk.Tk()
root.title("Earth Hour Countdown!")
now = round_time(datetime.datetime.now(), round_to=1)
eh = datetime.datetime(2019, 3, 31, 20, 30)
tte = eh - now

canvas = tk.Canvas(root, height=360, width=1333)
canvas.pack()

bg_img = tk.PhotoImage(file="C:/Users/bmg/Desktop/eh1.gif")
bg_label = canvas.create_image((0,0), image=bg_img, anchor=tk.N+tk.W)

label_msg = canvas.create_text((410, 120), text="Earth Hour Countdown:", font="MSGothic 50 bold", fill="#652828")

label_cd = canvas.create_text((1030,120), text=str(tte), font="MSGothic 50 bold", fill="#652828")

ehtime_label = canvas.create_text((650,240), text=("Earth Hour:" + eh.strftime("%d-%m-%Y %H:%M:%S")), font="MSGothic 50 bold", fill="#652828")

ct()

root.mainloop()

Note that this also requires some changes to the placement of the text and how to update it. I've tried to keep things as close to your example as possible. Do note that not having a background for your text might not give you the readability you'd like:

Sample Image

Is it possible to have a clear/seethrough label in tkinter?

No, it is not possible to have a clear background in the tkinter label widget. However, there are ways to get the same output. For simple colour backgrounds, you can match the colour in the background of the label. For picture backgrounds like this example, you can use a canvas but the picture has to be within the same canvas.

Here is how you can use Canvas:

import tkinter as tk

win = tk.Tk()
win.config(bg="red")

canvas = tk.Canvas(win, width=200, height=20)
canvas.create_rectangle(0,0,30,30, fill="orange")
canvas.pack()

canvas.create_text(10,10, text="This isn't ideal but it works", anchor="nw")

win.mainloop()

Example Window

As you can see, the text doesn't have any background to it and any items beneath it are visible. However, this only applies to items in the canvas. As shown, the window background is red but the canvas background is the default grey - the canvas itself has a background colour but only to widgets outside of the canvas.

How to display TEXT ONLY (transparent background, with no menu bar or buttons) on screen using Python?

Transparent background of Text in popupis not supported.

New popup function required and defined by yourself in PySimpleGUI.

  • Set option background_color of your Text element to one specified color for transparency, like '#add123'.
  • Set option transparent_color in Window to the specified color for transparency.
  • Set option no_titlebar in Window to hide the titlebar.

Following code show the way

import PySimpleGUI as sg

def popup(message):
global win
if win:
win.close()
layout = [[sg.Text(message, background_color=bg, pad=(0, 0))]]
win = sg.Window('title', layout, no_titlebar=True, keep_on_top=True,
location=(1000, 200), auto_close=True, auto_close_duration=3,
transparent_color=bg, margins=(0, 0))
event, values = win.read(timeout=0)
return win

bg = '#add123'
sg.set_options(font=("Courier New", 24))
layout = [[sg.Button('POPUP')]]
window = sg.Window('title', layout)
win = None
while True:

event, value = window.read()
if event == sg.WIN_CLOSED:
break
elif event == 'POPUP':
win = popup('Here is the message.')
window.force_focus()

if win:
win.close()
window.close()


Related Topics



Leave a reply



Submit