中的方框图字符问题

2024-09-28 05:24:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个终端音乐播放器。我几乎做了盒绘图界面^{货币1}$

如您所见,我打破了右边框("║")。我试过了。但是出了点问题,它不起作用。看看我的代码:

import vlc
import glob
import sys
from termcolor import colored, cprint
def scanfolder(directory, extension):
    sfile = glob.glob(f'{directory}*.{extension}')
    box1 = colored("║", "yellow", attrs=["bold"])
    box2 = colored("╚", "yellow", attrs=["bold"])
    box3 = colored("═", "yellow", attrs=["bold"])
    box4 = colored("╔", "yellow", attrs=["bold"])
    box5 = colored("╗", "yellow", attrs=["bold"])
    box6 = colored("╝", "yellow", attrs=["bold"])
    x = len(directory)
    y = len(extension) + 1
    z = len(max(sfile, key=len))
    z = z - x - y + 3
    box3 = box3 * z
    print(box4 + box3 + box5)
    for i, track in enumerate(sfile):
        trackEn = box1 + str(i + 1) + ". " + str(track[x:-y])
        indent = " "
        mFactor = z - len(trackEn) #z is the most long track name length, len(trackEn) is any other track's length
        indent = indent * mFactor #space * mFactor, used to align box drawing character ║, so they can make one solid wall
        print(trackEn + indent + box1) #track name + aligning spaces + ║
    print(box2 + box3 + box6)
sext = input("Choose extension: ")
sdir = input("Choose directory: ")
cprint("""
________          ________ ______                 
___  __ \_____  _____  __ \___  /______ ______  __
__  /_/ /__  / / /__  /_/ /__  / _  __ `/__  / / /
_  ____/ _  /_/ / _  ____/ _  /  / /_/ / _  /_/ / 
/_/      _\__, /  /_/      /_/   \__,_/  _\__, /  
         /____/                          /____/   """, "yellow", "on_blue", attrs=["bold"])
scanfolder(sdir, sext)
chooseTrack = colored("Choose track", "green", attrs=["bold"])
colon = colored(": ", "green", attrs=["bold", "blink"])
trackn = input(chooseTrack + colon)

Tags: importlenextensiontrackdirectoryattrsglobbold
1条回答
网友
1楼 · 发布于 2024-09-28 05:24:58

Question: Issues with box drawing character using termcolor.colored symbols.

问题是从termcolor.colored返回的box1变量。
这个字符串box1已经用ANSI颜色序列格式化,例如\E231║
因此len(trackEn)给出了6更多的结果mFactor = z - len(trackEn) == -6
你的mFactor变为负数,因此indent



Change the following:

    for i, track in enumerate(sfile):
        trackEn = str(i + 1) + ". " + str(track[x:-y])

        # z is the most long track name length, len(trackEn) is any other track's length
        mFactor = z - len(trackEn)

        # space * mFactor, used to align box drawing character ║, 
        # so they can make one solid wall 
        indent = " " * mFactor 

        # ║ + track name + aligning spaces + ║
        print(box1 + trackEn + indent + box1) 

相关问题 更多 >

    热门问题