show Popup on top of open window in PySimpleGUI - python-3.x

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)

Related

Window doesn't show up on tkinter window. It shows up in the Mac menubar instead. How is this fixable?

I want the menu to show up in the tkinter window. Why is it showing up in the Mac menubar. I notice this is a Mac problem as I don't really have this problem with windows.
Picture of the problem:
Code:
# import libraries
import tkinter as tk
from tkinter import Menu
# window
window = tk.Tk()
window.title("NewPlay")
window.geometry('1000x800')
window.minsize(1000, 800) # minimum size user can scale window
window.maxsize(1200, 1000) # maximum size user can scale window
# menu
def menu():
# menubar
menuBar = Menu(window)
window.config(menu=menuBar)
# creates menu
user_menu = Menu(menuBar)
# exit program
user_menu.add_command(
label='Exit',
command=window.destroy
)
# search for music
menuBar.add_cascade(
label="Search",
menu=user_menu
)
# loop window
menu()
window.config(bg="orange") # background color
window.mainloop()

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.

How can I change (specifically "config" just like we can config the text) the 'command' on a button in canvas

I have this a situation in which I want to change the command of button using a defined function.
In this below code when I click on "Print Output" button it executes "first_func". And the second button "Change Output" is created to change the function(which is to be executed) of "Print Output" button.
When we click on "Change Output" button the function should change to "second_func()" of "Print Output" button.
We can say that before pressing "Change Output" button, "first_fucn" is executing, but after clicking it "second_func" should execute.
I don't know if you can understand my question or not, but please help me with this problem in any way you can.
import tkinter as tk
from PIL import Image, ImageTk
import itertools
def first_func():
canvas.create_text(10,10, text = 'First Function Executes', anchor="nw")
def second_func():
def executes():
canvas.create_text(15, 15, text = 'Second Function Executes')
canvas.itemconfigure(first_button, command =executes)
window = tk.Tk()
window.geometry("200x200")
canvas = tk.Canvas(window, width = 200, height = 200)
canvas.pack()
first_button = tk.Button(canvas, text="Print Output", command=first_func,anchor="nw", font = ("Bookman Old Style", 15, "bold"))
first_button_win = canvas.create_window(100, 60, window = first_button)
toggle_funcs = itertools.cycle((second_func, first_func))
def toggle():
func = next(toggle_funcs)
func()
second_button = tk.Button(canvas, text = "Change Output", command = toggle,anchor="nw", font = ("Bookman Old Style", 15, "bold"))
second_button_win = canvas.create_window(100, 120, window = second_button)
window.mainloop()
The cause of the error is that the itemconfigure method requires an item tag or id, but you're passing a button object.
If you're trying to change the command attribute of the button then you need to use the configure method of the button, not the canvas item.
first_button.configure(command=executes)

PySimpleGUI how to make transparent click-through window?

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

OptionMenu not working when using overridedirect on two different windows

I am using Python 3.6 on a mac, in view of creating controls for a heating system using a Raspberry Pi 3. I have created a master Tk window with a canvas in it to display graphics for on/off times. I am using an OptionMenu in a top-level popup window with overridedirect to remove the window decorations. I wish to also use overridedirect on the master Tk window, but when I use these two instances of overridedirect, my OptionMenu ceases to work(it works with just one use of overridedirect). The OptionMenu is displayed in the popup window, but does not drop down when clicked on. It is quite a long program, so I haven't included any of my code as I'm unsure what would be relevant. Any advice would be greatly appreciated!
Didn't think I could do it, which is why I didn't include code(kept getting errors), but managed it in the end!
from tkinter import *
master = Tk()
master.overrideredirect(1) # remove window border
master.geometry('800x480')
canvas_window = Canvas(master, width=800, height=305)
outer_rect = canvas_window.create_rectangle(39, 3, 760, 304, fill="BLUE")
close_button = Button(master, text="close", command=master.destroy)
close_button.pack()
def menu_sel_trg(self):
print("Menu Triggered")
def popup(*self):
popup_win = Toplevel()
popup_win.wm_overrideredirect(1) # remove window border
popup_win.geometry('250x250')
on_hrs_options = range(0, 24, 1)# sets range of list
on_hrs_variable = StringVar(popup_win)
on_hrs_variable.set(1)# menu default value
menu_on_hours = OptionMenu(popup_win, on_hrs_variable, *on_hrs_options, command=menu_sel_trg)
menu_on_hours.pack()
close_popup = Button(popup_win, text="Close", command=popup_win.destroy)
close_popup.pack()
canvas_window.tag_bind(outer_rect, '<Button-1>', popup)
canvas_window.pack()
master.mainloop()

Resources