Text box simply do not stand out against the wall paper.

Joined
Feb 7, 2025
Messages
1
Reaction score
0
Hi All,
I put together a script using python and it pretty much does what I want. Maybe it is not a new idea, but I don't know, as I new to this. Anway, here is the run down or summary if you like.
Desktop Overlay (the script) provides an interactive desktop overlay that you can drag and resize, create dynamically styled text boxes by right‑clicking, perform web and file searches, and view file search results—all while maintaining a transparent appearance that doesn’t interfere with the taskbar or underlying applications.

Below is a summary of the functions and features provided by the overlay script:

  1. Overlay Window Creation & Layout:
    • Frameless, Transparent Overlay:
      The script creates a frameless overlay window using PyQt5 that is mostly transparent. It is sized to cover the available desktop area (excluding the taskbar) so that it does not cover important system elements.
    • Draggable & Resizable:
      The overlay supports user interaction: you can drag the window by left‑clicking and moving it, and you can resize it by left‑clicking near the bottom‑right corner (within a 10‑pixel margin).
  2. Close Button:
    • A close button (displayed as "X") is positioned in the top‑right corner of the overlay.
    • The close button is styled with a red background and black text and is repositioned automatically (via the overridden resizeEvent) so that it always stays at the top‑right regardless of window size changes.
  3. Dynamic Text Box Creation:
    • Right‑Click to Create Text Box:
      When you right‑click on the overlay, a text box (QLineEdit) is created at the mouse click location.
    • Adaptive Styling Based on Wallpaper Brightness:
      The text box’s appearance (its background and text color) is dynamically adjusted based on the brightness of the underlying desktop wallpaper.
      • For bright areas, it sets a light background with dark, bold text.
      • For dark areas, it uses a dark background with white, bold text.
    • Bold Text:
      All user-entered text in the text box is rendered in bold to improve legibility.
  4. Search Functionality:
    • Web Search:
      If the query entered into the text box does not start with "file:", the script constructs a search URL (using Google by default) and opens it in the user’s default web browser.
    • File Search:
      If the query starts with "file:", the script treats the rest of the query as a file search term. It then scans all drives (from A: to Z:) for files whose names contain the search term (case‑insensitive).
      • It includes error handling during directory scanning to avoid crashes if some directories are inaccessible.
  5. Displaying File Search Results:
    • Plain Text Results Widget:
      The matching file paths are displayed in a centered QPlainTextEdit widget that shows the list of found files.
    • Auto‑Close:
      The results widget displays a message stating that it will close automatically after 15 seconds, and it does so automatically to keep the overlay uncluttered.
  6. Utility Functions & Windows API Integration:
    • Brightness Calculation:
      The script includes a function that captures a small (1×1 pixel) image of a given area of the desktop wallpaper and computes its average brightness using a standard luminosity formula (0.299 × R + 0.587 × G + 0.114 × B). This value drives the dynamic text box styling.
    • Empty Text Box Cleanup:
      A timer is set for each text box so that if no text is entered within 10 seconds, the text box is automatically removed.
    • Z‑Order Adjustment:
      Using ctypes to call Windows API functions (like SetWindowPos and GetWindowLongW), the script can modify the overlay’s Z‑order (for example, removing the “always on top” attribute when desired).
My issue is number 3.
  • Adaptive Styling Based on Wallpaper Brightness:
    The text box’s appearance (its background and text color) is dynamically adjusted based on the brightness of the underlying desktop wallpaper.
    • For bright areas, it sets a light background with dark, bold text.
    • For dark areas, it uses a dark background with white, bold text.
Because the overlay is transparent, if it made fully transparent the text box disappears and there is no input from the mouse click, so this is the current setting

available = self.screen().availableGeometry()
self.setGeometry(available)

self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.Tool)
self.setWindowOpacity(0.2)
self.setStyleSheet("background: rgba(50, 50, 50, 0);") # Fully transparent background

Captures a 1x1 scaled image of the specified region of the desktop wallpaper and computes its brightness.
desktop_hwnd = ctypes.windll.user32.GetDesktopWindow()
screen = QApplication.primaryScreen()
pixmap = screen.grabWindow(desktop_hwnd, x, y, w, h)
image = pixmap.toImage().scaled(1, 1)
color = QColor(image.pixel(0, 0))
brightness = 0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()
return brightness

This is the text box.

Adjusts the text box's style based on the wallpaper behind it.
The text is rendered in bold; for bright areas, a light background with dark bold text is used;
otherwise, a dark background with white bold text is applied.
"""
geo = text_box.geometry()
global_top_left = text_box.mapToGlobal(geo.topLeft())
x, y = global_top_left.x(), global_top_left.y()
w, h = geo.width(), geo.height()
brightness = self.get_average_brightness(x, y, w, h)
if brightness > 128:
new_style = ("background: rgba(240,240,240,0.8); "
"color: black; font-size: 16px; font-weight: bold; padding: 5px; border-radius: 5px;")
else:
new_style = ("background: rgba(25,25,25,0.8); "
"color: white; font-size: 16px; font-weight: bold; padding: 5px; border-radius: 5px;")
text_box.setStyleSheet(new_style)

Is there any way that i can adjust this script/code so that the text is as per my requirements white on dark and dark on bright?

Cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,218
Messages
2,571,124
Members
47,727
Latest member
smavolo

Latest Threads

Top