在文本框中的文本周围制作矩形边框 python tkinter

发布于 2025-01-10 12:52:16 字数 268 浏览 5 评论 0原文

我想要在某些文本周围有一个矩形边框,该文本从末尾添加到文本框并将放置在中心。

例如:

“在此处输入图像描述”"

不幸的是,我找不到办法做到这一点,因为我 不知道如何将文本放置在文本框中的行的中心,也不知道如何用矩形包围文本。

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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北座城市 2025-01-17 12:52:16

您可以在 Text 小部件中使用 justify='center' 将带有空格和换行符之间边框的 Label 包裹起来。

下面是一个示例:

import tkinter as tk

root = tk.Tk()

textbox = tk.Text(root, width=30, height=10)
textbox.pack()

textbox.tag_config('center', justify='center')

def center_label(textbox, **kwargs):
    textbox.insert('end', ' ', 'center')
    lbl = tk.Label(textbox, bd=3, relief='solid', **kwargs)
    textbox.window_create('end', window=lbl)
    textbox.insert('end', '\n\n')

center_label(textbox, text='hello', width=10, font='Arial 12 bold')
center_label(textbox, text='............', width=20)

textbox.insert('end', '\nhello\n')

root.mainloop()

结果:

在此处输入图像描述

You can wrap a Label with border between a space and newline with justify='center' in a Text widget.

Below is an example:

import tkinter as tk

root = tk.Tk()

textbox = tk.Text(root, width=30, height=10)
textbox.pack()

textbox.tag_config('center', justify='center')

def center_label(textbox, **kwargs):
    textbox.insert('end', ' ', 'center')
    lbl = tk.Label(textbox, bd=3, relief='solid', **kwargs)
    textbox.window_create('end', window=lbl)
    textbox.insert('end', '\n\n')

center_label(textbox, text='hello', width=10, font='Arial 12 bold')
center_label(textbox, text='............', width=20)

textbox.insert('end', '\nhello\n')

root.mainloop()

Result:

enter image description here

眼眸里的快感 2025-01-17 12:52:16

尝试将文本框放入其自己的框架中。

像这样的事情:

from Tkinter import *
root = Tk()

labelframe = LabelFrame(root, text="LabelFrame")
labelframe.pack()

text = Label(labelframe, text="Text inside labelframe")
text.pack()

root.mainloop()

Try putting the text box into it's own frame.

Some thing like this:

from Tkinter import *
root = Tk()

labelframe = LabelFrame(root, text="LabelFrame")
labelframe.pack()

text = Label(labelframe, text="Text inside labelframe")
text.pack()

root.mainloop()
早乙女 2025-01-17 12:52:16

您可以使用 relief = "solid" 将边框添加到 Entry,使用 outline 将文本居中,并且可以使用 grid 来对齐小部件你想要的方式。

import tkinter as tk

root = tk.Tk()
root.geometry("400x200")
root.grid_columnconfigure(0, weight = 1)

ent1 = tk.Entry(root, relief = "solid", justify = "center")
ent1.insert(0, "hello")
ent1.grid(row = 0, column = 0, pady = 10)

ent2 = tk.Entry(root, relief = "solid", justify = "center")
ent2.insert(0, ".......")
ent2.grid(row = 1, column = 0, pady = 10)

lab1 = tk.Label(root, text = "hello")
lab1.grid(row = 2, column = 0, sticky = "w")

lab2 = tk.Label(root, text = "hello")
lab2.grid(row = 3, column = 0, sticky = "w")

root.mainloop()

其中大部分都很简单,root.grid_columnconfigure 行通过将第一列的权重设置为 1,使网格占据根窗口的整个宽度。结果与您的示例非常相似:< br>
图片Tkinter 窗口

You can add the border to the Entry using relief = "solid", centre the text with outline and you can use grid to align the widgets the way you want.

import tkinter as tk

root = tk.Tk()
root.geometry("400x200")
root.grid_columnconfigure(0, weight = 1)

ent1 = tk.Entry(root, relief = "solid", justify = "center")
ent1.insert(0, "hello")
ent1.grid(row = 0, column = 0, pady = 10)

ent2 = tk.Entry(root, relief = "solid", justify = "center")
ent2.insert(0, ".......")
ent2.grid(row = 1, column = 0, pady = 10)

lab1 = tk.Label(root, text = "hello")
lab1.grid(row = 2, column = 0, sticky = "w")

lab2 = tk.Label(root, text = "hello")
lab2.grid(row = 3, column = 0, sticky = "w")

root.mainloop()

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:
Image of Tkinter window

皓月长歌 2025-01-17 12:52:16

您可以使用 text.window_create() 在文本框中创建一个 Entry 小部件。您可以自定义 Entry 小部件的边框,并且您可以在其中键入文本。为了使其看起来更像文本框的一部分,您应该注册事件,以便当用户按 Right 并且插入符号是 Entry 左侧的一个字符时,给出 使用focus_set输入焦点。您可以对 Left 执行相同的操作。

You could create an Entry widget in the textbox using text.window_create(). You could customize the border of the Entry 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 the Entry, give the Entry focus using focus_set. You could do the same with the Left.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文