1

感谢 stackoverflow 上的另一位成员,今天晚上早些时候我看到了这种很酷的技术。我是 python 的初学者,所以我的大部分学习都是通过阅读我拥有的这本书并编辑代码来了解它是如何工作的。

我正在尝试为菜单底部设计这个:

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X  TASK1  X  TASK2  X  TASK3  X  TASK4  X  TASK5  X
X   [ ]   X   [ ]   X   [ ]   X   [ ]   X   [ ]   X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

所以你基本上看到了这个:

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X                                                 X
X          TASK MANAGER - TASK SELECTION          X
X                                                 X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X                                                 X
X                 READY TO START?                 X
X              ---------------------              X
X                PRESS THE 'ENTER'                X
X                  KEY TO BEGIN!                  X
X              ---------------------              X
X                                                 X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X  TASK1  X  TASK2  X  TASK3  X  TASK4  X  TASK5  X
X   [ ]   X   [ ]   X   [ ]   X   [ ]   X   [ ]   X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

底部的目的是,如果您选择说任务 2,那么它只会为选择的任何一个打印一个函数......所以它会是(如果选择了任务 2):

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X                                                 X
X          TASK MANAGER - TASK SELECTION          X
X                                                 X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X                                                 X
X                 READY TO START?                 X
X              ---------------------              X
X                PRESS THE 'ENTER'                X
X                  KEY TO BEGIN!                  X
X              ---------------------              X
X                                                 X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X  TASK1  X  TASK2  X  TASK3  X  TASK4  X  TASK5  X
X   [ ]   X   [X]   X   [ ]   X   [ ]   X   [ ]   X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

这是当前代码:

def printLine (text = ''):
    print('X {: ^47} X'.format(text))

def printSelTitle (text = '', title = None):
    print('X{: ^9}'.format(text)),

def printSelCheck (text = '', title = None):
    print('X   [ ]   X'.format(text)),

def printBorder (title = None):
    print('X' * 51)

def printBox (*lines, title = None, showBottomBorder = False):
    printBorder(title)
    printLine()
    for line in lines:
        printLine(line)
    printLine()
    if showBottomBorder:
        printBorder()

printBox('TASK MANAGER - TASK SELECTION')
printBox('READY TO START?', '-' * 21, "PRESS THE 'ENTER'",\
         'KEY TO BEGIN!', '-' * 21, showBottomBorder = True)
printSelTitle('TASK1') and printSelTitle('TASK2')
printSelCheck()
printBorder()

printSelTitle('TASK3')
printSelTitle('TASK4')
printSelTitle('TASK5')

这是当前代码的样子:

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X                                                 X
X          TASK MANAGER - TASK SELECTION          X
X                                                 X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X                                                 X
X                 READY TO START?                 X
X              ---------------------              X
X                PRESS THE 'ENTER'                X
X                  KEY TO BEGIN!                  X
X              ---------------------              X
X                                                 X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X  TASK1  
X   [ ]   X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X  TASK3  
X  TASK4  
X  TASK5

有什么想法吗?提前感谢您的帮助。

最好的,

史蒂文

4

2 回答 2

3

如果您不想在 之后没有换行符print,请不要使用print(aasd),(这是元组构造),但是print('123', end='')

打印语法差异:Python 2.x:

print "1", # special syntax for print statement
print "2"

普顿3:

print("1", end="") # actually returns None (print() is a function)
print("2")

两种情况的结果:12

Python 3(交互式外壳):

>>> a = print('123')
123
>>> repr(a)
'None'
>>> a = print('123'),
123 # newline presents
>>> repr(a)
'(None,)' # result is tuple constructed by comma
于 2012-06-06T03:27:01.607 回答
0

这是代码:

def printLine (text = ''):
    print('X {: ^47} X'.format(text))

def printSelTitle (text = '', title = None):
    print('X{: ^9}'.format(text), end = ""),

def printSelCheck (text = '', title = None):
    print('X   [ ]   '.format(text), end = "")

def printBorder (title = None):
    print('X' * 51)

def printBox (*lines, title = None, showBottomBorder = False):
    printBorder(title)
    printLine()
    for line in lines:
        printLine(line)
    printLine()
    if showBottomBorder:
        printBorder()

printBox('TASK MANAGER - TASK SELECTION')
printBox('READY TO START?', '-' * 21, "PRESS THE 'ENTER'",\
         'KEY TO BEGIN!', '-' * 21, showBottomBorder = True)
printSelTitle('TASK1'), printSelTitle('TASK2'),\
        printSelTitle('TASK3'), printSelTitle('TASK4'),\
        printSelTitle('TASK5'), print("X")
printSelCheck(), printSelCheck(), printSelCheck(),\
                 printSelCheck(), printSelCheck(), print("X")
printBorder() 

这是它的样子:

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X                                                 X
X          TASK MANAGER - TASK SELECTION          X
X                                                 X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X                                                 X
X                 READY TO START?                 X
X              ---------------------              X
X                PRESS THE 'ENTER'                X
X                  KEY TO BEGIN!                  X
X              ---------------------              X
X                                                 X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X  TASK1  X  TASK2  X  TASK3  X  TASK4  X  TASK5  X
X   [ ]   X   [ ]   X   [ ]   X   [ ]   X   [ ]   X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

仍在尝试布局我将如何在选择时制作“X”......但我会进一步研究它。

于 2012-06-06T03:41:24.013 回答