如何使用Python在没有窗口的屏幕上显示文本

2024-05-29 01:46:31 发布

您现在位置:Python中文网/ 问答频道 /正文

问题是:

我需要直接在没有窗口的屏幕上写文本。文本需要显示在所有其他窗口和全屏应用程序之上,不应以任何方式单击或交互。

示例: Screenshot of text on the screen. 如示例所示,文本不需要有透明的背景。我可以在Windows7上使用Python2或3。

我的解决方案:

我试着用Tkinter制作一个独立的标签:

编辑:借助Christian Rapp改进

import Tkinter
label = Tkinter.Label(text='Text on the screen', font=('Times','30'), fg='black', bg='white')
label.master.overrideredirect(True)
label.master.geometry("+250+250")
label.master.lift()
label.master.wm_attributes("-topmost", True)
label.master.wm_attributes("-disabled", True)
label.master.wm_attributes("-transparentcolor", "white")
label.pack()
label.mainloop()

什么有效:

  • 文本显示时没有窗口
  • 文本仍高于所有其他窗口
  • 背景可以是透明的

什么不:

  • 文本不显示在全屏应用程序上方
  • 文本块单击发生在其上的事件
  • 背景透明度不是alpha,所以有硬边

Tags: 文本mastertrue应用程序示例屏幕tkinter方式
2条回答

我也有类似的需求,发现pygame库对我所寻找的东西做得非常好。我能够生成非常大的文本,更新非常快,没有闪烁。请参阅下面的主题(第一个代码“solution”就是它):

Simple way to display text on screen in Python?

我加快了速度,而且很快。也使字体更大,这并没有太大的影响速度。所有这些都在一块橙色的圆珠笔板上运行(20美元)。您可以从命令行w/o GUI(或从窗口桌面)启动它,在任何一种情况下,它都是全屏的,不会显示为“窗口”应用程序。

作为Python和Pygame的新手,我的假设是可以加载一个图像文件作为背景,然后将文本放在上面。

哦,我用Tkinter的例子也试过了,速度慢,闪烁,字体看起来不太“正确”。皮加梅显然是赢家。它被设计成在屏幕上快速播放,而不会“撕扯”,因为当图像更新时,游戏不需要闪烁。我很惊讶它不依赖OpenGL,因为它很快。我认为Orange Pi不支持OpenGL(对此无法给出明确的答案)。所以对于二维的东西,哇,pygame是令人印象深刻的。!

原来这里有两个完全不同的问题。要在窗口上显示文本,您需要创建一个未装饰的最上面的窗口,并为背景设置色度键。但是,当运行全屏应用程序(如游戏)时,这将不起作用。在全屏应用程序上显示文本的唯一可靠方法是使用Direct3D挂钩。

我还没有写一个Direct3D钩子的例子,但是我会给第一个问题两个不同的解决方案。

解决方案1:Tkinter+pywin32

在本例中,我使用Tkinter完成了大部分工作,并使用win32api防止文本阻止鼠标单击。如果win32api对您不可用,那么您只需删除该部分代码。

import Tkinter, win32api, win32con, pywintypes

label = Tkinter.Label(text='Text on the screen', font=('Times New Roman','80'), fg='black', bg='white')
label.master.overrideredirect(True)
label.master.geometry("+250+250")
label.master.lift()
label.master.wm_attributes("-topmost", True)
label.master.wm_attributes("-disabled", True)
label.master.wm_attributes("-transparentcolor", "white")

hWindow = pywintypes.HANDLE(int(label.master.frame(), 16))
# http://msdn.microsoft.com/en-us/library/windows/desktop/ff700543(v=vs.85).aspx
# The WS_EX_TRANSPARENT flag makes events (like mouse clicks) fall through the window.
exStyle = win32con.WS_EX_COMPOSITED | win32con.WS_EX_LAYERED | win32con.WS_EX_NOACTIVATE | win32con.WS_EX_TOPMOST | win32con.WS_EX_TRANSPARENT
win32api.SetWindowLong(hWindow, win32con.GWL_EXSTYLE, exStyle)

label.pack()
label.mainloop()

解决方案2:pywin32

这个例子通过pywin32完成了所有事情。这使得它更复杂,更不易携带,但功能更强大。我在整个代码中都包含了到Windows API相关部分的链接。

import win32api, win32con, win32gui, win32ui

