Python Tkinter 为文本框设置非活动边框?

发布于 2024-11-28 02:50:09 字数 30 浏览 0 评论 0原文

即使在非活动状态下,是否可以使文本框具有边框?

Is it possible to get a text box to have a border even when inactive?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

吹梦到西洲 2024-12-05 02:50:09

我不确定我知道“不活动时的边框”是什么意思,但您当然可以在 TkInter 中向文本添加边框。以下代码创建两个外部框架和两个内部框架,然后向每个内部框架添加一个文本。框架的所有边都有 5 px 的边框。

from Tkinter import *

root = Tk()

left_outer = Frame(root, bd=1)
left_outer.pack(side=LEFT, fill=Y, pady=5, padx=5)
right_outer = Frame(root, bd=1)
right_outer.pack(side=LEFT, fill=Y, pady=5, padx=5)

left = Frame(left_outer, bd=2, relief=SUNKEN)
right = Frame(right_outer, bd=2, relief=SUNKEN)
left.pack(fill=Y)
right.pack(fill=Y)

t_start = Text(left, width=20, height=200)
t_start.pack(side=LEFT, fill=Y)
s_start = Scrollbar(left)
s_start.pack(side=RIGHT, fill=Y)
s_start.config(command=t_start.yview)
t_start.config(yscrollcommand=s_start.set)

t_end = Text(right, width=20, height=200)
t_end.pack(side=LEFT, fill=Y)
s_end = Scrollbar(right)
s_end.pack(side=RIGHT, fill=Y)
s_end.config(command=t_end.yview)
t_end.config(yscrollcommand=s_end.set)

root.geometry("400x200")
root.mainloop()

I am not sure I know what "border when inactive" means, but you can certainly add a border to a Text in TkInter. The following code creates two outer frames and two inner frames, then adds a Text to each inner frame. The frames are given a border of 5 px on all sides.

from Tkinter import *

root = Tk()

left_outer = Frame(root, bd=1)
left_outer.pack(side=LEFT, fill=Y, pady=5, padx=5)
right_outer = Frame(root, bd=1)
right_outer.pack(side=LEFT, fill=Y, pady=5, padx=5)

left = Frame(left_outer, bd=2, relief=SUNKEN)
right = Frame(right_outer, bd=2, relief=SUNKEN)
left.pack(fill=Y)
right.pack(fill=Y)

t_start = Text(left, width=20, height=200)
t_start.pack(side=LEFT, fill=Y)
s_start = Scrollbar(left)
s_start.pack(side=RIGHT, fill=Y)
s_start.config(command=t_start.yview)
t_start.config(yscrollcommand=s_start.set)

t_end = Text(right, width=20, height=200)
t_end.pack(side=LEFT, fill=Y)
s_end = Scrollbar(right)
s_end.pack(side=RIGHT, fill=Y)
s_end.config(command=t_end.yview)
t_end.config(yscrollcommand=s_end.set)

root.geometry("400x200")
root.mainloop()
巾帼英雄 2024-12-05 02:50:09

odie5533 的答案包括为仅包含 Text 对象的框架提供边框。这是在文本对象周围提供 2D 效果的好方法,但在混合中添加了另一个小部件。我认为最初的问题与设置文本对象的边框宽度和浮雕类型有关。此代码片段在不涉及其他框架的情况下减轻了文本对​​象的负担。

from Tkinter import *

root = Tk()

text_top = Text(root, relief=GROOVE, height=5, width = 40, borderwidth=2)
text_top.pack()

text_bottom = Text(root, relief=RIDGE, height=5, width = 40, borderwidth=2)
text_bottom.pack()

root.geometry("400x200")
root.mainloop()

odie5533's answer covers giving a border to a frame that contains only a Text object. This is a great way to give a 2D around a text object, but adds another widget in the mix. I think the original question was related to setting both the border width and relief type of the Text object. This snippet gives a relief to the Text object without involving another frame.

from Tkinter import *

root = Tk()

text_top = Text(root, relief=GROOVE, height=5, width = 40, borderwidth=2)
text_top.pack()

text_bottom = Text(root, relief=RIDGE, height=5, width = 40, borderwidth=2)
text_bottom.pack()

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