Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 双下划线文本标记_Python_Tkinter - Fatal编程技术网

Python 双下划线文本标记

Python 双下划线文本标记,python,tkinter,Python,Tkinter,请我需要帮助使用双下划线的文本在tkinter。 示例代码是: allbl=tk.Label(hmpage,text='Purchases',font='calibri 25粗体双下划线',fg='white',bg='2a475e') 所有基线位置(relx=0.45,rely=0.25,anchor=“center”) 您似乎没有正确使用字体属性 我想说。。。 而不是写这封信: font='calibri 25 bold doubleunderline' 写下: font=('calibr

请我需要帮助使用双下划线的文本在tkinter。 示例代码是:

allbl=tk.Label(hmpage,text='Purchases',font='calibri 25粗体双下划线',fg='white',bg='2a475e')
所有基线位置(relx=0.45,rely=0.25,anchor=“center”)

您似乎没有正确使用字体属性

我想说。。。 而不是写这封信:

font='calibri 25 bold doubleunderline'
写下:

font=('calibri', 25, 'bold underline')
我也没有写双下划线,因为tkinter中没有类似的东西,所以你可以直接回溯

因此,正确的代码是:

allbl=tk.Label(hmpage,text='Purchases', font=('calibri', 25, 'bold underline'),fg='white',bg='#2a475e')
allbl.place(relx=0.45,rely=0.25,anchor="center")

经过长时间的搜索,我不得不接受这样一个悲惨的现实:在撰写本文时,tkinter不支持双下划线功能。

没有用于双下划线的字体选项,因此这不能用简单的
标签
小部件来完成。但是,可以基于
画布创建自定义类,以便在文本下方绘制两行:

import tkinter as tk
from tkinter.font import Font

class DblUnderlineLabel(tk.Canvas):
    def __init__(self, master, **kw):
        # collect text's properties
        font = Font(master, kw.pop('font', ''))
        text = kw.pop('text', '')
        if 'fg' in kw:
            fg = kw.pop('fg')
        else:
            fg = kw.pop('foreground', 'black')
        # initialize the canvas
        tk.Canvas.__init__(self, master, **kw)
        # display the text
        self.create_text(2, 2, anchor='nw', font=font, text=text, fill=fg, tags='text')
        h = font.metrics('linespace')  # font property needed to position correctly the underlining
        bbox = self.bbox('text')   # get text bounding box in the canvas
        w = font.actual('size')//8 # scale thickness of the underlining with fontsize
        # create the double underlining 
        self.create_line(bbox[0], h - 1, bbox[2], h - 1, fill=fg, width=w, tags='line')
        self.create_line(bbox[0], h + int(1.1*w), bbox[2], h + int(1.1*w), fill=fg, width=w, tags='line')
        # resize the canvas to fit the text
        bbox = self.bbox('all')
        self.configure(width=bbox[2], height=bbox[3])

root = tk.Tk()
for size in [8, 16, 32, 64]:
    DblUnderlineLabel(root, text="Arial %i bold" % size, font="Arial %i bold" % size).pack()
root.mainloop()    
文本示例:

有什么问题吗?欢迎光临!请阅读添加并相应地更新您的问题。没有“doubleunderline”字体属性,因此您不能使用简单的
标签
。有了
文本
小部件,我想您可以将其改为双下划线,而不是它提供的曲线。或者您可以使用
画布
创建文本并在下面画两行。谢谢您,亚达夫。我很高兴。:)哇,谢谢你million@Belz不客气!tkinter中很少有现成的“奇特”功能,但一旦您熟悉了这个库,您就可以自己实现它们或在线查找现有代码。@Belz:请参阅