def main():
    hInstance = win32api.GetModuleHandle()
    className = 'MyWindowClassName'

    # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633576(v=vs.85).aspx
    # win32gui does not support WNDCLASSEX.
    wndClass                = win32gui.WNDCLASS()
    # http://msdn.microsoft.com/en-us/library/windows/desktop/ff729176(v=vs.85).aspx
    wndClass.style          = win32con.CS_HREDRAW | win32con.CS_VREDRAW
    wndClass.lpfnWndProc    = wndProc
    wndClass.hInstance      = hInstance
    wndClass.hCursor        = win32gui.LoadCursor(None, win32con.IDC_ARROW)
    wndClass.hbrBackground  = win32gui.GetStockObject(win32con.WHITE_BRUSH)
    wndClass.lpszClassName  = className
    # win32gui does not support RegisterClassEx
    wndClassAtom = win32gui.RegisterClass(wndClass)

    # http://msdn.microsoft.com/en-us/library/windows/desktop/ff700543(v=vs.85).aspx
    # Consider using: WS_EX_COMPOSITED, WS_EX_LAYERED, WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW, WS_EX_TOPMOST, WS_EX_TRANSPARENT
    # The WS_EX_TRANSPARENT flag makes events (like mouse clicks) fall through the window.
    exStyle = win32con.WS_EX_COMPOSITED | win32con.WS_EX_LAYERED | win32con.WS_EX_NOACTIVATE | win32con.WS_EX_TOPMOST | win32con.WS_EX_TRANSPARENT

    # http://msdn.microsoft.com/en-us/library/windows/desktop/ms632600(v=vs.85).aspx
    # Consider using: WS_DISABLED, WS_POPUP, WS_VISIBLE
    style = win32con.WS_DISABLED | win32con.WS_POPUP | win32con.WS_VISIBLE

    # http://msdn.microsoft.com/en-us/library/windows/desktop/ms632680(v=vs.85).aspx
    hWindow = win32gui.CreateWindowEx(
        exStyle,
        wndClassAtom,
        None, # WindowName
        style,
        0, # x
        0, # y
        win32api.GetSystemMetrics(win32con.SM_CXSCREEN), # width
        win32api.GetSystemMetrics(win32con.SM_CYSCREEN), # height
        None, # hWndParent
        None, # hMenu
        hInstance,
        None # lpParam
    )

    # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633540(v=vs.85).aspx
    win32gui.SetLayeredWindowAttributes(hWindow, 0x00ffffff, 255, win32con.LWA_COLORKEY | win32con.LWA_ALPHA)

    # http://msdn.microsoft.com/en-us/library/windows/desktop/dd145167(v=vs.85).aspx
    #win32gui.UpdateWindow(hWindow)

    # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633545(v=vs.85).aspx
    win32gui.SetWindowPos(hWindow, win32con.HWND_TOPMOST, 0, 0, 0, 0,
        win32con.SWP_NOACTIVATE | win32con.SWP_NOMOVE | win32con.SWP_NOSIZE | win32con.SWP_SHOWWINDOW)

    # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx
    #win32gui.ShowWindow(hWindow, win32con.SW_SHOW)

    win32gui.PumpMessages()

def wndProc(hWnd, message, wParam, lParam):
    if message == win32con.WM_PAINT:
        hdc, paintStruct = win32gui.BeginPaint(hWnd)

        dpiScale = win32ui.GetDeviceCaps(hdc, win32con.LOGPIXELSX) / 60.0
        fontSize = 80

        # http://msdn.microsoft.com/en-us/library/windows/desktop/dd145037(v=vs.85).aspx
        lf = win32gui.LOGFONT()
        lf.lfFaceName = "Times New Roman"
        lf.lfHeight = int(round(dpiScale * fontSize))
        #lf.lfWeight = 150
        # Use nonantialiased to remove the white edges around the text.
        # lf.lfQuality = win32con.NONANTIALIASED_QUALITY
        hf = win32gui.CreateFontIndirect(lf)
        win32gui.SelectObject(hdc, hf)

        rect = win32gui.GetClientRect(hWnd)
        # http://msdn.microsoft.com/en-us/library/windows/desktop/dd162498(v=vs.85).aspx
        win32gui.DrawText(
            hdc,
            'Text on the screen',
            -1,
            rect,
            win32con.DT_CENTER | win32con.DT_NOCLIP | win32con.DT_SINGLELINE | win32con.DT_VCENTER
        )
        win32gui.EndPaint(hWnd, paintStruct)
        return 0

    elif message == win32con.WM_DESTROY:
        print 'Closing the window.'
        win32gui.PostQuitMessage(0)
        return 0

    else:
        return win32gui.DefWindowProc(hWnd, message, wParam, lParam)


if __name__ == '__main__':
    main()

相关问题 更多 >

    热门问题