python保持窗口_Python GUI窗口保持在顶部,没有焦点

这仅限于windows,但要使用python将窗口的样式设置为不获取焦点,可以使用ctypes:import tkinter as tk

from ctypes import windll, wintypes

GWL_STYLE = -16

GWL_EXSTYLE = -20

WS_CHILD = 0x40000000

WS_EX_APPWINDOW = 0x00040000

WS_EX_TOOLWINDOW = 0x00000080

WS_EX_NOACTIVATE = 0x08000000

SWP_FRAMECHANGED = 0x0020

SWP_NOACTIVATE = 0x0010

SWP_NOMOVE = 0x0002

SWP_NOSIZE = 0x0001

# write short names for functions and specify argument and return types

GetWindowLong = windll.user32.GetWindowLongW

GetWindowLong.restype = wintypes.ULONG

GetWindowLong.argtpes = (wintypes.HWND, wintypes.INT)

SetWindowLong = windll.user32.SetWindowLongW

SetWindowLong.restype = wintypes.ULONG

SetWindowLong.argtpes = (wintypes.HWND, wintypes.INT, wintypes.ULONG)

SetWindowPos = windll.user32.SetWindowPos

def find_root_window(win): # takes tkinter window ref

w_id = win.winfo_id() # gets handle

style = GetWindowLong(w_id, GWL_STYLE) # get existing style

newstyle = style & ~WS_CHILD # remove child style

res = SetWindowLong(w_id, GWL_STYLE, newstyle) # set new style

res = SetWindowPos(w_id, 0, 0,0,0,0, SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE)

hwnd = int(root.wm_frame(), 16) # find handle of parent

res = SetWindowLong(w_id, GWL_STYLE, style) # set back to old style

res = SetWindowPos(w_id, 0, 0,0,0,0, SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE)

return hwnd # return parents handle

def set_no_focus(hwnd):

style = GetWindowLong(hwnd, GWL_EXSTYLE) # get existing style

style = style & ~WS_EX_TOOLWINDOW # remove toolwindow style

style = style | WS_EX_NOACTIVATE | WS_EX_APPWINDOW

res = SetWindowLong(hwnd, GWL_EXSTYLE, style)

res = SetWindowPos(hwnd, 0, 0,0,0,0, SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE)

def push_me():

print('you pushed me!')

def focus_me(event):

root.focus_force()

root = tk.Tk()

root.wm_attributes("-topmost", 1)

tk.Button(root, text="Push me", command=push_me).pack()

e = tk.Entry(root)

e.pack()

e.bind('', focus_me) # if we have a widget that must have focus it needs to be bound

root.update() # for some reason, window style is messed with after window creation, update to get past this

hwnd = find_root_window(root)

if hwnd:

set_no_focus(hwnd)

root.mainloop()

您需要根据应用程序环境使用正确的Get/Set函数。对于32位窗口,您似乎需要使用:

GetWindowLongW和{}

GetWindowLongA和{}

但64位需要:

GetWindowLongPtrW和{}

GetWindowLongPtrA和{}

见this

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值