在文本框中的文本周围制作矩形边框 python tkinter
I want to have a rectangle border around certain text that is added to the text box from the end and will be placed at the center.
For example:
Unfortunately, I can't find a way to do that, because I
don't know how to place texts at the center of the line in the text box, and don't know how to surround a text with a rectangle.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在
Text
小部件中使用justify='center'
将带有空格和换行符之间边框的Label
包裹起来。下面是一个示例:
结果:
You can wrap a
Label
with border between a space and newline withjustify='center'
in aText
widget.Below is an example:
Result:
尝试将文本框放入其自己的框架中。
像这样的事情:
Try putting the text box into it's own frame.
Some thing like this:
您可以使用
relief = "solid"
将边框添加到 Entry,使用outline
将文本居中,并且可以使用grid
来对齐小部件你想要的方式。其中大部分都很简单,

root.grid_columnconfigure
行通过将第一列的权重设置为 1,使网格占据根窗口的整个宽度。结果与您的示例非常相似:< br>You can add the border to the Entry using
relief = "solid"
, centre the text withoutline
and you can usegrid
to align the widgets the way you want.Most of this is straightforward, the

root.grid_columnconfigure
line makes the grid take up the full width of the root window by giving the first column a weight of 1. The result is very similar to your example:您可以使用
text.window_create()
在文本框中创建一个Entry
小部件。您可以自定义Entry
小部件的边框,并且您可以在其中键入文本。为了使其看起来更像文本框的一部分,您应该注册事件,以便当用户按 Right 并且插入符号是Entry
左侧的一个字符时,给出使用
焦点。您可以对 Left 执行相同的操作。focus_set
输入You could create an
Entry
widget in the textbox usingtext.window_create()
. You could customize the border of theEntry
widget, and you would be able to type text inside it. To make it look more like part of the textbox, you should register events so when the user presses Right and the caret is one character left of theEntry
, give theEntry
focus usingfocus_set
. You could do the same with the Left.