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()
CodePudding user response:
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.
CodePudding user response:
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.
CodePudding user response:
Transparent background of Text
in popup
is not supported.
New popup function required and defined by yourself in PySimpleGUI.
- Set option
background_color
of yourText
element to one specified color for transparency, like '#add123'. - Set option
transparent_color
inWindow
to the specified color for transparency. - Set option
no_titlebar
inWindow
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()