Tkinter:没有标题栏但可调整大小的窗口 [英] Tkinter: windows without title bar but resizable

查看:64
本文介绍了Tkinter:没有标题栏但可调整大小的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所知道的是如果我想创建一个没有标题栏的窗口,我可以写

What I know is if I want to create a window without title bar, I can write

    root = Tk()
    ........
    root.overrideredirect(1)

但我也希望窗口可以调整大小.有什么解决办法吗?

But I would also like the window to be resizable. Is there any solution?

(仅供参考:我在 Windows 机器上工作,虽然我不确定它是否真的很重要.如果有独立于操作系统的解决方案就完美了,但如果至少有一个适用于 Windows 的解决方案,我很高兴.)

(FYI: I am working on Windows machine, although I am not sure if it really matters. It will be perfect if there is OS-independent solution, but I am happy if there is at least a solution for Windows first.)

推荐答案

问题是,窗口可调整大小的,但是当你打开 overrideredirect 你失去任何排序您可以抓取以调整窗口大小的标题或边缘.唯一的解决方案是自己实施调整大小.您可以添加自己的边框,或添加在鼠标靠近边缘时起作用的鼠标绑定.

The problem is, the window is resizable, but when you turn on overrideredirect you lose any sort of header or edge that you can grab in order to resize the window. The only solution is to implement resizing yourself. You can add your own borders, or add mouse bindings that work when the mouse is near an edge.

此答案显示了如何移动这样的窗口:Python/Tkinter:鼠标拖动没有边框的窗口,例如.覆盖直接(1)

This answer shows how to move such a window: Python/Tkinter: Mouse drag a window without borders, eg. overridedirect(1)

这是一个说明调整大小的简短示例.它几乎没有在 OSX 上进行过测试,但应该可以在任何平台上运行.它使用 python2,但它应该只通过更改 import 语句就可以与 python3 一起使用.

Here's a short example that illustrates resizing. It has only barely been tested on OSX but should work on any platform. It uses python2, though it should work with python3 just by changing the import statements.

import Tkinter as tk
import ttk

class Example(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.floater = FloatingWindow(self)

class FloatingWindow(tk.Toplevel):
    def __init__(self, *args, **kwargs):
        tk.Toplevel.__init__(self, *args, **kwargs)
        self.overrideredirect(True)
        self.wm_geometry("400x400")

        self.label = tk.Label(self, text="Grab the lower-right corner to resize")
        self.label.pack(side="top", fill="both", expand=True)

        self.grip = ttk.Sizegrip(self)
        self.grip.place(relx=1.0, rely=1.0, anchor="se")
        self.grip.lift(self.label)
        self.grip.bind("<B1-Motion>", self.OnMotion)


    def OnMotion(self, event):
        x1 = self.winfo_pointerx()
        y1 = self.winfo_pointery()
        x0 = self.winfo_rootx()
        y0 = self.winfo_rooty()
        self.geometry("%sx%s" % ((x1-x0),(y1-y0)))
        return

app=Example()
app.mainloop()

这篇关于Tkinter:没有标题栏但可调整大小的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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