在tkinter中获得字体的高和宽
# 获取字体的实际高度(行高) font_height = self.axis_font.metrics("linespace") print(f"Font height (linespace): {font_height} pixels") # 创建一个隐藏的Label来测量文本宽度 # hidden_label = tk.Label(root, text="Sample Text", font=self.axis_font) # hidden_label.pack() # hidden_label.update_idletasks() # 更新小部件以计算其尺寸 # text_width = hidden_label.winfo_width() # print(f"Text width: {text_width} pixels") # 临时绘制轴标注以获取其边界框 self.x_label_temp = self.create_text(350, 200, text="XXXXXXX", fill="black", font=self.axis_font, anchor="w") # 获取轴标注的边界框 self.x_label_bbox = self.bbox(self.x_label_temp) print(self.x_label_bbox)
在类初始化时,测试有效的是
# 获取字体的实际高度(行高) font_height = self.axis_font.metrics("linespace") print(f"Font height (linespace): {font_height} pixels") #Font height (linespace): 24 pixels # 创建一个Label来测量文本宽度 self.x_label_temp = self.create_text(350, 200, text="XXXXXXX", fill="black", font=self.axis_font, anchor="w") # 获取轴标注的边界框 self.x_label_bbox = self.bbox(self.x_label_temp) print(self.x_label_bbox)#(349, 188, 449, 212) x1=349 x2=449 y1=188 y2=212 212-188 = 24
需要加库 :from tkinter import font

浙公网安备 33010602011771号