python - tkinter wraplength 单位是像素吗?

标签 python tkinter

Re:tkinter.Label 的 wraplength,来自 John Shipman 的 NM Tech 文档:“您可以通过将此选项设置为所需的数字来限制每行中的字符数。默认值 0 表示行将只在换行符处被打破。”其他来源同意 wraplength 的单位是字符。

下面的代码似乎打破了界限,好像 wraplength 的单位是像素,而不是字符。例如,如果我将 wraplength 设置为 10,则标签会显示一列一或两个字符宽的文本。如果我将 wraplength 设置为 20,则行长为 3 或 4 个字符。

在我的应用程序中,用户将为自定义表单创建自己的简单小部件,如果 wraplength 选项使用字符计数而不是它正在执行的任何操作会更好。由于 NMTech 文档适用于 Tkinter 8.5,但 8.6 是我的 Python 3.5 附带的,也许这解释了差异,但我没有看到 8.6 的文档。

但理想情况下,行应该在单词之间最近的空格处换行,而 wraplength 仅用作最大行长度。因此,如果用户无论如何都必须输入自己的\n,那么 wraplength 对我来说似乎毫无用处。

总结:在为自己的简单小部件输入选项值的用户的 GUI 中,1) 有没有办法让 Tkinter 接受包裹长度单位的字符和 2) 我可以在最近的地方换行吗wraplength 的空白,其中 wraplength 仅用作最大行长度,而不是绝对行长度?

感谢任何解决方案或建议。

import tkinter as tk

root = tk.Tk()

sib = tk.Label(root, text='Give em hell Harry', wraplength=10)
sib.grid()

root.mainloop()

最佳答案

Is there a way to get Tkinter to accept characters for wraplength units

不,没有。如果您需要在单词边界处进行换行,您可以使用文本小部件而不是标签。

除非另有说明,否则单位为像素。来自 canonical tk documentation :

wraplength For widgets that can perform word-wrapping, this option specifies the maximum line length. Lines that would exceed this length are wrapped onto the next line, so that no line is longer than the specified length. The value may be specified in any of the standard forms for screen distances. If this value is less than or equal to 0 then no wrapping is done: lines will break only at newline characters in the text.

在上面的文字中,“屏幕距离的任何标准形式”是指您可以使用后缀来指定打印机点的距离(例如:“72p”),厘米(例如:“2.54c”)、毫米(例如:“1000m”)或英寸(例如:“1i”)

关于python - tkinter wraplength 单位是像素吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55241150/

相关文章:

python - 从集合创建一个 numpy 数组

python - 如何检查 screen 是否正在运行?

python - 如何绑定(bind)到 Tkinter 中的所有数字键?

python - 如何在bash中读取python的输入

python - unsigned char* 图像到 Python

python - tf.histogram_fixed_width() 是否支持反向传播?

python - 如何在 Linux 中的键盘上获取箭头键和回车键,使其表现得像 Windows7

python - 重置主 GUI 窗口

tkinter - 类型错误 : __str__ returned non-string (type instance)

python - 如何在 python/tkinter 中允许函数每分钟只调用一次?