使用本机 Python 强制 tkinter 窗口呈现在其他所有内容下方

时间:2021-05-26 22:01:54

标签: python windows tkinter tk user32

我想创建一个位于所有其他窗口下方的 tkinter 窗口,并且无法使用 Window 的 SetWindowPos() 函数进行交互。我希望下面的代码可以解决这个问题,但是出现的窗口可以调整大小,可以接受焦点,可以接收鼠标点击,并且不会低于其他所有内容。

import time
import ctypes.wintypes
from ctypes import c_bool, c_int
from ctypes.wintypes import HWND, UINT
import tkinter

# Create a window and get its HWND
root = tkinter.Tk()
hwnd = root.winfo_id()

# Flags to move the window to the bottom and keep it from being touched
SWP_NOSIZE          = 0x0001
SWP_NOMOVE          = 0x0002
SWP_NOACTIVATE      = 0x0010
SWP_NOOWNERZORDER   = 0x0200
SWP_NOSENDCHANGING  = 0x0400

swpFlags = SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING

SetWindowPos = ctypes.windll.user32.SetWindowPos
SetWindowPos.argtypes = [HWND, HWND, c_int, c_int, c_int, c_int, UINT]
SetWindowPos.restype = c_bool

HWND_BOTTOM = 1

root.update()
SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 400, 600, swpFlags)

while True:
    time.sleep(0.1)
    root.update_idletasks()
    root.update()

2 个答案:

答案 0 :(得分:1)

根据您的评论,我相信您误解了 SetWindowPos 的使用。您想让您的窗口无法调整大小,并将 zorder 设置为底部以及其他一些内容。

这个 api 调用做了 3 件事,设置 zorder、大小和位置。但是,如果您只想设置其中的 1 或 2 个,您可以使用标志说“不要做那个项目”。它不会让窗口不再做那些事情。我相信您需要使用不同的 api 调用。

在上面的代码中,您实际上是说不要执行调用要执行的 3 件事中的任何一个,这会使调用变成 NOP。将所有 3 个一起使用似乎是一个编程错误。

我猜 Windows api 没有任何其他方式来拥有可选参数。他们可能应该将那个电话分成 3 个单独的电话。

对于您想要的,我认为您正在寻找不同的 api 调用,可能 SetWindowLongPtr 就是您正在寻找的。

在 tcl twapi 代码中,有 2 个小程序用于设置额外的样式位。您可以在 zip 下载 twapi 源代码

``https://sourceforge.net/projects/twapi/files/Development%20Releases/

然后在文件 ui.tcl 中有一些用于设置样式的代码。

# Update the frame - needs to be called after setting certain style bits
proc twapi::redraw_window_frame {hwin} {
    # 0x4037 -> SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE |
    #    SWP_NOMOVE | SWP_NOSIZE |
    #    SWP_NOZORDER | SWP_FRAMECHANGED
    SetWindowPos $hwin 0 0 0 0 0 0x4037
}

proc twapi::set_window_style {hwin style exstyle} {
    # GWL_STYLE -> -16, GWL_EXSTYLE -20
    set style [SetWindowLongPtr $hwin -16 $style]
    set exstyle [SetWindowLongPtr $hwin -20 $exstyle]

    redraw_window_frame $hwin
    return
}

答案 1 :(得分:0)

横向思考,grab_* 会是您用例的解决方案吗?您将创建一个窗口,将其设置为背景和合适的大小,然后在其顶部创建一个新窗口并执行 grab_set,强制应用程序中的交互仅发生在新窗口及其上孩子们。这种模式通常用于对话框窗口和弹出框以强制用户与特定窗口交互,但它也可以在这里工作,具体取决于您真正想要做什么。