Tkinter 标签小部件中的下划线文本?
我正在开发一个项目,需要我在 Tkinter 标签小部件中的某些文本下划线。我知道可以使用下划线方法,但根据参数,我似乎只能让它为小部件的 1 个字符添加下划线。即
p = Label(root, text=" Test Label", bg='blue', fg='white', underline=0)
change underline to 0, and it underlines the first character, 1 the second etc
我需要能够在小部件中的所有文本下划线,我确信这是可能的,但是如何呢?
我在 Windows 7 上使用 Python 2.6。
I am working on a project that requires me to underline some text in a Tkinter Label widget. I know that the underline method can be used, but I can only seem to get it to underline 1 character of the widget, based on the argument. i.e.
p = Label(root, text=" Test Label", bg='blue', fg='white', underline=0)
change underline to 0, and it underlines the first character, 1 the second etc
I need to be able to underline all the text in the widget, I'm sure this is possible, but how?
I am using Python 2.6 on Windows 7.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
要为标签小部件中的所有文本添加下划线,您需要创建一种新字体,并将下划线属性设置为 True。这是一个例子:
To underline all the text in a label widget you'll need to create a new font that has the underline attribute set to True. Here's an example:
对于那些使用 Python 3 且无法使下划线起作用的人,可以使用以下示例代码来使其起作用。
For those working on Python 3 and can't get the underline to work, here's example code to make it work.
内线
oneliner
试试这个下划线:
Try this for underline:
输入你自己的字体(我选择helvetica 8)
put your own font (i choose helvetica 8)
要为所有字符添加下划线,您应该导入 tkinter.font 并用它创建您自己的字体样式。例子-
To underline all the characters you should import tkinter.font and make your own font style with this. Example-
应采用以下格式:
dev_label=Label(Right_frame, text="[email] protected]", font=("Times",15,"粗斜体下划线"), fg="black",bg="white")
dev_label.place(x=80 ,y=120)
should be in this format:
dev_label=Label(Right_frame, text="[email protected]", font=("Times",15,"bold italic underline"), fg="black",bg="white")
dev_label.place(x=80,y=120)