tkinter - 调整文本小部件的大小(文本小部件的sizegrip)

时间:2017-11-25 21:51:40

标签: python tkinter

我的目标是拥有一个" sizegrip小部件"使用户能够调整文本小部件的大小。

图片说明:(我为糟糕的网格道歉)

enter image description here

是否可以使sizegrip小部件调整文本小部件而不是根窗口?

任何符合我需求的替代小部件?

1 个答案:

答案 0 :(得分:0)

如果你真的想这样做,这是一个使用place来管理包含文本小部件,滚动条和sizegrip的框架的示例。如果删除默认绑定并为sizegrip提供新绑定,则可以防止它更改顶层几何体并使其执行其他操作。 请注意,对于这种事情,一个或两个窗格是更正常的UI设计。

import sys
import tkinter as tk
import tkinter.ttk as ttk

class App(ttk.Frame):
    def __init__(self, parent, title=None):
        super().__init__(parent)
        parent.wm_geometry("600x400")
        parent.wm_title(title)
        self.top = tk.Frame(self, height=100, background="green4")
        self.middle = tk.Frame(self, background="red4")
        self.lower = tk.Frame(self, height=100, background="blue4")

        self.textframe = ttk.Frame(self.middle)
        self.text = tk.Text(self.textframe)
        self.vs = ttk.Scrollbar(self.textframe,
                                command=self.text.yview)
        self.sg = ttk.Sizegrip(self.textframe)
        self.text.configure(yscrollcommand=self.vs.set)
        self.text.grid(row=0, column=0, sticky="news")
        self.vs.grid(row=0, column=1, sticky="news")
        self.sg.grid(row=1, column=1, sticky="sew")
        self.textframe.grid_rowconfigure(0, weight=1)
        self.textframe.grid_columnconfigure(0, weight=1)
        self.middle.bind('<Map>', self.Place)

        self.sg.bindtags((self.sg, '.','all'))
        self.sg.bind('<Button-1>', self.Press)
        self.sg.bind('<B1-Motion>', self.Drag)
        self.sg.bind('<ButtonRelease-1>', self.Release)

        self.top.grid(row=0, column=0, sticky="news")
        self.middle.grid(row=1, column=0, sticky="news")
        self.lower.grid(row=2, column=0, sticky="news")
        self.grid_rowconfigure(1, weight=1)
        self.grid_columnconfigure(0, weight=1)
        self.grid(row=0,column=0, sticky="news")
        parent.grid_rowconfigure(0, weight=1)
        parent.grid_columnconfigure(0, weight=1)

    def Press(self, ev):
        self.dragging=True
        self.dragpos = (ev.x_root, ev.y_root)

    def Drag(self, ev):
        if self.dragging:
            dx = ev.x_root - self.dragpos[0]
            dy = ev.y_root - self.dragpos[1]
            sgx = self.sg.winfo_x() + dx
            sgy = self.sg.winfo_y() + dy
            sgw = self.sg.winfo_reqwidth()
            sgh = self.sg.winfo_reqheight()
            max_w = self.middle.winfo_width() - sgw
            max_h = self.middle.winfo_height() - sgh
            if sgx < 0 or sgx > max_w or sgy < 0 or sgy > max_h:
                return
            self.textframe.place(x=0, y=0,
                            width=sgx+sgw, height=sgy+sgh)
            self.dragpos = (ev.x_root, ev.y_root)

    def Release(self, ev):
        self.dragging=False

    def Place(self, ev):
        """Place the text widget controls once the rest of the
        application has been mapped so we actually have a size
        to work from.
        Reset the Map event to avoid re-calling this code."""
        self.textframe.bind('<Map>', None)
        self.textframe.place(x=0, y=0,
                             width=self.middle.winfo_width(),
                             height=self.middle.winfo_height())

def main(args=None):
    root = tk.Tk()
    app = App(root, "Sizegrip demo")
    root.mainloop()
    return 0

if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))

结果: screenshot of the resulting UI