使用 TCL 扩展在 Tkinter 中设置本机窗口样式 [英] Using TCL extensions to set native window style in Tkinter

查看:33
本文介绍了使用 TCL 扩展在 Tkinter 中设置本机窗口样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pythonware.com/library/tkinter/introduction/...记录一个 overrideredirect 方法这将删除标题栏和边界,如果这还不够你必须设置原生窗口样式,我是不确定 Tkinter 是否给你那个一种低级访问,如果没有,请尝试类似的东西twapi.magicsplat.com/ui.html#set_window_styleTCL扩展

pythonware.com/library/tkinter/introduction/… documents a overrideredirect method that will remove thetitlebar and borders, if that is not enough you must set the native window style, I'm not sure if Tkinter gives you that kind of low-level access, if not, try the something like twapi.magicsplat.com/ui.html#set_window_style TCL extension

在之前的一篇文章中,我收到了这个关于如何在 Tkinter 中获得类似于下图的边框的回复.我不熟悉 Tcl 及其扩展.那么如何去做呢?最终目标基本上是在 Tkinter 窗口上获得下面的边框.

In an earlier post I got this as a reply on how to get a border in Tkinter similar to the one pictured below. I am not familiar with Tcl and it's extensions. So how would go about doing this? The end goal is basicaly to get the border below on a Tkinter window.

我在 Windows 7 上使用了以下内容,但它似乎没有改变样式.它可能缺少一些东西.任何帮助将不胜感激,这可能真的很酷!

I used the following on Windows 7 and it didn't seem to change the style. It's probably missing something. Any help would be appreciated, this could be really cool!

import string, win32ui, win32con
import Tkinter as tk

root = tk.Tk()

frame = win32ui.CreateWindowFromHandle(string.atoi(root.wm_frame(), 0))
frame.ModifyStyle(win32con.WS_CAPTION, 0, win32con.SWP_FRAMECHANGED)

root.mainloop()

推荐答案

您可以组合使用 Python win32 api 包和 Tkinter.您需要知道的是,Tk 窗口是 Win32 窗口的客户端部分.窗口管理器交互使用一个包装器来处理,该包装器是 Tk 窗口本身的父级.如果您有一个 Tkinter 窗口w",那么您可以为框架创建一个 PyWin32 窗口,或者直接操作它.您可以使用 w.wm_frame() 并解析返回的十六进制字符串或通过对来自 Tk 窗口的 winfo_id 值使用 GetParent 来获取帧 hwnd(尽管 wm_frame 可能更可靠).

You can do this using a combination of the Python win32 api packages and Tkinter. What you need to know is that a Tk window is the client section of a Win32 window. The window manager interactions are handled using a wrapper that is the parent of Tk window itself. If you have a Tkinter window 'w' then you can create a PyWin32 window for the frame or just manipulate it directly. You can get the frame hwnd using w.wm_frame() and parsing the hex string returned or by using GetParent on the winfo_id value from the Tk window (although wm_frame is likely to be more reliable).

import string, win32ui, win32con
from Tkinter import *
w = Tk()
frame = win32ui.CreateWindowFromHandle(string.atoi(w.wm_frame(), 0))
frame.ModifyStyle(win32con.WS_CAPTION, 0, win32con.SWP_FRAMECHANGED)

这将删除 WS_CAPTION 样式并通知窗口其框架已修改,这会强制重新计算几何,以便更改传播到 Tk 子窗口.

This removes the WS_CAPTION style and notifies the window that its frame is modified which forces a geometry recalculation so that the change propagates to the Tk child window.

编辑---以下安排确保我们在窗口完全创建并映射到显示后修改窗口样式.

EDIT --- The following arranges to ensure we modify the window style after the window has been fully created and mapped to the display.

import string, win32ui, win32con
from Tkinter import *

def decaption(event):
    w = event.widget
    frame = win32ui.CreateWindowFromHandle(string.atoi(w.wm_frame(), 0))
    frame.ModifyStyle(win32con.WS_CAPTION, 0, win32con.SWP_FRAMECHANGED)
    w.bind("<Map>", None)

root = Tk()
root.bind("<Map>", decaption)
root.mainloop()

这篇关于使用 TCL 扩展在 Tkinter 中设置本机窗口样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