PySimpleGUI how to make transparent click-through window? - python-3.x

I want to draw a circle on the screen but it should be click-through and I should be able to click somethings behind in this circle. So far, I achieved to draw circle and made it transparent but i couldn't make it click-through. I know that it is possible with pywin32 win32con.WS_EX_LAYERED flag but there is no information about this on PySimpleGUI documentation. How can I solve this problem?
My current window configuration:
window = sg.Window('Sample window', layout,
keep_on_top=True,
auto_size_buttons=False,
grab_anywhere=False,
no_titlebar=True,
return_keyboard_events=False,
alpha_channel=0.8,
use_default_focus=False,
transparent_color='red',
finalize=True)
If it is not possible with PySimpleGUI, is it possible to do with pywin32 module combination?

The transparent_color property can be used to create "click-through" windows. All you need to do is make whatever you want to be click through the same color as the transparent_color. Here is an example where you can toggle the transparency of a shape in a canvas to the transparent color and make it click through.
import PySimpleGUI as sg
layout = [
[sg.Canvas(size=(100, 100), key= 'canvas',)],
[sg.T('Change circle color to:'), sg.Button('Red'), sg.Button('Blue')]
]
window = sg.Window('Sample window', layout,
keep_on_top=True,
auto_size_buttons=False,
grab_anywhere=False,
no_titlebar=True,
return_keyboard_events=False,
alpha_channel=0.8,
use_default_focus=False,
transparent_color='red',
finalize=True)
window.Finalize()
canvas = window['canvas']
cir = canvas.TKCanvas.create_oval(50, 50, 100, 100)
while True:
event, values = window.read()
if event is None:
break
if event == 'Blue':
canvas.TKCanvas.itemconfig(cir, fill="Blue")
elif event == 'Red':
#Here is where the cirle changes to a recolor making it transparent and click through
canvas.TKCanvas.itemconfig(cir, fill="Red")

