简体   繁体   English

tkinter 网格布局和 label 高度

[英]tkinter grid layout and label hight

I have an entry form with (among other widgets) an Entry widget to enter some value and a Listbox to show the valid values entered so far.我有一个条目表单(在其他小部件中)有一个Entry小部件来输入一些值和一个Listbox来显示到目前为止输入的有效值。 Below the Entry widget I show a validation message (empty when value is valid or error description if not) along with a big symbol ( '' for error and '' for valid).Entry小部件下方,我显示了一条验证消息(当值有效时为空,否则为错误描述)以及一个大符号( ''表示错误, ''表示有效)。

The following is a minimal example to simulate valid and invalid entries by clicking the button:以下是通过单击按钮模拟有效和无效条目的最小示例:

import tkinter as tk

root = tk.Tk()

tk.Entry(root).grid(column=0, row=0, sticky=tk.N)
tk.Listbox(root, height=10).grid(column=1, row=0, rowspan=3, sticky=tk.N)

label = tk.Label(root)
label.grid(column=0, row=1, sticky=tk.N)

symbol = tk.Label(root, font='Arial 72 bold')
symbol.grid(column=0, row=2, sticky=tk.S)

def toggle():
    n = 0 if len(label['text']) > 10 else 3
    label['text'] = '\n'.join([f'error message line {i}' for i in range(n)])
    symbol['text'] = '🗴' if n else '🗸'
        
tk.Button(root, text='toggle message', command=toggle).grid(column=0, row=3)

root.mainloop()

在此处输入图像描述

The symbol moves down when the error message becomes longer although there's still plenty of space between the bottom of the last error message line and the top of the symbol.当错误消息变长时,符号会向下移动,尽管在最后一个错误消息行的底部和符号顶部之间仍有足够的空间。 The reason is that the maximum character hight of the 72 pt font could use this extra space (can be easyly verified by inserting a full hight vertical line before the symbol.原因是 72 pt 字体的最大字符高度可以使用这个额外的空间(可以通过在符号前插入一条完整的高度垂直线来轻松验证。

Apart from decreasing the symbol font size (which I don't want) or increasing the total hight reserved for the message and the symbol (which I can't) I see the only workaround in replacing the character symbol by an image symbol where the strokes extend over the full image hight.除了减小符号字体大小(我不想要)或增加为消息和符号保留的总高度(我不能)之外,我看到了用图像符号替换字符符号的唯一解决方法,其中笔画延伸到整个图像高度。

So my question is: how to keep the button in place when the message line hight changes?所以我的问题是:当消息行高度发生变化时,如何将按钮保持在原位? Is it possible to somehow overlap the symbol and the message (knowing that the symbol character strokes don't fill the full font hight).是否有可能以某种方式重叠符号和消息(知道符号字符笔划不会填充完整的字体高度)。 I tried negative paddings but tkinter does not allow that.我尝试了负填充,但 tkinter 不允许这样做。 I also want to stick with the grid layout manager.我也想坚持使用grid布局管理器。

By making the second row resize to adapt to the window height and setting grid_propagate to False, then the button stays in place:通过调整第二行的大小以适应 window 高度并将grid_propagate设置为 False,然后按钮保持原位:

import tkinter as tk

root = tk.Tk()

tk.Entry(root).grid(column=0, row=0, sticky=tk.N)
tk.Listbox(root, height=10).grid(column=1, row=0, rowspan=3, sticky=tk.N)

root.rowconfigure(2, weight=1)  # make second row resize to fit window height 
label = tk.Label(root)
label.grid(column=0, row=1, sticky=tk.N)

symbol = tk.Label(root, font='Arial 72 bold')
symbol.grid(column=0, row=2, sticky=tk.S)

def toggle():
    n = 0 if len(label['text']) > 10 else 3
    label['text'] = '\n'.join([f'error message line {i}' for i in range(n)])
    symbol['text'] = '🗴' if n else '🗸'

tk.Button(root, text='toggle message', command=toggle).grid(column=0, row=3)
root.update_idletasks()
root.grid_propagate(False)  # disable window resizing when its children change size
root.mainloop()

Note that if the error message is very long, the symbol will not be visible.请注意,如果错误消息很长,则符号将不可见。

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

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