简体   繁体   English

在不更改网格单元大小的情况下增加文本大小 tkinter

[英]Increase text size without changing grid cell size tkinter

As part of a project I'm making, I have the following function that creates a grid of specified x and y cells:作为我正在制作的项目的一部分,我有以下函数可以创建指定 x 和 y 单元格的网格:

def grid():
    x = 5
    z = 5
    for i in range(x * z):
        b = Label(letters, width=0, height=0,font=("Noto Sans SemiBold", 14), text=str(i)[0])
        b.grid(row=math.floor(i / x), column=i % x, sticky="nsew", padx=1, pady=1)
    for i in range(x):
        letters.columnconfigure(i, weight=1)
    for i in range(x + (z - x)):
        letters.rowconfigure(i, weight=1)

This works fine except the text is too small on each cell (ignore the current text, my final will be similar to wordle in that each cell will contain a large letter).这工作正常,除了每个单元格上的文本太小(忽略当前文本,我的最终将类似于 wordle,因为每个单元格将包含一个大字母)。 This is the current size这是当前尺寸

带有文本的网格太小

And this is what happens if I increase the text size:如果我增加文本大小,就会发生这种情况:

文本过大的网格

Essentially, the spacing around each character remains the same meaning the text still doesn't adequately fill the boxes.本质上,每个字符周围的间距保持不变,这意味着文本仍然不能充分填充框。 As such, my question is how can I increase the size of the text without also increasing the size of the cells, so my characters fill each cell.因此,我的问题是如何在不增加单元格大小的情况下增加文本的大小,以便我的字符填充每个单元格。

You can create a 1x1 blank image and add this image to every label, then you can specify the width and height in pixels which not affected by the size of the font used:您可以创建一个 1x1 空白图像并将此图像添加到每个标签,然后您可以指定不受所用字体大小影响的widthheight (以像素为单位):

...
blank = PhotoImage()

def grid():
    x = 5
    z = 5
    for i in range(x * z):
        b = Label(letters, width=100, height=100, image=blank, font=("Noto Sans SemiBold", 24), text=str(i)[0], compound='c')
        b.grid(row=math.floor(i / x), column=i % x, sticky="nsew", padx=1, pady=1)
    for i in range(x):
        letters.columnconfigure(i, weight=1)
    for i in range(x + (z - x)):
        letters.rowconfigure(i, weight=1)
...

Result of font size 24:字体大小 24 的结果:

在此处输入图像描述

Result of font size 64:字体大小为 64 的结果:

在此处输入图像描述

I suggest you use the place() geometry manager because it allows precise positioning.我建议您使用place()几何管理器,因为它允许精确定位。

import math
import tkinter as tk


def grid():
    x = 5
    z = 5

    fontsize = 64
    pad = 2
    cellsize = fontsize + pad
    font = ("Noto Sans SemiBold", -fontsize)  # Neg font size to set size in pixels.
    for i in range(x * z):
        b = tk.Label(letters, width=2, height=1, font=font, text=str(i)[0], relief='ridge')
        row, col = divmod(i, x)
        b.place(x=row*cellsize, y=col*cellsize)


root = tk.Tk()
root.geometry('400x400')
letters = tk.Frame(root)
letters.pack(fill='both', expand=True)
grid()
root.mainloop()

Here's some screenshots showing the results of using different fontsize values:下面是一些屏幕截图,显示了使用不同fontsize大小值的结果:

fontsize=24 : fontsize=24

截图1

fontsize=64 : fontsize=64

截图2

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

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