python Tkinter调整标签字体但不调整标签大小

时间:2016-02-19 03:35:45

标签: python tkinter

请教我如何调整标签字体大小以适应标签宽度和高度。现在,每当我调整标签字体大小时,标签尺寸也会变大。下面是我的代码:

self.the_font = tkFont.Font(family="helvetica", size=18)

self.table_num = Tkinter.StringVar()
self.tbl_num = Tkinter.Label(self, anchor="c",fg="black", bg = "orange", width = 15, height = 7, font = self.the_font, relief="sunken", textvariable = self.table_num)
self.tbl_num.place(relx = .75, rely = .5, anchor = "c")

然后,我的另一个函数将更新标签如下:

def GetINFO(self) :
    ID_input = self.user_text.get()
    ID_input = ID_input.strip("\n")
    ID_input = ID_input.strip(" ")

    ID_input = int(ID_input)

    wb = openpyxl.load_workbook('example.xlsx')
    A = wb.get_sheet_by_name('Full List')

    for index, row in enumerate(A.iter_rows()):

        row_details = [cell.value for cell in row]

        for data in row_details :
            #data = data.strip()
            #data  = int(data)
            if data == ID_input :
                HID_num = row_details[0]
                emp_num = row_details[1]
                emp_name = row_details[2]
                tbl_num = row_details[3]

    self.employee_name.set(emp_name)
    self.employee_id.set(emp_num)
    self.reg_status.set("Registered")

    size = self.the_font.cget("size")
    size += 7
    self.the_font.configure(size=size)
    self.table_num.set(tbl_num)

我想要调整大小

是table_num

1 个答案:

答案 0 :(得分:0)

如果标签显示文字,则以文字为单位给出尺寸。如果标签显示图像,则以像素为单位给出尺寸 如果大小设置为0或省略,则根据标签内容计算。

  
    

高度取决于字体。

  

您可以使用width方法获取文字的heightfont.mesure()

import Tkinter
import tkFont

root = Tkinter.Tk()

the_font = tkFont.Font(family="helvetica", size=36)

w, h = (the_font.measure('test size'), the_font.metrics("linespace"))