繁体   English   中英

调整大小后如何获取Tkinter Text小部件的高度?

[英]How to get the height of the Tkinter Text widget after resizing?

调整大小后如何获取Tkinter Text小部件的高度? 如何获得新的行数高度?

import tkinter as tk

class MainWindow:
    def __init__(self, parent):
        self.viewText = tk.Text(parent, height=32, width=20)
        self.viewText.grid(row=0, column=0, sticky=tk.NSEW)

        parent.grid_rowconfigure(0, weight=1)
        parent.grid_columnconfigure(0, weight=1)

if __name__ == "__main__":
    app = tk.Tk()
    window = MainWindow(app)
    app.mainloop()

绑定<Configure>事件。 例如:

import tkinter.font as tkfont

def on_configure(ev):
    """Get the current font and convert the pixel size into character sizes"""
    font = tkfont.nametofont(ev.widget.cget("font"))
    width = int(ev.width / font.measure("0"))
    height = int(ev.height / font.metrics('linespace'))
    print("{0}x{1}".format(width, height))

text.bind('<Configure>', on_configure)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM