簡體   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