Transparent Background in a Tkinter Window

Transparent background in a Tkinter window

There is no cross-platform way to make just the background transparent in tkinter.

Display an image with transparency and no background or window in Python

Thanks to the suggestion from Kartikeya, I was able to solve my own question.

Using PyQt5, this code will display an image with transparency and no border or background at all

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QMainWindow, QApplication, QLabel

app = QApplication(sys.argv)

window = QMainWindow()

window.setAttribute(Qt.WA_TranslucentBackground, True)
window.setAttribute(Qt.WA_NoSystemBackground, True)
window.setWindowFlags(Qt.FramelessWindowHint)

label = QLabel(window)
pixmap = QPixmap('image.png')
label.setPixmap(pixmap)
label.setGeometry(0, 0, pixmap.width(), pixmap.height())

window.label = label

window.resize(pixmap.width(),pixmap.height())

window.show()
sys.exit(app.exec_())

Once I was looking for PyQt5, I found this question and only needed to modify the code slightly. Here is what it looks like now.

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