创建一个中心文本框

时间:2015-06-29 12:40:45

标签: python arrays grid box

我想在我的信息周围画一个方框。结果是这样的:

 # # # # # # # # # # # # # # # # # # # # # # # # #
 #                                               #
 #                   Message                     #
 #                                               #
 # # # # # # # # # # # # # # # # # # # # # # # # #

如果邮件更长或更短,则必须调整框的大小。 我尝试了一种方法,但我无法弄清楚它是否能够进一步解决它。

total_rows = 5
total_cols = 25
Matrix = [[' ' for x in range(total_cols)] for y in range(total_rows)]

def LBoder():
    for i in range(total_rows):
        Matrix[i][0] = '#'

def TBorder():
    for i in range(total_cols - 1):
        Matrix[0][i] = '#'

def BBorder():
    for i in range(total_cols - 1):
        Matrix[total_rows - 1][i] = '#'

def RBorder():
    for i in range(total_rows):
        Matrix[i][total_cols - 1] = '#'

def message(msg):
    Matrix[2][1] = msg

def output():
    count = len('Test')
    message('Test')

    TBorder()
    LBoder()
    RBorder()
    BBorder()

    for row in Matrix:
        orow = ''
        for x in row:
            orow += " " + str(x)
        print orow

output()

1 个答案:

答案 0 :(得分:0)

分别构建每一行:

emptyspace = "                   "
lempty = len(emptyspace)
addS = "Message"
news = "#" + emptyspace + addS + emptyspace + "#"
lnew = len(news)

edge = "# "
mid = "# "
for i in range((lnew / 2 - 1)):
    edge += "# "
    mid += "  "
edge += "#"
mid += "#"

print edge
print mid
print news
print mid
print edge