首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Python在屏幕上只显示文本(透明背景,没有菜单栏或按钮)?

如何使用Python在屏幕上只显示文本(透明背景,没有菜单栏或按钮)?
EN

Stack Overflow用户
提问于 2022-03-11 22:09:30
回答 3查看 1.1K关注 0票数 0

我一直在到处寻找这种简单的解决方案,但我仍然很短。

我所要做的只是简单地将一些文本溅到屏幕上,没有背景,或者使用带有菜单栏/按钮的“消息框”。只是短信仅此而已。也许用一种方法来设置字体的颜色,以及我希望文本是浅灰的。使用Windows 10和Python。

我看过easygui (消息框,使用按钮),PySimpleGUI (到目前为止最好的选项,但默认设置为白色背景),Tkinter和pygame (但我不知道如何设置我在这些包中寻找的内容。

这就是我所使用的PySimpleGui,如果可能的话,希望这是一个透明的背景!

代码语言:javascript
运行
复制
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)

更新

这是我使用Tkinter最接近的东西。现在的问题是菜单栏仍然是可见的,并且使用Label类会产生坚实的背景。我怎么能只在屏幕上用PYTHON显示文本呢?我甚至不确定我这样做是正确的,但似乎是一个相当基本的想法?

代码语言:javascript
运行
复制
# 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()

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-03-12 07:08:15

不支持Textpopup中的透明背景。

在PySimpleGUI中自己需要并定义了新的弹出函数。

将您的transparency.

  • Set元素的
  • Set选项background_color设置为一种指定的颜色以提高透明度,例如“# hide 123”。
  • Set选项在Window中设置为Window中的指定颜色,以隐藏标题栏。H 214f 215

下面的代码显示了方法

代码语言:javascript
运行
复制
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()
票数 1
EN

Stack Overflow用户

发布于 2022-03-11 23:50:42

您正在尝试制作一种没有title-menu.的屏幕,在这种情况下,我可以告诉您使用root.overrideredirect(True),这是代码的一个关键部分。

票数 0
EN

Stack Overflow用户

发布于 2022-03-12 05:25:30

在没有背景的情况下绘制文本有两个选项。第一种方法是使用画布绘制文本,如下所示。使用这种方法,如果您关心大小,每次窗口调整大小时,您都必须自己对文本进行居中。

代码语言:javascript
运行
复制
canvas.create_text(x, y, "Text with no bg!")

第二种方法是使用一个禁用按钮,里面有一个空的img。

代码语言:javascript
运行
复制
img = PhotoImage(file="file.png")
b = tk.Button(root, text="My Text", image=img, relief="FLAT", state="DISABLED")
b.image = img
b.pack()

有关更多信息,请参见此answer。如果你还有其他问题,请告诉我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71445140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档