且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Tkinter:没有标题栏但可调整大小的窗口

更新时间:2023-01-13 22:09:56

问题是,窗口可调整大小的,但是当你打开 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()