在中心文本创建一个盒子 [英] Create a box with text in center

查看:153
本文介绍了在中心文本创建一个盒子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要画在我的消息框。其结果是这样的:

  ########################
 ##
 # 信息 #
 ##
 ########################

如果该消息是长或短的框必须被调整。
我试过的方法,但我不能算出它来进一步解决这个问题。

  = TOTAL_ROWS 5
total_cols = 25
矩阵= [''有效范围内(total_cols)X] y的范围内(TOTAL_ROWS)高清LBoder():
    因为我在范围内(TOTAL_ROWS):
        矩阵[I] [0] ='#'高清TBorder():
    在范围I(total_cols - 1):
        矩阵[0] [I] ='#'高清BBorder():
    在范围I(total_cols - 1):
        矩阵[TOTAL_ROWS - 1] [I] ='#'高清RBorder():
    因为我在范围内(TOTAL_ROWS):
        矩阵[I] [total_cols - 1] ='#'高清消息(MSG):
    矩阵[2] [1] =味精高清输出():
    数= LEN(测试)
    消息(测试)    TBorder()
    LBoder()
    RBorder()
    BBorder()    在矩阵行:
        orow =''
        在x行:
            orow + =+ STR(X)
        打印orow输出()


解决方案

只是单独建立的每一行:

emptyspace =
朗普蒂= LEN(emptyspace)
ADDS =消息
新闻=#+ emptyspace +增加了+ emptyspace +#
lnew = LEN(新闻)边缘=#
中期=#
因为我在范围内((lnew / 2 - 1)):
    边+ =#
    中期+ =
边+ =#
中期+ =#打印边缘
打印中旬
新闻出版
打印中旬
打印边缘

I want to draw a box around my message. The result to be like this:

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

The box must be resized if the message is longer or shorter. I tried a method, but I can't figure it out to solve it further.

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()

解决方案

Just build each line separately:

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

这篇关于在中心文本创建一个盒子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