调整大小后如何获取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()
答案 0 :(得分:0)
绑定<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)