layout = [[sg.Button("Button) ],
[sg.Graph(canvas_size =(400,100),
graph_bottom_left=(-400,-400),
graph_top_right=(400,400),
background_color='red') ]]
window = sg.Window('Click through transparent',
layout,background_color='red',keep_on_top=True,
transparent_color='red', alpha_channel=.5,
grab_anywhere=True, resizable=True).Finalize()
while True:
event, values = window.Read()
if event == sg.WIN_CLOSED:
break
if event == 'Click':
print(" Button Clicked")
Remember to put keep_on_top = True
This works well on windows, don't know if other os

Related

Display an image using PySimpleGUI and get coordinates of mouse click

Suppose I have an image named 'image.jpg'. I want to be able to display this image using PySimpleGUI and get the pixel coordinate values of a mouse click on the displayed image.
I have looked in many places on the internet but couldn't find a solution. Any ideas are appreciated.
Thanks in advance!
With Graph element, you can set option enable_events=True to generate event when click, also method draw_image to place an image onto the canvas.
Refer https://www.pysimplegui.org/en/latest/call%20reference/#graph-element
import PySimpleGUI as sg
size = (320, 240)
image = sg.EMOJI_BASE64_HAPPY_BIG_SMILE
layout = [
[sg.Graph(size, (0, 0), size, enable_events=True, key='-GRAPH-')],
]
window = sg.Window("Title", layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == '-GRAPH-':
x, y = values[event]
location = (x-28, y+28) # figure size is (56, 56)
# Using option `filename` if the image source is a file
window['-GRAPH-'].draw_image(data=image, location=location)
window.close()

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

I have been looking everywhere for this kind of simple solution and I'm still coming up short.
All I want to do is simply splash some text only onto the screen, without a background or using a "message box" that has a menu bar/buttons with it. Just text, that's all. Maybe with a way to set a color for the font as well as I want the text to be a light gray. Using Windows 10 and Python 3.8+.
I've looked at easygui (message box, uses buttons), PySimpleGUI (best option so far, shown below, but defaults to a white background), Tkinter and pygame (but I have no clue how to set up what I'm looking for with those packages.
This is what I have using PySimpleGui, would love for this to be a transparent background if possible!
def popup_message(message):
sg.popup(str(message),
title = None,
button_color = 'white',
background_color = None,
text_color = 'grey85',#'light gray',
button_type = 5,
auto_close = True,
auto_close_duration = 20,
custom_text = (None, None),
non_blocking = False,
icon = None,
line_width = None,
font = None,
no_titlebar = True,
grab_anywhere = True,
keep_on_top = True,
location = (None, None),
relative_location = (250, 250),
any_key_closes = True,
image = None,
modal = True)
UPDATE
This is the closest thing I've got with using Tkinter. Problem now is the menu bar is still visible, and using the Label class results in a solid background. HOW CAN I ONLY SHOW TEXT ON SCREEN IN PYTHON!? I'm not even sure I'm going about this the right way, but seems like a pretty basic idea?
# Import the Tkinter Library
from tkinter import *
# Create an instance of Tkinter Frame
root = Tk()
# Create a Label to print some text
label = Label(root, text="This is a New Line Text", font= ('Helvetica 14 bold'), foreground= "red3")
label.pack()
# Create a transparent window
root.wm_attributes('-transparentcolor','#add123')
# Set the geometry of window
root.geometry("700x350")
# Add a background color to the Main Window
root.config(bg = '#add123')
root.mainloop()
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()
You're trying to make a sort of screen without a title-menu. In, that case I can help you by telling you to use root.overrideredirect(True) which is a crucial part of your code.
You have two options for drawing text without a background.
The first is to use the canvas to draw the text as seen below. With this approach, you'd have to center the text yourself everytime the window is resized if you care about resizability.
canvas.create_text(x, y, "Text with no bg!")
Second approach is to use a disabled button with an empty img inside.
img = PhotoImage(file="file.png")
b = tk.Button(root, text="My Text", image=img, relief="FLAT", state="DISABLED")
b.image = img
b.pack()
See this answer for more info.
Let me know if you have any other questions.

PySimpleGUI textbox border color

Is there a way to change the color of the textbox border only?
This code results in changing the background color. I just want the border color changed to red.
[sg.Input("textbox",size=(20, 1),background_color="red")]
There's no option for boder color in sg.InputText, here's example for you and it work under my WIN 10. Tkinter code required for it.
import PySimpleGUI as sg
def main():
layout = [
[sg.Input("textbox", size=(20, 1), key='-INPUT-')],
]
window = sg.Window("Title", layout, finalize=True)
window['-INPUT-'].Widget.configure(highlightcolor='red', highlightthickness=2)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
print(event, values)
window.close()
if __name__ == '__main__':
main()

How do you determine rendered text width so it can be centered in pygame?

I am creating my first Pygame board game. I want to display the game title in the sidebar to the right of the game board. I've computed the width of the sidebar, but now I haven't found a method to report how wide the rendered text is so I can subtract half the width from the center-line.
Is there a function for this? I've searched several other sites, but haven't found the right answer yet.
For displaying text in pygame:
Use pygame.font.SysFont to set the font name and size
Use Sysfont.render to create surface object of the text
Use Rect.center to set the center of the text surface
Use Surface.blit to draw the text surface onto the main screen surface
Here's some sample code:
import pygame
pygame.init()
win = pygame.display.set_mode((300,100))
pygame.display.set_caption("Text")
font = pygame.font.SysFont("impact", 40) # font name and size
text = font.render("<< Python >>", False, (100,255,100)) # surface for text
textRect = text.get_rect()
textRect.center = (300//2, 100//2) # center of text is screen center
win.blit(text, textRect) # draw text
pygame.display.update()
while True:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); exit()
Output

show Popup on top of open window in PySimpleGUI

My Popup opens behind the current open window, so unable to see the Popup. How to show the Popup on top of the current open window?
Following is the sample code:
import PySimpleGUI as sg
# set global options for window
background = '#F0F0F0'
sg.SetOptions(background_color=background,
element_background_color=background,
text_element_background_color=background,
window_location=(0, 0),
margins=(0,0),
text_color = 'Black',
input_text_color ='Black',
button_color = ('Black', 'gainsboro'))
layout = [[sg.Button('Ok'), sg.Button('Cancel')]]
window = sg.Window('Test Window', grab_anywhere=False, size=(800, 480), return_keyboard_events=True, keep_on_top=True).Layout(layout).Finalize()
window.Maximize();
while True:
event, values = window.read()
if event in (None, 'Cancel'):
break
else:
sg.Popup('Ok clicked')
I tried Popup with keep_on_top=True but it's not working, window getting priority for show on top so Popup remains hidden behind the window. Is there any way to show Popup above the window?
Setting keep on top in the Popup call created the window on top for me.
sg.Popup('Ok clicked', keep_on_top=True)
However, if you click on the window behind, because it also has keep on top set, it will cover your popup.
Since your main window is being maximized, then perhaps it does not need keep on top set. This will allow you to only set it on the popup so that it does stay on top of your main window.
import PySimpleGUI as sg
# set global options for window
background = '#F0F0F0'
sg.SetOptions(background_color=background,
element_background_color=background,
text_element_background_color=background,
window_location=(0, 0),
margins=(0,0),
text_color = 'Black',
input_text_color ='Black',
button_color = ('Black', 'gainsboro'))
layout = [[sg.Button('Ok'), sg.Button('Cancel')]]
window = sg.Window('Test Window', layout, grab_anywhere=False, size=(800, 480), return_keyboard_events=True, finalize=True)
window.Maximize()
window.BringToFront()
while True:
event, values = window.read()
if event in (None, 'Cancel'):
break
else:
sg.Popup('Ok clicked', keep_on_top=True)

Resources