繁体   English   中英

在网格布局中自动调整Tkinter标签的大小

[英]Automatically resizing of Tkinter labels inside a grid layout

我在Tkinter和Python上还很陌生,所以也许这对您来说很容易,但是我找不到解决此问题的方法...

我想创建这样的东西:

在此处输入图片说明

因此,我正在使用网格布局管理来创建每个单个单元格,并在每个单元格中存储一个标签。

我当前的代码是:

from tkinter import *

class RCP:

    frame = None
    commandsHandle = None

def __init__(self, targets):
    self.n_row = len(targets)
    self.n_col = len(targets[0])
    self.targets = targets
    self.canvasRoot = Tk()

def createGrid(self):
    # Create and configure the root
    Grid.rowconfigure(self.canvasRoot, 0, weight=1)
    Grid.columnconfigure(self.canvasRoot, 0, weight=1)

    # Create and configure the frame
    frame = Frame(self.canvasRoot)
    frame.grid(row=0, column=0, sticky="NESW")

    # Create the grid of commands
    commandsHandle = [[None for i in range(self.n_col)] for j in range(self.n_row)]
    for row_index in range(self.n_row):
        Grid.rowconfigure(frame, row_index, weight=1)
        for col_index in range(self.n_col):
            Grid.columnconfigure(frame, col_index, weight=1)
            commandsHandle[row_index][col_index] = Label(text=self.targets[row_index][col_index])
            commandsHandle[row_index][col_index].grid(row=row_index, column=col_index)

    self.canvasRoot.mainloop()


targets = [['A','B','C','D'],['E','F','G','H'],['I','J','K','L']]
myRCP = RCP(targets)
myRCP.createGrid()

,绘制此图:

在此处输入图片说明

但是我有两个问题要解决:

  • 在每个网格单元内将标签文本居中
  • 自动调整标签的文本大小以填充整个单元格

我该怎么做?

提前致谢。

因此,这绝不是一个完美的解决方案(并在我的机器上获得了出色的性能),但您可以这样做:

from tkinter import *

text = [['A','B','C','D'],['E','F','G','H'],['I','J','K','L'],['M', 'N', 'O', 'P'],['Q','R','S','T'],['U','V','W','X'],['Y','Z','1','2'],['3','4','5','6']]

root = Tk()

for c, i in enumerate(text):
    Grid.rowconfigure(root, c, weight=1)
    for a, b in enumerate(i):
        Label(root, text=b, font=('', 10)).grid(row=c, column=a)
        Grid.columnconfigure(root, a, weight=1)

class Resize:
    def __init__(self, root):
        self.root = root
        self.root.update()
        self.startsize = (root.winfo_width(),root.winfo_height())
        self.root.bind("<Configure>", self.on_resize)
    def on_resize(self, event):
        for i in self.root.winfo_children():
            i.config(font=('', int(10*((((self.root.winfo_width() * root.winfo_height()) - (self.startsize[0] * self.startsize[1]))/(self.startsize[0] * self.startsize[1]))+1))))

Resize(root)
root.mainloop()

要回答您的查询:


在每个网格单元内将标签文本居中

小部件默认位于网格“单元”的中心,我假设您的意思是使它们均匀地分布在窗口上。 使用以下命令可以完成此操作:

Grid.rowconfigure(root, c, weight=1)

Grid.columnconfigure(root, a, weight=1)

在这里,我们为每个网格columnrow赋予相等的权重1 这意味着每一column将共享窗口获得的任何额外的x轴空间,每一row共享窗口获得的任何额外的y轴空间。 这意味着每个“单元”均与窗口的其他子项平均共享空间,因此它们被均匀分布。


自动调整标签的文本大小以填充整个单元格

为了实现这一点,我们需要计算与原始大小相比的窗口大小增加。 所以我们使用这个长方程:

((((self.root.winfo_width() * root.winfo_height()) - (self.startsize[0] * self.startsize[1]))/(self.startsize[0] * self.startsize[1]))+1)

为了使它更具可读性,它与以下内容相同:

(((((当前窗口x轴*当前窗口y轴)-(起始窗口x轴*起始窗口y轴))/((起始窗口x轴*起始窗口y轴))+ 1

要么

((当前窗口区域-原始窗口区域)/原始窗口区域)+ 1

考虑到窗口的原始大小,这导致窗口面积的百分比增加。

这意味着,如果窗口的大小与其原始大小相比增加了10%,那么我们最终将得到1.1作为等式的结果。 然后,将原始字体大小(为方便起见,为10)乘以等式( 1.1 )的结果,得到新的字体大小11,比10大10%,这意味着我们的字体大小随窗口缩放。

不幸的是,我还没有找到一种方法让程序检测标签的字体何时增加太多,以至于迫使文本超出网格“ cell”的边界。 这意味着,当在x轴上扩展窗口时,我们最终会遇到以下情况:文本的垂直高度导致部分字母被切除。 我目前没有解决这个问题的方法。 如果有办法,我将更新我的答案。

暂无
暂无

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

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